2 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
4 * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
5 * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/bug.h>
17 #include <linux/interrupt.h>
18 #include <linux/device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/list.h>
21 #include <linux/slab.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-mem2mem.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
30 #include "fimc-mdevice.h"
31 #include "fimc-core.h"
33 static int fimc_init_capture(struct fimc_dev
*fimc
)
35 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
36 struct fimc_sensor_info
*sensor
;
40 if (fimc
->pipeline
.sensor
== NULL
|| ctx
== NULL
)
42 if (ctx
->s_frame
.fmt
== NULL
)
45 sensor
= v4l2_get_subdev_hostdata(fimc
->pipeline
.sensor
);
47 spin_lock_irqsave(&fimc
->slock
, flags
);
48 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
49 fimc_set_yuv_order(ctx
);
51 fimc_hw_set_camera_polarity(fimc
, sensor
->pdata
);
52 fimc_hw_set_camera_type(fimc
, sensor
->pdata
);
53 fimc_hw_set_camera_source(fimc
, sensor
->pdata
);
54 fimc_hw_set_camera_offset(fimc
, &ctx
->s_frame
);
56 ret
= fimc_set_scaler_info(ctx
);
58 fimc_hw_set_input_path(ctx
);
59 fimc_hw_set_prescaler(ctx
);
60 fimc_hw_set_mainscaler(ctx
);
61 fimc_hw_set_target_format(ctx
);
62 fimc_hw_set_rotation(ctx
);
63 fimc_hw_set_effect(ctx
, false);
64 fimc_hw_set_output_path(ctx
);
65 fimc_hw_set_out_dma(ctx
);
66 if (fimc
->variant
->has_alpha
)
67 fimc_hw_set_rgb_alpha(ctx
);
68 clear_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
70 spin_unlock_irqrestore(&fimc
->slock
, flags
);
74 static int fimc_capture_state_cleanup(struct fimc_dev
*fimc
, bool suspend
)
76 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
77 struct fimc_vid_buffer
*buf
;
81 spin_lock_irqsave(&fimc
->slock
, flags
);
82 streaming
= fimc
->state
& (1 << ST_CAPT_ISP_STREAM
);
84 fimc
->state
&= ~(1 << ST_CAPT_RUN
| 1 << ST_CAPT_SHUT
|
85 1 << ST_CAPT_STREAM
| 1 << ST_CAPT_ISP_STREAM
);
87 fimc
->state
|= (1 << ST_CAPT_SUSPENDED
);
89 fimc
->state
&= ~(1 << ST_CAPT_PEND
| 1 << ST_CAPT_SUSPENDED
);
91 /* Release unused buffers */
92 while (!suspend
&& !list_empty(&cap
->pending_buf_q
)) {
93 buf
= fimc_pending_queue_pop(cap
);
94 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
96 /* If suspending put unused buffers onto pending queue */
97 while (!list_empty(&cap
->active_buf_q
)) {
98 buf
= fimc_active_queue_pop(cap
);
100 fimc_pending_queue_add(cap
, buf
);
102 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
108 spin_unlock_irqrestore(&fimc
->slock
, flags
);
111 return fimc_pipeline_s_stream(fimc
, 0);
116 static int fimc_stop_capture(struct fimc_dev
*fimc
, bool suspend
)
120 if (!fimc_capture_active(fimc
))
123 spin_lock_irqsave(&fimc
->slock
, flags
);
124 set_bit(ST_CAPT_SHUT
, &fimc
->state
);
125 fimc_deactivate_capture(fimc
);
126 spin_unlock_irqrestore(&fimc
->slock
, flags
);
128 wait_event_timeout(fimc
->irq_queue
,
129 !test_bit(ST_CAPT_SHUT
, &fimc
->state
),
130 (2*HZ
/10)); /* 200 ms */
132 return fimc_capture_state_cleanup(fimc
, suspend
);
136 * fimc_capture_config_update - apply the camera interface configuration
138 * To be called from within the interrupt handler with fimc.slock
139 * spinlock held. It updates the camera pixel crop, rotation and
142 int fimc_capture_config_update(struct fimc_ctx
*ctx
)
144 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
147 if (!test_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
))
150 spin_lock(&ctx
->slock
);
151 fimc_hw_set_camera_offset(fimc
, &ctx
->s_frame
);
152 ret
= fimc_set_scaler_info(ctx
);
154 fimc_hw_set_prescaler(ctx
);
155 fimc_hw_set_mainscaler(ctx
);
156 fimc_hw_set_target_format(ctx
);
157 fimc_hw_set_rotation(ctx
);
158 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
159 fimc_hw_set_out_dma(ctx
);
160 if (fimc
->variant
->has_alpha
)
161 fimc_hw_set_rgb_alpha(ctx
);
162 clear_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
164 spin_unlock(&ctx
->slock
);
168 static int start_streaming(struct vb2_queue
*q
, unsigned int count
)
170 struct fimc_ctx
*ctx
= q
->drv_priv
;
171 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
172 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
176 vid_cap
->frame_count
= 0;
178 ret
= fimc_init_capture(fimc
);
182 set_bit(ST_CAPT_PEND
, &fimc
->state
);
184 min_bufs
= fimc
->vid_cap
.reqbufs_count
> 1 ? 2 : 1;
186 if (vid_cap
->active_buf_cnt
>= min_bufs
&&
187 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
188 fimc_activate_capture(ctx
);
190 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
191 fimc_pipeline_s_stream(fimc
, 1);
196 fimc_capture_state_cleanup(fimc
, false);
200 static int stop_streaming(struct vb2_queue
*q
)
202 struct fimc_ctx
*ctx
= q
->drv_priv
;
203 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
205 if (!fimc_capture_active(fimc
))
208 return fimc_stop_capture(fimc
, false);
211 int fimc_capture_suspend(struct fimc_dev
*fimc
)
213 bool suspend
= fimc_capture_busy(fimc
);
215 int ret
= fimc_stop_capture(fimc
, suspend
);
218 return fimc_pipeline_shutdown(fimc
);
221 static void buffer_queue(struct vb2_buffer
*vb
);
223 int fimc_capture_resume(struct fimc_dev
*fimc
)
225 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
226 struct fimc_vid_buffer
*buf
;
229 if (!test_and_clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
))
232 INIT_LIST_HEAD(&fimc
->vid_cap
.active_buf_q
);
233 vid_cap
->buf_index
= 0;
234 fimc_pipeline_initialize(fimc
, &fimc
->vid_cap
.vfd
->entity
,
236 fimc_init_capture(fimc
);
238 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
240 for (i
= 0; i
< vid_cap
->reqbufs_count
; i
++) {
241 if (list_empty(&vid_cap
->pending_buf_q
))
243 buf
= fimc_pending_queue_pop(vid_cap
);
244 buffer_queue(&buf
->vb
);
250 static unsigned int get_plane_size(struct fimc_frame
*fr
, unsigned int plane
)
252 if (!fr
|| plane
>= fr
->fmt
->memplanes
)
254 return fr
->f_width
* fr
->f_height
* fr
->fmt
->depth
[plane
] / 8;
257 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*pfmt
,
258 unsigned int *num_buffers
, unsigned int *num_planes
,
259 unsigned int sizes
[], void *allocators
[])
261 struct fimc_ctx
*ctx
= vq
->drv_priv
;
262 struct fimc_fmt
*fmt
= ctx
->d_frame
.fmt
;
268 *num_planes
= fmt
->memplanes
;
270 for (i
= 0; i
< fmt
->memplanes
; i
++) {
271 sizes
[i
] = get_plane_size(&ctx
->d_frame
, i
);
272 allocators
[i
] = ctx
->fimc_dev
->alloc_ctx
;
278 static int buffer_prepare(struct vb2_buffer
*vb
)
280 struct vb2_queue
*vq
= vb
->vb2_queue
;
281 struct fimc_ctx
*ctx
= vq
->drv_priv
;
284 if (ctx
->d_frame
.fmt
== NULL
)
287 for (i
= 0; i
< ctx
->d_frame
.fmt
->memplanes
; i
++) {
288 unsigned long size
= ctx
->d_frame
.payload
[i
];
290 if (vb2_plane_size(vb
, i
) < size
) {
291 v4l2_err(ctx
->fimc_dev
->vid_cap
.vfd
,
292 "User buffer too small (%ld < %ld)\n",
293 vb2_plane_size(vb
, i
), size
);
296 vb2_set_plane_payload(vb
, i
, size
);
302 static void buffer_queue(struct vb2_buffer
*vb
)
304 struct fimc_vid_buffer
*buf
305 = container_of(vb
, struct fimc_vid_buffer
, vb
);
306 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
307 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
308 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
312 spin_lock_irqsave(&fimc
->slock
, flags
);
313 fimc_prepare_addr(ctx
, &buf
->vb
, &ctx
->d_frame
, &buf
->paddr
);
315 if (!test_bit(ST_CAPT_SUSPENDED
, &fimc
->state
) &&
316 !test_bit(ST_CAPT_STREAM
, &fimc
->state
) &&
317 vid_cap
->active_buf_cnt
< FIMC_MAX_OUT_BUFS
) {
318 /* Setup the buffer directly for processing. */
319 int buf_id
= (vid_cap
->reqbufs_count
== 1) ? -1 :
322 fimc_hw_set_output_addr(fimc
, &buf
->paddr
, buf_id
);
323 buf
->index
= vid_cap
->buf_index
;
324 fimc_active_queue_add(vid_cap
, buf
);
326 if (++vid_cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
327 vid_cap
->buf_index
= 0;
329 fimc_pending_queue_add(vid_cap
, buf
);
332 min_bufs
= vid_cap
->reqbufs_count
> 1 ? 2 : 1;
335 if (vb2_is_streaming(&vid_cap
->vbq
) &&
336 vid_cap
->active_buf_cnt
>= min_bufs
&&
337 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
338 fimc_activate_capture(ctx
);
339 spin_unlock_irqrestore(&fimc
->slock
, flags
);
341 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
342 fimc_pipeline_s_stream(fimc
, 1);
345 spin_unlock_irqrestore(&fimc
->slock
, flags
);
348 static void fimc_lock(struct vb2_queue
*vq
)
350 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
351 mutex_lock(&ctx
->fimc_dev
->lock
);
354 static void fimc_unlock(struct vb2_queue
*vq
)
356 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
357 mutex_unlock(&ctx
->fimc_dev
->lock
);
360 static struct vb2_ops fimc_capture_qops
= {
361 .queue_setup
= queue_setup
,
362 .buf_prepare
= buffer_prepare
,
363 .buf_queue
= buffer_queue
,
364 .wait_prepare
= fimc_unlock
,
365 .wait_finish
= fimc_lock
,
366 .start_streaming
= start_streaming
,
367 .stop_streaming
= stop_streaming
,
371 * fimc_capture_ctrls_create - initialize the control handler
372 * Initialize the capture video node control handler and fill it
373 * with the FIMC controls. Inherit any sensor's controls if the
374 * 'user_subdev_api' flag is false (default behaviour).
375 * This function need to be called with the graph mutex held.
377 int fimc_capture_ctrls_create(struct fimc_dev
*fimc
)
379 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
382 if (WARN_ON(vid_cap
->ctx
== NULL
))
384 if (vid_cap
->ctx
->ctrls_rdy
)
387 ret
= fimc_ctrls_create(vid_cap
->ctx
);
388 if (ret
|| vid_cap
->user_subdev_api
)
391 return v4l2_ctrl_add_handler(&vid_cap
->ctx
->ctrl_handler
,
392 fimc
->pipeline
.sensor
->ctrl_handler
);
395 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
);
397 static int fimc_capture_open(struct file
*file
)
399 struct fimc_dev
*fimc
= video_drvdata(file
);
400 int ret
= v4l2_fh_open(file
);
405 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
407 /* Return if the corresponding video mem2mem node is already opened. */
408 if (fimc_m2m_active(fimc
))
411 set_bit(ST_CAPT_BUSY
, &fimc
->state
);
412 pm_runtime_get_sync(&fimc
->pdev
->dev
);
414 if (++fimc
->vid_cap
.refcnt
== 1) {
415 ret
= fimc_pipeline_initialize(fimc
,
416 &fimc
->vid_cap
.vfd
->entity
, true);
418 dev_err(&fimc
->pdev
->dev
,
419 "Video pipeline initialization failed\n");
420 pm_runtime_put_sync(&fimc
->pdev
->dev
);
421 fimc
->vid_cap
.refcnt
--;
422 v4l2_fh_release(file
);
423 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
426 ret
= fimc_capture_ctrls_create(fimc
);
428 if (!ret
&& !fimc
->vid_cap
.user_subdev_api
)
429 ret
= fimc_capture_set_default_format(fimc
);
434 static int fimc_capture_close(struct file
*file
)
436 struct fimc_dev
*fimc
= video_drvdata(file
);
438 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
440 if (--fimc
->vid_cap
.refcnt
== 0) {
441 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
442 fimc_stop_capture(fimc
, false);
443 fimc_pipeline_shutdown(fimc
);
444 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
447 pm_runtime_put(&fimc
->pdev
->dev
);
449 if (fimc
->vid_cap
.refcnt
== 0) {
450 vb2_queue_release(&fimc
->vid_cap
.vbq
);
451 fimc_ctrls_delete(fimc
->vid_cap
.ctx
);
453 return v4l2_fh_release(file
);
456 static unsigned int fimc_capture_poll(struct file
*file
,
457 struct poll_table_struct
*wait
)
459 struct fimc_dev
*fimc
= video_drvdata(file
);
461 return vb2_poll(&fimc
->vid_cap
.vbq
, file
, wait
);
464 static int fimc_capture_mmap(struct file
*file
, struct vm_area_struct
*vma
)
466 struct fimc_dev
*fimc
= video_drvdata(file
);
468 return vb2_mmap(&fimc
->vid_cap
.vbq
, vma
);
471 static const struct v4l2_file_operations fimc_capture_fops
= {
472 .owner
= THIS_MODULE
,
473 .open
= fimc_capture_open
,
474 .release
= fimc_capture_close
,
475 .poll
= fimc_capture_poll
,
476 .unlocked_ioctl
= video_ioctl2
,
477 .mmap
= fimc_capture_mmap
,
481 * Format and crop negotiation helpers
484 static struct fimc_fmt
*fimc_capture_try_format(struct fimc_ctx
*ctx
,
485 u32
*width
, u32
*height
,
486 u32
*code
, u32
*fourcc
, int pad
)
488 bool rotation
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
489 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
490 struct samsung_fimc_variant
*var
= fimc
->variant
;
491 struct fimc_pix_limit
*pl
= var
->pix_limit
;
492 struct fimc_frame
*dst
= &ctx
->d_frame
;
493 u32 depth
, min_w
, max_w
, min_h
, align_h
= 3;
494 u32 mask
= FMT_FLAGS_CAM
;
495 struct fimc_fmt
*ffmt
;
497 /* Color conversion from/to JPEG is not supported */
498 if (code
&& ctx
->s_frame
.fmt
&& pad
== FIMC_SD_PAD_SOURCE
&&
499 fimc_fmt_is_jpeg(ctx
->s_frame
.fmt
->color
))
500 *code
= V4L2_MBUS_FMT_JPEG_1X8
;
502 if (fourcc
&& *fourcc
!= V4L2_PIX_FMT_JPEG
&& pad
!= FIMC_SD_PAD_SINK
)
503 mask
|= FMT_FLAGS_M2M
;
505 ffmt
= fimc_find_format(fourcc
, code
, mask
, 0);
509 *code
= ffmt
->mbus_code
;
511 *fourcc
= ffmt
->fourcc
;
513 if (pad
== FIMC_SD_PAD_SINK
) {
514 max_w
= fimc_fmt_is_jpeg(ffmt
->color
) ?
515 pl
->scaler_dis_w
: pl
->scaler_en_w
;
516 /* Apply the camera input interface pixel constraints */
517 v4l_bound_align_image(width
, max_t(u32
, *width
, 32), max_w
, 4,
518 height
, max_t(u32
, *height
, 32),
519 FIMC_CAMIF_MAX_HEIGHT
,
520 fimc_fmt_is_jpeg(ffmt
->color
) ? 3 : 1,
524 /* Can't scale or crop in transparent (JPEG) transfer mode */
525 if (fimc_fmt_is_jpeg(ffmt
->color
)) {
526 *width
= ctx
->s_frame
.f_width
;
527 *height
= ctx
->s_frame
.f_height
;
530 /* Apply the scaler and the output DMA constraints */
531 max_w
= rotation
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
;
532 min_w
= ctx
->state
& FIMC_DST_CROP
? dst
->width
: var
->min_out_pixsize
;
533 min_h
= ctx
->state
& FIMC_DST_CROP
? dst
->height
: var
->min_out_pixsize
;
534 if (var
->min_vsize_align
== 1 && !rotation
)
535 align_h
= fimc_fmt_is_rgb(ffmt
->color
) ? 0 : 1;
537 depth
= fimc_get_format_depth(ffmt
);
538 v4l_bound_align_image(width
, min_w
, max_w
,
539 ffs(var
->min_out_pixsize
) - 1,
540 height
, min_h
, FIMC_CAMIF_MAX_HEIGHT
,
542 64/(ALIGN(depth
, 8)));
544 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
545 pad
, code
? *code
: 0, *width
, *height
,
546 dst
->f_width
, dst
->f_height
);
551 static void fimc_capture_try_crop(struct fimc_ctx
*ctx
, struct v4l2_rect
*r
,
554 bool rotate
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
555 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
556 struct samsung_fimc_variant
*var
= fimc
->variant
;
557 struct fimc_pix_limit
*pl
= var
->pix_limit
;
558 struct fimc_frame
*sink
= &ctx
->s_frame
;
559 u32 max_w
, max_h
, min_w
= 0, min_h
= 0, min_sz
;
560 u32 align_sz
= 0, align_h
= 4;
561 u32 max_sc_h
, max_sc_v
;
563 /* In JPEG transparent transfer mode cropping is not supported */
564 if (fimc_fmt_is_jpeg(ctx
->d_frame
.fmt
->color
)) {
565 r
->width
= sink
->f_width
;
566 r
->height
= sink
->f_height
;
567 r
->left
= r
->top
= 0;
570 if (pad
== FIMC_SD_PAD_SOURCE
) {
571 if (ctx
->rotation
!= 90 && ctx
->rotation
!= 270)
573 max_sc_h
= min(SCALER_MAX_HRATIO
, 1 << (ffs(sink
->width
) - 3));
574 max_sc_v
= min(SCALER_MAX_VRATIO
, 1 << (ffs(sink
->height
) - 1));
575 min_sz
= var
->min_out_pixsize
;
577 u32 depth
= fimc_get_format_depth(sink
->fmt
);
578 align_sz
= 64/ALIGN(depth
, 8);
579 min_sz
= var
->min_inp_pixsize
;
580 min_w
= min_h
= min_sz
;
581 max_sc_h
= max_sc_v
= 1;
584 * For the crop rectangle at source pad the following constraints
586 * - it must fit in the sink pad format rectangle (f_width/f_height);
587 * - maximum downscaling ratio is 64;
588 * - maximum crop size depends if the rotator is used or not;
589 * - the sink pad format width/height must be 4 multiple of the
590 * prescaler ratios determined by sink pad size and source pad crop,
591 * the prescaler ratio is returned by fimc_get_scaler_factor().
594 rotate
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
,
595 rotate
? sink
->f_height
: sink
->f_width
);
596 max_h
= min_t(u32
, FIMC_CAMIF_MAX_HEIGHT
, sink
->f_height
);
597 if (pad
== FIMC_SD_PAD_SOURCE
) {
598 min_w
= min_t(u32
, max_w
, sink
->f_width
/ max_sc_h
);
599 min_h
= min_t(u32
, max_h
, sink
->f_height
/ max_sc_v
);
601 swap(max_sc_h
, max_sc_v
);
605 v4l_bound_align_image(&r
->width
, min_w
, max_w
, ffs(min_sz
) - 1,
606 &r
->height
, min_h
, max_h
, align_h
,
608 /* Adjust left/top if cropping rectangle is out of bounds */
609 r
->left
= clamp_t(u32
, r
->left
, 0, sink
->f_width
- r
->width
);
610 r
->top
= clamp_t(u32
, r
->top
, 0, sink
->f_height
- r
->height
);
611 r
->left
= round_down(r
->left
, var
->hor_offs_align
);
613 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
614 pad
, r
->left
, r
->top
, r
->width
, r
->height
,
615 sink
->f_width
, sink
->f_height
);
619 * The video node ioctl operations
621 static int fimc_vidioc_querycap_capture(struct file
*file
, void *priv
,
622 struct v4l2_capability
*cap
)
624 struct fimc_dev
*fimc
= video_drvdata(file
);
626 strncpy(cap
->driver
, fimc
->pdev
->name
, sizeof(cap
->driver
) - 1);
627 strncpy(cap
->card
, fimc
->pdev
->name
, sizeof(cap
->card
) - 1);
628 cap
->bus_info
[0] = 0;
629 cap
->capabilities
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_CAPTURE_MPLANE
;
634 static int fimc_cap_enum_fmt_mplane(struct file
*file
, void *priv
,
635 struct v4l2_fmtdesc
*f
)
637 struct fimc_fmt
*fmt
;
639 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
| FMT_FLAGS_M2M
,
643 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
644 f
->pixelformat
= fmt
->fourcc
;
645 if (fmt
->fourcc
== V4L2_MBUS_FMT_JPEG_1X8
)
646 f
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
651 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
653 * @ctx: FIMC capture context
654 * @tfmt: media bus format to try/set on subdevs
655 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
656 * @set: true to set format on subdevs, false to try only
658 static int fimc_pipeline_try_format(struct fimc_ctx
*ctx
,
659 struct v4l2_mbus_framefmt
*tfmt
,
660 struct fimc_fmt
**fmt_id
,
663 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
664 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
665 struct v4l2_subdev
*csis
= fimc
->pipeline
.csis
;
666 struct v4l2_subdev_format sfmt
;
667 struct v4l2_mbus_framefmt
*mf
= &sfmt
.format
;
668 struct fimc_fmt
*ffmt
= NULL
;
671 if (WARN_ON(!sd
|| !tfmt
))
674 memset(&sfmt
, 0, sizeof(sfmt
));
677 sfmt
.which
= set
? V4L2_SUBDEV_FORMAT_ACTIVE
: V4L2_SUBDEV_FORMAT_TRY
;
679 ffmt
= fimc_find_format(NULL
, mf
->code
!= 0 ? &mf
->code
: NULL
,
683 * Notify user-space if common pixel code for
684 * host and sensor does not exist.
688 mf
->code
= tfmt
->code
= ffmt
->mbus_code
;
690 ret
= v4l2_subdev_call(sd
, pad
, set_fmt
, NULL
, &sfmt
);
693 if (mf
->code
!= tfmt
->code
) {
697 if (mf
->width
!= tfmt
->width
|| mf
->height
!= tfmt
->height
) {
698 u32 fcc
= ffmt
->fourcc
;
699 tfmt
->width
= mf
->width
;
700 tfmt
->height
= mf
->height
;
701 ffmt
= fimc_capture_try_format(ctx
,
702 &tfmt
->width
, &tfmt
->height
,
703 NULL
, &fcc
, FIMC_SD_PAD_SOURCE
);
704 if (ffmt
&& ffmt
->mbus_code
)
705 mf
->code
= ffmt
->mbus_code
;
706 if (mf
->width
!= tfmt
->width
||
707 mf
->height
!= tfmt
->height
)
709 tfmt
->code
= mf
->code
;
712 ret
= v4l2_subdev_call(csis
, pad
, set_fmt
, NULL
, &sfmt
);
714 if (mf
->code
== tfmt
->code
&&
715 mf
->width
== tfmt
->width
&& mf
->height
== tfmt
->height
)
723 dbg("code: 0x%x, %dx%d, %p", mf
->code
, mf
->width
, mf
->height
, ffmt
);
727 static int fimc_cap_g_fmt_mplane(struct file
*file
, void *fh
,
728 struct v4l2_format
*f
)
730 struct fimc_dev
*fimc
= video_drvdata(file
);
731 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
733 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
736 return fimc_fill_format(&ctx
->d_frame
, f
);
739 static int fimc_cap_try_fmt_mplane(struct file
*file
, void *fh
,
740 struct v4l2_format
*f
)
742 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
743 struct fimc_dev
*fimc
= video_drvdata(file
);
744 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
745 struct v4l2_mbus_framefmt mf
;
746 struct fimc_fmt
*ffmt
= NULL
;
748 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
751 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
752 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
753 NULL
, &pix
->pixelformat
,
755 ctx
->s_frame
.f_width
= pix
->width
;
756 ctx
->s_frame
.f_height
= pix
->height
;
758 ffmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
759 NULL
, &pix
->pixelformat
,
764 if (!fimc
->vid_cap
.user_subdev_api
) {
765 mf
.width
= pix
->width
;
766 mf
.height
= pix
->height
;
767 mf
.code
= ffmt
->mbus_code
;
768 fimc_md_graph_lock(fimc
);
769 fimc_pipeline_try_format(ctx
, &mf
, &ffmt
, false);
770 fimc_md_graph_unlock(fimc
);
772 pix
->width
= mf
.width
;
773 pix
->height
= mf
.height
;
775 pix
->pixelformat
= ffmt
->fourcc
;
778 fimc_adjust_mplane_format(ffmt
, pix
->width
, pix
->height
, pix
);
782 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx
*ctx
, bool jpeg
)
784 ctx
->scaler
.enabled
= !jpeg
;
785 fimc_ctrls_activate(ctx
, !jpeg
);
788 set_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
790 clear_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
793 static int fimc_capture_set_format(struct fimc_dev
*fimc
, struct v4l2_format
*f
)
795 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
796 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
797 struct v4l2_mbus_framefmt
*mf
= &fimc
->vid_cap
.mf
;
798 struct fimc_frame
*ff
= &ctx
->d_frame
;
799 struct fimc_fmt
*s_fmt
= NULL
;
802 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
804 if (vb2_is_busy(&fimc
->vid_cap
.vbq
))
807 /* Pre-configure format at camera interface input, for JPEG only */
808 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
809 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
810 NULL
, &pix
->pixelformat
,
812 ctx
->s_frame
.f_width
= pix
->width
;
813 ctx
->s_frame
.f_height
= pix
->height
;
815 /* Try the format at the scaler and the DMA output */
816 ff
->fmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
817 NULL
, &pix
->pixelformat
,
822 /* Update RGB Alpha control state and value range */
823 fimc_alpha_ctrl_update(ctx
);
825 /* Try to match format at the host and the sensor */
826 if (!fimc
->vid_cap
.user_subdev_api
) {
827 mf
->code
= ff
->fmt
->mbus_code
;
828 mf
->width
= pix
->width
;
829 mf
->height
= pix
->height
;
831 fimc_md_graph_lock(fimc
);
832 ret
= fimc_pipeline_try_format(ctx
, mf
, &s_fmt
, true);
833 fimc_md_graph_unlock(fimc
);
836 pix
->width
= mf
->width
;
837 pix
->height
= mf
->height
;
839 fimc_adjust_mplane_format(ff
->fmt
, pix
->width
, pix
->height
, pix
);
840 for (i
= 0; i
< ff
->fmt
->colplanes
; i
++)
842 (pix
->width
* pix
->height
* ff
->fmt
->depth
[i
]) / 8;
844 set_frame_bounds(ff
, pix
->width
, pix
->height
);
845 /* Reset the composition rectangle if not yet configured */
846 if (!(ctx
->state
& FIMC_DST_CROP
))
847 set_frame_crop(ff
, 0, 0, pix
->width
, pix
->height
);
849 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ff
->fmt
->color
));
851 /* Reset cropping and set format at the camera interface input */
852 if (!fimc
->vid_cap
.user_subdev_api
) {
853 ctx
->s_frame
.fmt
= s_fmt
;
854 set_frame_bounds(&ctx
->s_frame
, pix
->width
, pix
->height
);
855 set_frame_crop(&ctx
->s_frame
, 0, 0, pix
->width
, pix
->height
);
861 static int fimc_cap_s_fmt_mplane(struct file
*file
, void *priv
,
862 struct v4l2_format
*f
)
864 struct fimc_dev
*fimc
= video_drvdata(file
);
866 return fimc_capture_set_format(fimc
, f
);
869 static int fimc_cap_enum_input(struct file
*file
, void *priv
,
870 struct v4l2_input
*i
)
872 struct fimc_dev
*fimc
= video_drvdata(file
);
873 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
878 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
880 strlcpy(i
->name
, sd
->name
, sizeof(i
->name
));
884 static int fimc_cap_s_input(struct file
*file
, void *priv
, unsigned int i
)
886 return i
== 0 ? i
: -EINVAL
;
889 static int fimc_cap_g_input(struct file
*file
, void *priv
, unsigned int *i
)
896 * fimc_pipeline_validate - check for formats inconsistencies
897 * between source and sink pad of each link
899 * Return 0 if all formats match or -EPIPE otherwise.
901 static int fimc_pipeline_validate(struct fimc_dev
*fimc
)
903 struct v4l2_subdev_format sink_fmt
, src_fmt
;
904 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
905 struct v4l2_subdev
*sd
;
906 struct media_pad
*pad
;
909 /* Start with the video capture node pad */
910 pad
= media_entity_remote_source(&vid_cap
->vd_pad
);
913 /* FIMC.{N} subdevice */
914 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
917 /* Retrieve format at the sink pad */
918 pad
= &sd
->entity
.pads
[0];
919 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
921 /* Don't call FIMC subdev operation to avoid nested locking */
922 if (sd
== fimc
->vid_cap
.subdev
) {
923 struct fimc_frame
*ff
= &vid_cap
->ctx
->s_frame
;
924 sink_fmt
.format
.width
= ff
->f_width
;
925 sink_fmt
.format
.height
= ff
->f_height
;
926 sink_fmt
.format
.code
= ff
->fmt
? ff
->fmt
->mbus_code
: 0;
928 sink_fmt
.pad
= pad
->index
;
929 sink_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
930 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &sink_fmt
);
931 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
934 /* Retrieve format at the source pad */
935 pad
= media_entity_remote_source(pad
);
937 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
940 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
941 src_fmt
.pad
= pad
->index
;
942 src_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
943 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &src_fmt
);
944 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
947 if (src_fmt
.format
.width
!= sink_fmt
.format
.width
||
948 src_fmt
.format
.height
!= sink_fmt
.format
.height
||
949 src_fmt
.format
.code
!= sink_fmt
.format
.code
)
955 static int fimc_cap_streamon(struct file
*file
, void *priv
,
956 enum v4l2_buf_type type
)
958 struct fimc_dev
*fimc
= video_drvdata(file
);
959 struct fimc_pipeline
*p
= &fimc
->pipeline
;
962 if (fimc_capture_active(fimc
))
965 media_entity_pipeline_start(&p
->sensor
->entity
, p
->pipe
);
967 if (fimc
->vid_cap
.user_subdev_api
) {
968 ret
= fimc_pipeline_validate(fimc
);
972 return vb2_streamon(&fimc
->vid_cap
.vbq
, type
);
975 static int fimc_cap_streamoff(struct file
*file
, void *priv
,
976 enum v4l2_buf_type type
)
978 struct fimc_dev
*fimc
= video_drvdata(file
);
979 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
982 ret
= vb2_streamoff(&fimc
->vid_cap
.vbq
, type
);
984 media_entity_pipeline_stop(&sd
->entity
);
988 static int fimc_cap_reqbufs(struct file
*file
, void *priv
,
989 struct v4l2_requestbuffers
*reqbufs
)
991 struct fimc_dev
*fimc
= video_drvdata(file
);
992 int ret
= vb2_reqbufs(&fimc
->vid_cap
.vbq
, reqbufs
);
995 fimc
->vid_cap
.reqbufs_count
= reqbufs
->count
;
999 static int fimc_cap_querybuf(struct file
*file
, void *priv
,
1000 struct v4l2_buffer
*buf
)
1002 struct fimc_dev
*fimc
= video_drvdata(file
);
1004 return vb2_querybuf(&fimc
->vid_cap
.vbq
, buf
);
1007 static int fimc_cap_qbuf(struct file
*file
, void *priv
,
1008 struct v4l2_buffer
*buf
)
1010 struct fimc_dev
*fimc
= video_drvdata(file
);
1012 return vb2_qbuf(&fimc
->vid_cap
.vbq
, buf
);
1015 static int fimc_cap_dqbuf(struct file
*file
, void *priv
,
1016 struct v4l2_buffer
*buf
)
1018 struct fimc_dev
*fimc
= video_drvdata(file
);
1020 return vb2_dqbuf(&fimc
->vid_cap
.vbq
, buf
, file
->f_flags
& O_NONBLOCK
);
1023 static int fimc_cap_create_bufs(struct file
*file
, void *priv
,
1024 struct v4l2_create_buffers
*create
)
1026 struct fimc_dev
*fimc
= video_drvdata(file
);
1028 return vb2_create_bufs(&fimc
->vid_cap
.vbq
, create
);
1031 static int fimc_cap_prepare_buf(struct file
*file
, void *priv
,
1032 struct v4l2_buffer
*b
)
1034 struct fimc_dev
*fimc
= video_drvdata(file
);
1036 return vb2_prepare_buf(&fimc
->vid_cap
.vbq
, b
);
1039 static int fimc_cap_g_selection(struct file
*file
, void *fh
,
1040 struct v4l2_selection
*s
)
1042 struct fimc_dev
*fimc
= video_drvdata(file
);
1043 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1044 struct fimc_frame
*f
= &ctx
->s_frame
;
1046 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1049 switch (s
->target
) {
1050 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
1051 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1053 case V4L2_SEL_TGT_CROP_BOUNDS
:
1054 case V4L2_SEL_TGT_CROP_DEFAULT
:
1057 s
->r
.width
= f
->o_width
;
1058 s
->r
.height
= f
->o_height
;
1061 case V4L2_SEL_TGT_COMPOSE_ACTIVE
:
1063 case V4L2_SEL_TGT_CROP_ACTIVE
:
1064 s
->r
.left
= f
->offs_h
;
1065 s
->r
.top
= f
->offs_v
;
1066 s
->r
.width
= f
->width
;
1067 s
->r
.height
= f
->height
;
1074 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1075 int enclosed_rectangle(struct v4l2_rect
*a
, struct v4l2_rect
*b
)
1077 if (a
->left
< b
->left
|| a
->top
< b
->top
)
1079 if (a
->left
+ a
->width
> b
->left
+ b
->width
)
1081 if (a
->top
+ a
->height
> b
->top
+ b
->height
)
1087 static int fimc_cap_s_selection(struct file
*file
, void *fh
,
1088 struct v4l2_selection
*s
)
1090 struct fimc_dev
*fimc
= video_drvdata(file
);
1091 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1092 struct v4l2_rect rect
= s
->r
;
1093 struct fimc_frame
*f
;
1094 unsigned long flags
;
1097 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1100 switch (s
->target
) {
1101 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
1102 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1103 case V4L2_SEL_TGT_COMPOSE_ACTIVE
:
1105 pad
= FIMC_SD_PAD_SOURCE
;
1107 case V4L2_SEL_TGT_CROP_BOUNDS
:
1108 case V4L2_SEL_TGT_CROP_DEFAULT
:
1109 case V4L2_SEL_TGT_CROP_ACTIVE
:
1111 pad
= FIMC_SD_PAD_SINK
;
1117 fimc_capture_try_crop(ctx
, &rect
, pad
);
1119 if (s
->flags
& V4L2_SEL_FLAG_LE
&&
1120 !enclosed_rectangle(&rect
, &s
->r
))
1123 if (s
->flags
& V4L2_SEL_FLAG_GE
&&
1124 !enclosed_rectangle(&s
->r
, &rect
))
1128 spin_lock_irqsave(&fimc
->slock
, flags
);
1129 set_frame_crop(f
, s
->r
.left
, s
->r
.top
, s
->r
.width
,
1131 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1133 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1137 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops
= {
1138 .vidioc_querycap
= fimc_vidioc_querycap_capture
,
1140 .vidioc_enum_fmt_vid_cap_mplane
= fimc_cap_enum_fmt_mplane
,
1141 .vidioc_try_fmt_vid_cap_mplane
= fimc_cap_try_fmt_mplane
,
1142 .vidioc_s_fmt_vid_cap_mplane
= fimc_cap_s_fmt_mplane
,
1143 .vidioc_g_fmt_vid_cap_mplane
= fimc_cap_g_fmt_mplane
,
1145 .vidioc_reqbufs
= fimc_cap_reqbufs
,
1146 .vidioc_querybuf
= fimc_cap_querybuf
,
1148 .vidioc_qbuf
= fimc_cap_qbuf
,
1149 .vidioc_dqbuf
= fimc_cap_dqbuf
,
1151 .vidioc_prepare_buf
= fimc_cap_prepare_buf
,
1152 .vidioc_create_bufs
= fimc_cap_create_bufs
,
1154 .vidioc_streamon
= fimc_cap_streamon
,
1155 .vidioc_streamoff
= fimc_cap_streamoff
,
1157 .vidioc_g_selection
= fimc_cap_g_selection
,
1158 .vidioc_s_selection
= fimc_cap_s_selection
,
1160 .vidioc_enum_input
= fimc_cap_enum_input
,
1161 .vidioc_s_input
= fimc_cap_s_input
,
1162 .vidioc_g_input
= fimc_cap_g_input
,
1165 /* Capture subdev media entity operations */
1166 static int fimc_link_setup(struct media_entity
*entity
,
1167 const struct media_pad
*local
,
1168 const struct media_pad
*remote
, u32 flags
)
1170 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
1171 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1173 if (media_entity_type(remote
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
1176 if (WARN_ON(fimc
== NULL
))
1179 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1180 local
->entity
->name
, remote
->entity
->name
, flags
,
1181 fimc
->vid_cap
.input
);
1183 if (flags
& MEDIA_LNK_FL_ENABLED
) {
1184 if (fimc
->vid_cap
.input
!= 0)
1186 fimc
->vid_cap
.input
= sd
->grp_id
;
1190 fimc
->vid_cap
.input
= 0;
1194 static const struct media_entity_operations fimc_sd_media_ops
= {
1195 .link_setup
= fimc_link_setup
,
1199 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1200 * @sd: pointer to a subdev generating the notification
1201 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1202 * @arg: pointer to an u32 type integer that stores the frame payload value
1204 * The End Of Frame notification sent by sensor subdev in its still capture
1205 * mode. If there is only a single VSYNC generated by the sensor at the
1206 * beginning of a frame transmission, FIMC does not issue the LastIrq
1207 * (end of frame) interrupt. And this notification is used to complete the
1208 * frame capture and returning a buffer to user-space. Subdev drivers should
1209 * call this notification from their last 'End of frame capture' interrupt.
1211 void fimc_sensor_notify(struct v4l2_subdev
*sd
, unsigned int notification
,
1214 struct fimc_sensor_info
*sensor
;
1215 struct fimc_vid_buffer
*buf
;
1216 struct fimc_md
*fmd
;
1217 struct fimc_dev
*fimc
;
1218 unsigned long flags
;
1223 sensor
= v4l2_get_subdev_hostdata(sd
);
1224 fmd
= entity_to_fimc_mdev(&sd
->entity
);
1226 spin_lock_irqsave(&fmd
->slock
, flags
);
1227 fimc
= sensor
? sensor
->host
: NULL
;
1229 if (fimc
&& arg
&& notification
== S5P_FIMC_TX_END_NOTIFY
&&
1230 test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
1231 unsigned long irq_flags
;
1232 spin_lock_irqsave(&fimc
->slock
, irq_flags
);
1233 if (!list_empty(&fimc
->vid_cap
.active_buf_q
)) {
1234 buf
= list_entry(fimc
->vid_cap
.active_buf_q
.next
,
1235 struct fimc_vid_buffer
, list
);
1236 vb2_set_plane_payload(&buf
->vb
, 0, *((u32
*)arg
));
1238 fimc_capture_irq_handler(fimc
, true);
1239 fimc_deactivate_capture(fimc
);
1240 spin_unlock_irqrestore(&fimc
->slock
, irq_flags
);
1242 spin_unlock_irqrestore(&fmd
->slock
, flags
);
1245 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev
*sd
,
1246 struct v4l2_subdev_fh
*fh
,
1247 struct v4l2_subdev_mbus_code_enum
*code
)
1249 struct fimc_fmt
*fmt
;
1251 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, code
->index
);
1254 code
->code
= fmt
->mbus_code
;
1258 static int fimc_subdev_get_fmt(struct v4l2_subdev
*sd
,
1259 struct v4l2_subdev_fh
*fh
,
1260 struct v4l2_subdev_format
*fmt
)
1262 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1263 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1264 struct v4l2_mbus_framefmt
*mf
;
1265 struct fimc_frame
*ff
;
1267 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1268 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1273 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1274 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
? &ctx
->s_frame
: &ctx
->d_frame
;
1276 mutex_lock(&fimc
->lock
);
1277 /* The pixel code is same on both input and output pad */
1278 if (!WARN_ON(ctx
->s_frame
.fmt
== NULL
))
1279 mf
->code
= ctx
->s_frame
.fmt
->mbus_code
;
1280 mf
->width
= ff
->f_width
;
1281 mf
->height
= ff
->f_height
;
1282 mutex_unlock(&fimc
->lock
);
1287 static int fimc_subdev_set_fmt(struct v4l2_subdev
*sd
,
1288 struct v4l2_subdev_fh
*fh
,
1289 struct v4l2_subdev_format
*fmt
)
1291 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1292 struct v4l2_mbus_framefmt
*mf
= &fmt
->format
;
1293 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1294 struct fimc_frame
*ff
;
1295 struct fimc_fmt
*ffmt
;
1297 dbg("pad%d: code: 0x%x, %dx%d",
1298 fmt
->pad
, mf
->code
, mf
->width
, mf
->height
);
1300 if (fmt
->pad
== FIMC_SD_PAD_SOURCE
&&
1301 vb2_is_busy(&fimc
->vid_cap
.vbq
))
1304 mutex_lock(&fimc
->lock
);
1305 ffmt
= fimc_capture_try_format(ctx
, &mf
->width
, &mf
->height
,
1306 &mf
->code
, NULL
, fmt
->pad
);
1307 mutex_unlock(&fimc
->lock
);
1308 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1310 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1311 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1315 /* Update RGB Alpha control state and value range */
1316 fimc_alpha_ctrl_update(ctx
);
1318 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ffmt
->color
));
1320 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
?
1321 &ctx
->s_frame
: &ctx
->d_frame
;
1323 mutex_lock(&fimc
->lock
);
1324 set_frame_bounds(ff
, mf
->width
, mf
->height
);
1325 fimc
->vid_cap
.mf
= *mf
;
1328 /* Reset the crop rectangle if required. */
1329 if (!(fmt
->pad
== FIMC_SD_PAD_SOURCE
&& (ctx
->state
& FIMC_DST_CROP
)))
1330 set_frame_crop(ff
, 0, 0, mf
->width
, mf
->height
);
1332 if (fmt
->pad
== FIMC_SD_PAD_SINK
)
1333 ctx
->state
&= ~FIMC_DST_CROP
;
1334 mutex_unlock(&fimc
->lock
);
1338 static int fimc_subdev_get_crop(struct v4l2_subdev
*sd
,
1339 struct v4l2_subdev_fh
*fh
,
1340 struct v4l2_subdev_crop
*crop
)
1342 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1343 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1344 struct v4l2_rect
*r
= &crop
->rect
;
1345 struct fimc_frame
*ff
;
1347 if (crop
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1348 crop
->rect
= *v4l2_subdev_get_try_crop(fh
, crop
->pad
);
1351 ff
= crop
->pad
== FIMC_SD_PAD_SINK
?
1352 &ctx
->s_frame
: &ctx
->d_frame
;
1354 mutex_lock(&fimc
->lock
);
1355 r
->left
= ff
->offs_h
;
1356 r
->top
= ff
->offs_v
;
1357 r
->width
= ff
->width
;
1358 r
->height
= ff
->height
;
1359 mutex_unlock(&fimc
->lock
);
1361 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1362 ff
, crop
->pad
, r
->left
, r
->top
, r
->width
, r
->height
,
1363 ff
->f_width
, ff
->f_height
);
1368 static int fimc_subdev_set_crop(struct v4l2_subdev
*sd
,
1369 struct v4l2_subdev_fh
*fh
,
1370 struct v4l2_subdev_crop
*crop
)
1372 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1373 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1374 struct v4l2_rect
*r
= &crop
->rect
;
1375 struct fimc_frame
*ff
;
1376 unsigned long flags
;
1378 dbg("(%d,%d)/%dx%d", r
->left
, r
->top
, r
->width
, r
->height
);
1380 ff
= crop
->pad
== FIMC_SD_PAD_SOURCE
?
1381 &ctx
->d_frame
: &ctx
->s_frame
;
1383 mutex_lock(&fimc
->lock
);
1384 fimc_capture_try_crop(ctx
, r
, crop
->pad
);
1386 if (crop
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1387 mutex_lock(&fimc
->lock
);
1388 *v4l2_subdev_get_try_crop(fh
, crop
->pad
) = *r
;
1391 spin_lock_irqsave(&fimc
->slock
, flags
);
1392 set_frame_crop(ff
, r
->left
, r
->top
, r
->width
, r
->height
);
1393 if (crop
->pad
== FIMC_SD_PAD_SOURCE
)
1394 ctx
->state
|= FIMC_DST_CROP
;
1396 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1397 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1399 dbg("pad%d: (%d,%d)/%dx%d", crop
->pad
, r
->left
, r
->top
,
1400 r
->width
, r
->height
);
1402 mutex_unlock(&fimc
->lock
);
1406 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops
= {
1407 .enum_mbus_code
= fimc_subdev_enum_mbus_code
,
1408 .get_fmt
= fimc_subdev_get_fmt
,
1409 .set_fmt
= fimc_subdev_set_fmt
,
1410 .get_crop
= fimc_subdev_get_crop
,
1411 .set_crop
= fimc_subdev_set_crop
,
1414 static struct v4l2_subdev_ops fimc_subdev_ops
= {
1415 .pad
= &fimc_subdev_pad_ops
,
1418 static int fimc_create_capture_subdev(struct fimc_dev
*fimc
,
1419 struct v4l2_device
*v4l2_dev
)
1421 struct v4l2_subdev
*sd
;
1424 sd
= kzalloc(sizeof(*sd
), GFP_KERNEL
);
1428 v4l2_subdev_init(sd
, &fimc_subdev_ops
);
1429 sd
->flags
= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1430 snprintf(sd
->name
, sizeof(sd
->name
), "FIMC.%d", fimc
->pdev
->id
);
1432 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SINK
].flags
= MEDIA_PAD_FL_SINK
;
1433 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SOURCE
].flags
= MEDIA_PAD_FL_SOURCE
;
1434 ret
= media_entity_init(&sd
->entity
, FIMC_SD_PADS_NUM
,
1435 fimc
->vid_cap
.sd_pads
, 0);
1438 ret
= v4l2_device_register_subdev(v4l2_dev
, sd
);
1442 fimc
->vid_cap
.subdev
= sd
;
1443 v4l2_set_subdevdata(sd
, fimc
);
1444 sd
->entity
.ops
= &fimc_sd_media_ops
;
1447 media_entity_cleanup(&sd
->entity
);
1453 static void fimc_destroy_capture_subdev(struct fimc_dev
*fimc
)
1455 struct v4l2_subdev
*sd
= fimc
->vid_cap
.subdev
;
1459 media_entity_cleanup(&sd
->entity
);
1460 v4l2_device_unregister_subdev(sd
);
1462 fimc
->vid_cap
.subdev
= NULL
;
1465 /* Set default format at the sensor and host interface */
1466 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
)
1468 struct v4l2_format fmt
= {
1469 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
,
1473 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1474 .field
= V4L2_FIELD_NONE
,
1475 .colorspace
= V4L2_COLORSPACE_JPEG
,
1479 return fimc_capture_set_format(fimc
, &fmt
);
1482 /* fimc->lock must be already initialized */
1483 int fimc_register_capture_device(struct fimc_dev
*fimc
,
1484 struct v4l2_device
*v4l2_dev
)
1486 struct video_device
*vfd
;
1487 struct fimc_vid_cap
*vid_cap
;
1488 struct fimc_ctx
*ctx
;
1489 struct vb2_queue
*q
;
1492 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
1496 ctx
->fimc_dev
= fimc
;
1497 ctx
->in_path
= FIMC_CAMERA
;
1498 ctx
->out_path
= FIMC_DMA
;
1499 ctx
->state
= FIMC_CTX_CAP
;
1500 ctx
->s_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1501 ctx
->d_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1503 vfd
= video_device_alloc();
1505 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
1509 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s.capture",
1510 dev_name(&fimc
->pdev
->dev
));
1512 vfd
->fops
= &fimc_capture_fops
;
1513 vfd
->ioctl_ops
= &fimc_capture_ioctl_ops
;
1514 vfd
->v4l2_dev
= v4l2_dev
;
1516 vfd
->release
= video_device_release
;
1517 vfd
->lock
= &fimc
->lock
;
1518 video_set_drvdata(vfd
, fimc
);
1520 vid_cap
= &fimc
->vid_cap
;
1522 vid_cap
->active_buf_cnt
= 0;
1523 vid_cap
->reqbufs_count
= 0;
1524 vid_cap
->refcnt
= 0;
1526 INIT_LIST_HEAD(&vid_cap
->pending_buf_q
);
1527 INIT_LIST_HEAD(&vid_cap
->active_buf_q
);
1528 spin_lock_init(&ctx
->slock
);
1531 q
= &fimc
->vid_cap
.vbq
;
1532 memset(q
, 0, sizeof(*q
));
1533 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1534 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1535 q
->drv_priv
= fimc
->vid_cap
.ctx
;
1536 q
->ops
= &fimc_capture_qops
;
1537 q
->mem_ops
= &vb2_dma_contig_memops
;
1538 q
->buf_struct_size
= sizeof(struct fimc_vid_buffer
);
1542 fimc
->vid_cap
.vd_pad
.flags
= MEDIA_PAD_FL_SINK
;
1543 ret
= media_entity_init(&vfd
->entity
, 1, &fimc
->vid_cap
.vd_pad
, 0);
1546 ret
= fimc_create_capture_subdev(fimc
, v4l2_dev
);
1550 vfd
->ctrl_handler
= &ctx
->ctrl_handler
;
1554 media_entity_cleanup(&vfd
->entity
);
1556 video_device_release(vfd
);
1562 void fimc_unregister_capture_device(struct fimc_dev
*fimc
)
1564 struct video_device
*vfd
= fimc
->vid_cap
.vfd
;
1567 media_entity_cleanup(&vfd
->entity
);
1568 /* Can also be called if video device was
1570 video_unregister_device(vfd
);
1572 fimc_destroy_capture_subdev(fimc
);
1573 kfree(fimc
->vid_cap
.ctx
);
1574 fimc
->vid_cap
.ctx
= NULL
;