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 clear_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
68 spin_unlock_irqrestore(&fimc
->slock
, flags
);
72 static int fimc_capture_state_cleanup(struct fimc_dev
*fimc
, bool suspend
)
74 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
75 struct fimc_vid_buffer
*buf
;
79 spin_lock_irqsave(&fimc
->slock
, flags
);
80 streaming
= fimc
->state
& (1 << ST_CAPT_ISP_STREAM
);
82 fimc
->state
&= ~(1 << ST_CAPT_RUN
| 1 << ST_CAPT_SHUT
|
83 1 << ST_CAPT_STREAM
| 1 << ST_CAPT_ISP_STREAM
);
85 fimc
->state
&= ~(1 << ST_CAPT_PEND
| 1 << ST_CAPT_SUSPENDED
);
87 /* Release unused buffers */
88 while (!suspend
&& !list_empty(&cap
->pending_buf_q
)) {
89 buf
= fimc_pending_queue_pop(cap
);
90 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
92 /* If suspending put unused buffers onto pending queue */
93 while (!list_empty(&cap
->active_buf_q
)) {
94 buf
= fimc_active_queue_pop(cap
);
96 fimc_pending_queue_add(cap
, buf
);
98 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
100 set_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
105 spin_unlock_irqrestore(&fimc
->slock
, flags
);
108 return fimc_pipeline_s_stream(fimc
, 0);
113 static int fimc_stop_capture(struct fimc_dev
*fimc
, bool suspend
)
117 if (!fimc_capture_active(fimc
))
120 spin_lock_irqsave(&fimc
->slock
, flags
);
121 set_bit(ST_CAPT_SHUT
, &fimc
->state
);
122 fimc_deactivate_capture(fimc
);
123 spin_unlock_irqrestore(&fimc
->slock
, flags
);
125 wait_event_timeout(fimc
->irq_queue
,
126 !test_bit(ST_CAPT_SHUT
, &fimc
->state
),
127 (2*HZ
/10)); /* 200 ms */
129 return fimc_capture_state_cleanup(fimc
, suspend
);
133 * fimc_capture_config_update - apply the camera interface configuration
135 * To be called from within the interrupt handler with fimc.slock
136 * spinlock held. It updates the camera pixel crop, rotation and
139 int fimc_capture_config_update(struct fimc_ctx
*ctx
)
141 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
144 if (!test_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
))
147 spin_lock(&ctx
->slock
);
148 fimc_hw_set_camera_offset(fimc
, &ctx
->s_frame
);
149 ret
= fimc_set_scaler_info(ctx
);
151 fimc_hw_set_prescaler(ctx
);
152 fimc_hw_set_mainscaler(ctx
);
153 fimc_hw_set_target_format(ctx
);
154 fimc_hw_set_rotation(ctx
);
155 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
156 fimc_hw_set_out_dma(ctx
);
157 clear_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
159 spin_unlock(&ctx
->slock
);
163 static int start_streaming(struct vb2_queue
*q
, unsigned int count
)
165 struct fimc_ctx
*ctx
= q
->drv_priv
;
166 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
167 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
171 vid_cap
->frame_count
= 0;
173 ret
= fimc_init_capture(fimc
);
177 set_bit(ST_CAPT_PEND
, &fimc
->state
);
179 min_bufs
= fimc
->vid_cap
.reqbufs_count
> 1 ? 2 : 1;
181 if (vid_cap
->active_buf_cnt
>= min_bufs
&&
182 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
183 fimc_activate_capture(ctx
);
185 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
186 fimc_pipeline_s_stream(fimc
, 1);
191 fimc_capture_state_cleanup(fimc
, false);
195 static int stop_streaming(struct vb2_queue
*q
)
197 struct fimc_ctx
*ctx
= q
->drv_priv
;
198 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
200 if (!fimc_capture_active(fimc
))
203 return fimc_stop_capture(fimc
, false);
206 int fimc_capture_suspend(struct fimc_dev
*fimc
)
208 bool suspend
= fimc_capture_busy(fimc
);
210 int ret
= fimc_stop_capture(fimc
, suspend
);
213 return fimc_pipeline_shutdown(fimc
);
216 static void buffer_queue(struct vb2_buffer
*vb
);
218 int fimc_capture_resume(struct fimc_dev
*fimc
)
220 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
221 struct fimc_vid_buffer
*buf
;
224 if (!test_and_clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
))
227 INIT_LIST_HEAD(&fimc
->vid_cap
.active_buf_q
);
228 vid_cap
->buf_index
= 0;
229 fimc_pipeline_initialize(fimc
, &fimc
->vid_cap
.vfd
->entity
,
231 fimc_init_capture(fimc
);
233 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
235 for (i
= 0; i
< vid_cap
->reqbufs_count
; i
++) {
236 if (list_empty(&vid_cap
->pending_buf_q
))
238 buf
= fimc_pending_queue_pop(vid_cap
);
239 buffer_queue(&buf
->vb
);
245 static unsigned int get_plane_size(struct fimc_frame
*fr
, unsigned int plane
)
247 if (!fr
|| plane
>= fr
->fmt
->memplanes
)
249 return fr
->f_width
* fr
->f_height
* fr
->fmt
->depth
[plane
] / 8;
252 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*pfmt
,
253 unsigned int *num_buffers
, unsigned int *num_planes
,
254 unsigned int sizes
[], void *allocators
[])
256 struct fimc_ctx
*ctx
= vq
->drv_priv
;
257 struct fimc_fmt
*fmt
= ctx
->d_frame
.fmt
;
263 *num_planes
= fmt
->memplanes
;
265 for (i
= 0; i
< fmt
->memplanes
; i
++) {
266 sizes
[i
] = get_plane_size(&ctx
->d_frame
, i
);
267 allocators
[i
] = ctx
->fimc_dev
->alloc_ctx
;
273 static int buffer_prepare(struct vb2_buffer
*vb
)
275 struct vb2_queue
*vq
= vb
->vb2_queue
;
276 struct fimc_ctx
*ctx
= vq
->drv_priv
;
279 if (ctx
->d_frame
.fmt
== NULL
)
282 for (i
= 0; i
< ctx
->d_frame
.fmt
->memplanes
; i
++) {
283 unsigned long size
= ctx
->d_frame
.payload
[i
];
285 if (vb2_plane_size(vb
, i
) < size
) {
286 v4l2_err(ctx
->fimc_dev
->vid_cap
.vfd
,
287 "User buffer too small (%ld < %ld)\n",
288 vb2_plane_size(vb
, i
), size
);
291 vb2_set_plane_payload(vb
, i
, size
);
297 static void buffer_queue(struct vb2_buffer
*vb
)
299 struct fimc_vid_buffer
*buf
300 = container_of(vb
, struct fimc_vid_buffer
, vb
);
301 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
302 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
303 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
307 spin_lock_irqsave(&fimc
->slock
, flags
);
308 fimc_prepare_addr(ctx
, &buf
->vb
, &ctx
->d_frame
, &buf
->paddr
);
310 if (!test_bit(ST_CAPT_SUSPENDED
, &fimc
->state
) &&
311 !test_bit(ST_CAPT_STREAM
, &fimc
->state
) &&
312 vid_cap
->active_buf_cnt
< FIMC_MAX_OUT_BUFS
) {
313 /* Setup the buffer directly for processing. */
314 int buf_id
= (vid_cap
->reqbufs_count
== 1) ? -1 :
317 fimc_hw_set_output_addr(fimc
, &buf
->paddr
, buf_id
);
318 buf
->index
= vid_cap
->buf_index
;
319 fimc_active_queue_add(vid_cap
, buf
);
321 if (++vid_cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
322 vid_cap
->buf_index
= 0;
324 fimc_pending_queue_add(vid_cap
, buf
);
327 min_bufs
= vid_cap
->reqbufs_count
> 1 ? 2 : 1;
330 if (vb2_is_streaming(&vid_cap
->vbq
) &&
331 vid_cap
->active_buf_cnt
>= min_bufs
&&
332 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
333 fimc_activate_capture(ctx
);
334 spin_unlock_irqrestore(&fimc
->slock
, flags
);
336 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
337 fimc_pipeline_s_stream(fimc
, 1);
340 spin_unlock_irqrestore(&fimc
->slock
, flags
);
343 static void fimc_lock(struct vb2_queue
*vq
)
345 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
346 mutex_lock(&ctx
->fimc_dev
->lock
);
349 static void fimc_unlock(struct vb2_queue
*vq
)
351 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
352 mutex_unlock(&ctx
->fimc_dev
->lock
);
355 static struct vb2_ops fimc_capture_qops
= {
356 .queue_setup
= queue_setup
,
357 .buf_prepare
= buffer_prepare
,
358 .buf_queue
= buffer_queue
,
359 .wait_prepare
= fimc_unlock
,
360 .wait_finish
= fimc_lock
,
361 .start_streaming
= start_streaming
,
362 .stop_streaming
= stop_streaming
,
366 * fimc_capture_ctrls_create - initialize the control handler
367 * Initialize the capture video node control handler and fill it
368 * with the FIMC controls. Inherit any sensor's controls if the
369 * 'user_subdev_api' flag is false (default behaviour).
370 * This function need to be called with the graph mutex held.
372 int fimc_capture_ctrls_create(struct fimc_dev
*fimc
)
374 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
377 if (WARN_ON(vid_cap
->ctx
== NULL
))
379 if (vid_cap
->ctx
->ctrls_rdy
)
382 ret
= fimc_ctrls_create(vid_cap
->ctx
);
383 if (ret
|| vid_cap
->user_subdev_api
)
386 return v4l2_ctrl_add_handler(&vid_cap
->ctx
->ctrl_handler
,
387 fimc
->pipeline
.sensor
->ctrl_handler
);
390 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
);
392 static int fimc_capture_open(struct file
*file
)
394 struct fimc_dev
*fimc
= video_drvdata(file
);
395 int ret
= v4l2_fh_open(file
);
400 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
402 /* Return if the corresponding video mem2mem node is already opened. */
403 if (fimc_m2m_active(fimc
))
406 set_bit(ST_CAPT_BUSY
, &fimc
->state
);
407 pm_runtime_get_sync(&fimc
->pdev
->dev
);
409 if (++fimc
->vid_cap
.refcnt
== 1) {
410 ret
= fimc_pipeline_initialize(fimc
,
411 &fimc
->vid_cap
.vfd
->entity
, true);
413 dev_err(&fimc
->pdev
->dev
,
414 "Video pipeline initialization failed\n");
415 pm_runtime_put_sync(&fimc
->pdev
->dev
);
416 fimc
->vid_cap
.refcnt
--;
417 v4l2_fh_release(file
);
418 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
421 ret
= fimc_capture_ctrls_create(fimc
);
423 if (!ret
&& !fimc
->vid_cap
.user_subdev_api
)
424 ret
= fimc_capture_set_default_format(fimc
);
429 static int fimc_capture_close(struct file
*file
)
431 struct fimc_dev
*fimc
= video_drvdata(file
);
433 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
435 if (--fimc
->vid_cap
.refcnt
== 0) {
436 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
437 fimc_stop_capture(fimc
, false);
438 fimc_pipeline_shutdown(fimc
);
439 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
442 pm_runtime_put(&fimc
->pdev
->dev
);
444 if (fimc
->vid_cap
.refcnt
== 0) {
445 vb2_queue_release(&fimc
->vid_cap
.vbq
);
446 fimc_ctrls_delete(fimc
->vid_cap
.ctx
);
448 return v4l2_fh_release(file
);
451 static unsigned int fimc_capture_poll(struct file
*file
,
452 struct poll_table_struct
*wait
)
454 struct fimc_dev
*fimc
= video_drvdata(file
);
456 return vb2_poll(&fimc
->vid_cap
.vbq
, file
, wait
);
459 static int fimc_capture_mmap(struct file
*file
, struct vm_area_struct
*vma
)
461 struct fimc_dev
*fimc
= video_drvdata(file
);
463 return vb2_mmap(&fimc
->vid_cap
.vbq
, vma
);
466 static const struct v4l2_file_operations fimc_capture_fops
= {
467 .owner
= THIS_MODULE
,
468 .open
= fimc_capture_open
,
469 .release
= fimc_capture_close
,
470 .poll
= fimc_capture_poll
,
471 .unlocked_ioctl
= video_ioctl2
,
472 .mmap
= fimc_capture_mmap
,
476 * Format and crop negotiation helpers
479 static struct fimc_fmt
*fimc_capture_try_format(struct fimc_ctx
*ctx
,
480 u32
*width
, u32
*height
,
481 u32
*code
, u32
*fourcc
, int pad
)
483 bool rotation
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
484 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
485 struct samsung_fimc_variant
*var
= fimc
->variant
;
486 struct fimc_pix_limit
*pl
= var
->pix_limit
;
487 struct fimc_frame
*dst
= &ctx
->d_frame
;
488 u32 depth
, min_w
, max_w
, min_h
, align_h
= 3;
489 u32 mask
= FMT_FLAGS_CAM
;
490 struct fimc_fmt
*ffmt
;
492 /* Color conversion from/to JPEG is not supported */
493 if (code
&& ctx
->s_frame
.fmt
&& pad
== FIMC_SD_PAD_SOURCE
&&
494 fimc_fmt_is_jpeg(ctx
->s_frame
.fmt
->color
))
495 *code
= V4L2_MBUS_FMT_JPEG_1X8
;
497 if (fourcc
&& *fourcc
!= V4L2_PIX_FMT_JPEG
&& pad
!= FIMC_SD_PAD_SINK
)
498 mask
|= FMT_FLAGS_M2M
;
500 ffmt
= fimc_find_format(fourcc
, code
, mask
, 0);
504 *code
= ffmt
->mbus_code
;
506 *fourcc
= ffmt
->fourcc
;
508 if (pad
== FIMC_SD_PAD_SINK
) {
509 max_w
= fimc_fmt_is_jpeg(ffmt
->color
) ?
510 pl
->scaler_dis_w
: pl
->scaler_en_w
;
511 /* Apply the camera input interface pixel constraints */
512 v4l_bound_align_image(width
, max_t(u32
, *width
, 32), max_w
, 4,
513 height
, max_t(u32
, *height
, 32),
514 FIMC_CAMIF_MAX_HEIGHT
,
515 fimc_fmt_is_jpeg(ffmt
->color
) ? 3 : 1,
519 /* Can't scale or crop in transparent (JPEG) transfer mode */
520 if (fimc_fmt_is_jpeg(ffmt
->color
)) {
521 *width
= ctx
->s_frame
.f_width
;
522 *height
= ctx
->s_frame
.f_height
;
525 /* Apply the scaler and the output DMA constraints */
526 max_w
= rotation
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
;
527 min_w
= ctx
->state
& FIMC_DST_CROP
? dst
->width
: var
->min_out_pixsize
;
528 min_h
= ctx
->state
& FIMC_DST_CROP
? dst
->height
: var
->min_out_pixsize
;
529 if (var
->min_vsize_align
== 1 && !rotation
)
530 align_h
= fimc_fmt_is_rgb(ffmt
->color
) ? 0 : 1;
532 depth
= fimc_get_format_depth(ffmt
);
533 v4l_bound_align_image(width
, min_w
, max_w
,
534 ffs(var
->min_out_pixsize
) - 1,
535 height
, min_h
, FIMC_CAMIF_MAX_HEIGHT
,
537 64/(ALIGN(depth
, 8)));
539 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
540 pad
, code
? *code
: 0, *width
, *height
,
541 dst
->f_width
, dst
->f_height
);
546 static void fimc_capture_try_crop(struct fimc_ctx
*ctx
, struct v4l2_rect
*r
,
549 bool rotate
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
550 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
551 struct samsung_fimc_variant
*var
= fimc
->variant
;
552 struct fimc_pix_limit
*pl
= var
->pix_limit
;
553 struct fimc_frame
*sink
= &ctx
->s_frame
;
554 u32 max_w
, max_h
, min_w
= 0, min_h
= 0, min_sz
;
555 u32 align_sz
= 0, align_h
= 4;
556 u32 max_sc_h
, max_sc_v
;
558 /* In JPEG transparent transfer mode cropping is not supported */
559 if (fimc_fmt_is_jpeg(ctx
->d_frame
.fmt
->color
)) {
560 r
->width
= sink
->f_width
;
561 r
->height
= sink
->f_height
;
562 r
->left
= r
->top
= 0;
565 if (pad
== FIMC_SD_PAD_SOURCE
) {
566 if (ctx
->rotation
!= 90 && ctx
->rotation
!= 270)
568 max_sc_h
= min(SCALER_MAX_HRATIO
, 1 << (ffs(sink
->width
) - 3));
569 max_sc_v
= min(SCALER_MAX_VRATIO
, 1 << (ffs(sink
->height
) - 1));
570 min_sz
= var
->min_out_pixsize
;
572 u32 depth
= fimc_get_format_depth(sink
->fmt
);
573 align_sz
= 64/ALIGN(depth
, 8);
574 min_sz
= var
->min_inp_pixsize
;
575 min_w
= min_h
= min_sz
;
576 max_sc_h
= max_sc_v
= 1;
579 * For the crop rectangle at source pad the following constraints
581 * - it must fit in the sink pad format rectangle (f_width/f_height);
582 * - maximum downscaling ratio is 64;
583 * - maximum crop size depends if the rotator is used or not;
584 * - the sink pad format width/height must be 4 multiple of the
585 * prescaler ratios determined by sink pad size and source pad crop,
586 * the prescaler ratio is returned by fimc_get_scaler_factor().
589 rotate
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
,
590 rotate
? sink
->f_height
: sink
->f_width
);
591 max_h
= min_t(u32
, FIMC_CAMIF_MAX_HEIGHT
, sink
->f_height
);
592 if (pad
== FIMC_SD_PAD_SOURCE
) {
593 min_w
= min_t(u32
, max_w
, sink
->f_width
/ max_sc_h
);
594 min_h
= min_t(u32
, max_h
, sink
->f_height
/ max_sc_v
);
596 swap(max_sc_h
, max_sc_v
);
600 v4l_bound_align_image(&r
->width
, min_w
, max_w
, ffs(min_sz
) - 1,
601 &r
->height
, min_h
, max_h
, align_h
,
603 /* Adjust left/top if cropping rectangle is out of bounds */
604 r
->left
= clamp_t(u32
, r
->left
, 0, sink
->f_width
- r
->width
);
605 r
->top
= clamp_t(u32
, r
->top
, 0, sink
->f_height
- r
->height
);
606 r
->left
= round_down(r
->left
, var
->hor_offs_align
);
608 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
609 pad
, r
->left
, r
->top
, r
->width
, r
->height
,
610 sink
->f_width
, sink
->f_height
);
614 * The video node ioctl operations
616 static int fimc_vidioc_querycap_capture(struct file
*file
, void *priv
,
617 struct v4l2_capability
*cap
)
619 struct fimc_dev
*fimc
= video_drvdata(file
);
621 strncpy(cap
->driver
, fimc
->pdev
->name
, sizeof(cap
->driver
) - 1);
622 strncpy(cap
->card
, fimc
->pdev
->name
, sizeof(cap
->card
) - 1);
623 cap
->bus_info
[0] = 0;
624 cap
->capabilities
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_CAPTURE_MPLANE
;
629 static int fimc_cap_enum_fmt_mplane(struct file
*file
, void *priv
,
630 struct v4l2_fmtdesc
*f
)
632 struct fimc_fmt
*fmt
;
634 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
| FMT_FLAGS_M2M
,
638 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
639 f
->pixelformat
= fmt
->fourcc
;
640 if (fmt
->fourcc
== V4L2_MBUS_FMT_JPEG_1X8
)
641 f
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
646 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
648 * @ctx: FIMC capture context
649 * @tfmt: media bus format to try/set on subdevs
650 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
651 * @set: true to set format on subdevs, false to try only
653 static int fimc_pipeline_try_format(struct fimc_ctx
*ctx
,
654 struct v4l2_mbus_framefmt
*tfmt
,
655 struct fimc_fmt
**fmt_id
,
658 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
659 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
660 struct v4l2_subdev
*csis
= fimc
->pipeline
.csis
;
661 struct v4l2_subdev_format sfmt
;
662 struct v4l2_mbus_framefmt
*mf
= &sfmt
.format
;
663 struct fimc_fmt
*ffmt
= NULL
;
666 if (WARN_ON(!sd
|| !tfmt
))
669 memset(&sfmt
, 0, sizeof(sfmt
));
672 sfmt
.which
= set
? V4L2_SUBDEV_FORMAT_ACTIVE
: V4L2_SUBDEV_FORMAT_TRY
;
674 ffmt
= fimc_find_format(NULL
, mf
->code
!= 0 ? &mf
->code
: NULL
,
678 * Notify user-space if common pixel code for
679 * host and sensor does not exist.
683 mf
->code
= tfmt
->code
= ffmt
->mbus_code
;
685 ret
= v4l2_subdev_call(sd
, pad
, set_fmt
, NULL
, &sfmt
);
688 if (mf
->code
!= tfmt
->code
) {
692 if (mf
->width
!= tfmt
->width
|| mf
->width
!= tfmt
->width
) {
693 u32 fcc
= ffmt
->fourcc
;
694 tfmt
->width
= mf
->width
;
695 tfmt
->height
= mf
->height
;
696 ffmt
= fimc_capture_try_format(ctx
,
697 &tfmt
->width
, &tfmt
->height
,
698 NULL
, &fcc
, FIMC_SD_PAD_SOURCE
);
699 if (ffmt
&& ffmt
->mbus_code
)
700 mf
->code
= ffmt
->mbus_code
;
701 if (mf
->width
!= tfmt
->width
|| mf
->width
!= tfmt
->width
)
703 tfmt
->code
= mf
->code
;
706 ret
= v4l2_subdev_call(csis
, pad
, set_fmt
, NULL
, &sfmt
);
708 if (mf
->code
== tfmt
->code
&&
709 mf
->width
== tfmt
->width
&& mf
->width
== tfmt
->width
)
717 dbg("code: 0x%x, %dx%d, %p", mf
->code
, mf
->width
, mf
->height
, ffmt
);
721 static int fimc_cap_g_fmt_mplane(struct file
*file
, void *fh
,
722 struct v4l2_format
*f
)
724 struct fimc_dev
*fimc
= video_drvdata(file
);
725 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
727 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
730 return fimc_fill_format(&ctx
->d_frame
, f
);
733 static int fimc_cap_try_fmt_mplane(struct file
*file
, void *fh
,
734 struct v4l2_format
*f
)
736 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
737 struct fimc_dev
*fimc
= video_drvdata(file
);
738 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
739 struct v4l2_mbus_framefmt mf
;
740 struct fimc_fmt
*ffmt
= NULL
;
742 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
745 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
746 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
747 NULL
, &pix
->pixelformat
,
749 ctx
->s_frame
.f_width
= pix
->width
;
750 ctx
->s_frame
.f_height
= pix
->height
;
752 ffmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
753 NULL
, &pix
->pixelformat
,
758 if (!fimc
->vid_cap
.user_subdev_api
) {
759 mf
.width
= pix
->width
;
760 mf
.height
= pix
->height
;
761 mf
.code
= ffmt
->mbus_code
;
762 fimc_md_graph_lock(fimc
);
763 fimc_pipeline_try_format(ctx
, &mf
, &ffmt
, false);
764 fimc_md_graph_unlock(fimc
);
766 pix
->width
= mf
.width
;
767 pix
->height
= mf
.height
;
769 pix
->pixelformat
= ffmt
->fourcc
;
772 fimc_adjust_mplane_format(ffmt
, pix
->width
, pix
->height
, pix
);
776 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx
*ctx
, bool jpeg
)
778 ctx
->scaler
.enabled
= !jpeg
;
779 fimc_ctrls_activate(ctx
, !jpeg
);
782 set_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
784 clear_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
787 static int fimc_capture_set_format(struct fimc_dev
*fimc
, struct v4l2_format
*f
)
789 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
790 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
791 struct v4l2_mbus_framefmt
*mf
= &fimc
->vid_cap
.mf
;
792 struct fimc_frame
*ff
= &ctx
->d_frame
;
793 struct fimc_fmt
*s_fmt
= NULL
;
796 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
798 if (vb2_is_busy(&fimc
->vid_cap
.vbq
))
801 /* Pre-configure format at camera interface input, for JPEG only */
802 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
803 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
804 NULL
, &pix
->pixelformat
,
806 ctx
->s_frame
.f_width
= pix
->width
;
807 ctx
->s_frame
.f_height
= pix
->height
;
809 /* Try the format at the scaler and the DMA output */
810 ff
->fmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
811 NULL
, &pix
->pixelformat
,
815 /* Try to match format at the host and the sensor */
816 if (!fimc
->vid_cap
.user_subdev_api
) {
817 mf
->code
= ff
->fmt
->mbus_code
;
818 mf
->width
= pix
->width
;
819 mf
->height
= pix
->height
;
821 fimc_md_graph_lock(fimc
);
822 ret
= fimc_pipeline_try_format(ctx
, mf
, &s_fmt
, true);
823 fimc_md_graph_unlock(fimc
);
826 pix
->width
= mf
->width
;
827 pix
->height
= mf
->height
;
829 fimc_adjust_mplane_format(ff
->fmt
, pix
->width
, pix
->height
, pix
);
830 for (i
= 0; i
< ff
->fmt
->colplanes
; i
++)
832 (pix
->width
* pix
->height
* ff
->fmt
->depth
[i
]) / 8;
834 set_frame_bounds(ff
, pix
->width
, pix
->height
);
835 /* Reset the composition rectangle if not yet configured */
836 if (!(ctx
->state
& FIMC_DST_CROP
))
837 set_frame_crop(ff
, 0, 0, pix
->width
, pix
->height
);
839 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ff
->fmt
->color
));
841 /* Reset cropping and set format at the camera interface input */
842 if (!fimc
->vid_cap
.user_subdev_api
) {
843 ctx
->s_frame
.fmt
= s_fmt
;
844 set_frame_bounds(&ctx
->s_frame
, pix
->width
, pix
->height
);
845 set_frame_crop(&ctx
->s_frame
, 0, 0, pix
->width
, pix
->height
);
851 static int fimc_cap_s_fmt_mplane(struct file
*file
, void *priv
,
852 struct v4l2_format
*f
)
854 struct fimc_dev
*fimc
= video_drvdata(file
);
856 return fimc_capture_set_format(fimc
, f
);
859 static int fimc_cap_enum_input(struct file
*file
, void *priv
,
860 struct v4l2_input
*i
)
862 struct fimc_dev
*fimc
= video_drvdata(file
);
863 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
868 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
870 strlcpy(i
->name
, sd
->name
, sizeof(i
->name
));
874 static int fimc_cap_s_input(struct file
*file
, void *priv
, unsigned int i
)
876 return i
== 0 ? i
: -EINVAL
;
879 static int fimc_cap_g_input(struct file
*file
, void *priv
, unsigned int *i
)
886 * fimc_pipeline_validate - check for formats inconsistencies
887 * between source and sink pad of each link
889 * Return 0 if all formats match or -EPIPE otherwise.
891 static int fimc_pipeline_validate(struct fimc_dev
*fimc
)
893 struct v4l2_subdev_format sink_fmt
, src_fmt
;
894 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
895 struct v4l2_subdev
*sd
;
896 struct media_pad
*pad
;
899 /* Start with the video capture node pad */
900 pad
= media_entity_remote_source(&vid_cap
->vd_pad
);
903 /* FIMC.{N} subdevice */
904 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
907 /* Retrieve format at the sink pad */
908 pad
= &sd
->entity
.pads
[0];
909 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
911 /* Don't call FIMC subdev operation to avoid nested locking */
912 if (sd
== fimc
->vid_cap
.subdev
) {
913 struct fimc_frame
*ff
= &vid_cap
->ctx
->s_frame
;
914 sink_fmt
.format
.width
= ff
->f_width
;
915 sink_fmt
.format
.height
= ff
->f_height
;
916 sink_fmt
.format
.code
= ff
->fmt
? ff
->fmt
->mbus_code
: 0;
918 sink_fmt
.pad
= pad
->index
;
919 sink_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
920 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &sink_fmt
);
921 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
924 /* Retrieve format at the source pad */
925 pad
= media_entity_remote_source(pad
);
927 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
930 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
931 src_fmt
.pad
= pad
->index
;
932 src_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
933 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &src_fmt
);
934 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
937 if (src_fmt
.format
.width
!= sink_fmt
.format
.width
||
938 src_fmt
.format
.height
!= sink_fmt
.format
.height
||
939 src_fmt
.format
.code
!= sink_fmt
.format
.code
)
945 static int fimc_cap_streamon(struct file
*file
, void *priv
,
946 enum v4l2_buf_type type
)
948 struct fimc_dev
*fimc
= video_drvdata(file
);
949 struct fimc_pipeline
*p
= &fimc
->pipeline
;
952 if (fimc_capture_active(fimc
))
955 media_entity_pipeline_start(&p
->sensor
->entity
, p
->pipe
);
957 if (fimc
->vid_cap
.user_subdev_api
) {
958 ret
= fimc_pipeline_validate(fimc
);
962 return vb2_streamon(&fimc
->vid_cap
.vbq
, type
);
965 static int fimc_cap_streamoff(struct file
*file
, void *priv
,
966 enum v4l2_buf_type type
)
968 struct fimc_dev
*fimc
= video_drvdata(file
);
969 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
972 ret
= vb2_streamoff(&fimc
->vid_cap
.vbq
, type
);
974 media_entity_pipeline_stop(&sd
->entity
);
978 static int fimc_cap_reqbufs(struct file
*file
, void *priv
,
979 struct v4l2_requestbuffers
*reqbufs
)
981 struct fimc_dev
*fimc
= video_drvdata(file
);
982 int ret
= vb2_reqbufs(&fimc
->vid_cap
.vbq
, reqbufs
);
985 fimc
->vid_cap
.reqbufs_count
= reqbufs
->count
;
989 static int fimc_cap_querybuf(struct file
*file
, void *priv
,
990 struct v4l2_buffer
*buf
)
992 struct fimc_dev
*fimc
= video_drvdata(file
);
994 return vb2_querybuf(&fimc
->vid_cap
.vbq
, buf
);
997 static int fimc_cap_qbuf(struct file
*file
, void *priv
,
998 struct v4l2_buffer
*buf
)
1000 struct fimc_dev
*fimc
= video_drvdata(file
);
1002 return vb2_qbuf(&fimc
->vid_cap
.vbq
, buf
);
1005 static int fimc_cap_dqbuf(struct file
*file
, void *priv
,
1006 struct v4l2_buffer
*buf
)
1008 struct fimc_dev
*fimc
= video_drvdata(file
);
1010 return vb2_dqbuf(&fimc
->vid_cap
.vbq
, buf
, file
->f_flags
& O_NONBLOCK
);
1013 static int fimc_cap_cropcap(struct file
*file
, void *fh
,
1014 struct v4l2_cropcap
*cr
)
1016 struct fimc_dev
*fimc
= video_drvdata(file
);
1017 struct fimc_frame
*f
= &fimc
->vid_cap
.ctx
->s_frame
;
1019 if (cr
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1022 cr
->bounds
.left
= 0;
1024 cr
->bounds
.width
= f
->o_width
;
1025 cr
->bounds
.height
= f
->o_height
;
1026 cr
->defrect
= cr
->bounds
;
1031 static int fimc_cap_g_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
1033 struct fimc_dev
*fimc
= video_drvdata(file
);
1034 struct fimc_frame
*f
= &fimc
->vid_cap
.ctx
->s_frame
;
1036 cr
->c
.left
= f
->offs_h
;
1037 cr
->c
.top
= f
->offs_v
;
1038 cr
->c
.width
= f
->width
;
1039 cr
->c
.height
= f
->height
;
1044 static int fimc_cap_s_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
1046 struct fimc_dev
*fimc
= video_drvdata(file
);
1047 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1048 struct fimc_frame
*ff
;
1049 unsigned long flags
;
1051 fimc_capture_try_crop(ctx
, &cr
->c
, FIMC_SD_PAD_SINK
);
1054 spin_lock_irqsave(&fimc
->slock
, flags
);
1055 set_frame_crop(ff
, cr
->c
.left
, cr
->c
.top
, cr
->c
.width
, cr
->c
.height
);
1056 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1057 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1062 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops
= {
1063 .vidioc_querycap
= fimc_vidioc_querycap_capture
,
1065 .vidioc_enum_fmt_vid_cap_mplane
= fimc_cap_enum_fmt_mplane
,
1066 .vidioc_try_fmt_vid_cap_mplane
= fimc_cap_try_fmt_mplane
,
1067 .vidioc_s_fmt_vid_cap_mplane
= fimc_cap_s_fmt_mplane
,
1068 .vidioc_g_fmt_vid_cap_mplane
= fimc_cap_g_fmt_mplane
,
1070 .vidioc_reqbufs
= fimc_cap_reqbufs
,
1071 .vidioc_querybuf
= fimc_cap_querybuf
,
1073 .vidioc_qbuf
= fimc_cap_qbuf
,
1074 .vidioc_dqbuf
= fimc_cap_dqbuf
,
1076 .vidioc_streamon
= fimc_cap_streamon
,
1077 .vidioc_streamoff
= fimc_cap_streamoff
,
1079 .vidioc_g_crop
= fimc_cap_g_crop
,
1080 .vidioc_s_crop
= fimc_cap_s_crop
,
1081 .vidioc_cropcap
= fimc_cap_cropcap
,
1083 .vidioc_enum_input
= fimc_cap_enum_input
,
1084 .vidioc_s_input
= fimc_cap_s_input
,
1085 .vidioc_g_input
= fimc_cap_g_input
,
1088 /* Capture subdev media entity operations */
1089 static int fimc_link_setup(struct media_entity
*entity
,
1090 const struct media_pad
*local
,
1091 const struct media_pad
*remote
, u32 flags
)
1093 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
1094 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1096 if (media_entity_type(remote
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
1099 if (WARN_ON(fimc
== NULL
))
1102 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1103 local
->entity
->name
, remote
->entity
->name
, flags
,
1104 fimc
->vid_cap
.input
);
1106 if (flags
& MEDIA_LNK_FL_ENABLED
) {
1107 if (fimc
->vid_cap
.input
!= 0)
1109 fimc
->vid_cap
.input
= sd
->grp_id
;
1113 fimc
->vid_cap
.input
= 0;
1117 static const struct media_entity_operations fimc_sd_media_ops
= {
1118 .link_setup
= fimc_link_setup
,
1122 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1123 * @sd: pointer to a subdev generating the notification
1124 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1125 * @arg: pointer to an u32 type integer that stores the frame payload value
1127 * The End Of Frame notification sent by sensor subdev in its still capture
1128 * mode. If there is only a single VSYNC generated by the sensor at the
1129 * beginning of a frame transmission, FIMC does not issue the LastIrq
1130 * (end of frame) interrupt. And this notification is used to complete the
1131 * frame capture and returning a buffer to user-space. Subdev drivers should
1132 * call this notification from their last 'End of frame capture' interrupt.
1134 void fimc_sensor_notify(struct v4l2_subdev
*sd
, unsigned int notification
,
1137 struct fimc_sensor_info
*sensor
;
1138 struct fimc_vid_buffer
*buf
;
1139 struct fimc_md
*fmd
;
1140 struct fimc_dev
*fimc
;
1141 unsigned long flags
;
1146 sensor
= v4l2_get_subdev_hostdata(sd
);
1147 fmd
= entity_to_fimc_mdev(&sd
->entity
);
1149 spin_lock_irqsave(&fmd
->slock
, flags
);
1150 fimc
= sensor
? sensor
->host
: NULL
;
1152 if (fimc
&& arg
&& notification
== S5P_FIMC_TX_END_NOTIFY
&&
1153 test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
1154 unsigned long irq_flags
;
1155 spin_lock_irqsave(&fimc
->slock
, irq_flags
);
1156 if (!list_empty(&fimc
->vid_cap
.active_buf_q
)) {
1157 buf
= list_entry(fimc
->vid_cap
.active_buf_q
.next
,
1158 struct fimc_vid_buffer
, list
);
1159 vb2_set_plane_payload(&buf
->vb
, 0, *((u32
*)arg
));
1161 fimc_capture_irq_handler(fimc
, true);
1162 fimc_deactivate_capture(fimc
);
1163 spin_unlock_irqrestore(&fimc
->slock
, irq_flags
);
1165 spin_unlock_irqrestore(&fmd
->slock
, flags
);
1168 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev
*sd
,
1169 struct v4l2_subdev_fh
*fh
,
1170 struct v4l2_subdev_mbus_code_enum
*code
)
1172 struct fimc_fmt
*fmt
;
1174 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, code
->index
);
1177 code
->code
= fmt
->mbus_code
;
1181 static int fimc_subdev_get_fmt(struct v4l2_subdev
*sd
,
1182 struct v4l2_subdev_fh
*fh
,
1183 struct v4l2_subdev_format
*fmt
)
1185 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1186 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1187 struct v4l2_mbus_framefmt
*mf
;
1188 struct fimc_frame
*ff
;
1190 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1191 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1196 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1197 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
? &ctx
->s_frame
: &ctx
->d_frame
;
1199 mutex_lock(&fimc
->lock
);
1200 /* The pixel code is same on both input and output pad */
1201 if (!WARN_ON(ctx
->s_frame
.fmt
== NULL
))
1202 mf
->code
= ctx
->s_frame
.fmt
->mbus_code
;
1203 mf
->width
= ff
->f_width
;
1204 mf
->height
= ff
->f_height
;
1205 mutex_unlock(&fimc
->lock
);
1210 static int fimc_subdev_set_fmt(struct v4l2_subdev
*sd
,
1211 struct v4l2_subdev_fh
*fh
,
1212 struct v4l2_subdev_format
*fmt
)
1214 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1215 struct v4l2_mbus_framefmt
*mf
= &fmt
->format
;
1216 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1217 struct fimc_frame
*ff
;
1218 struct fimc_fmt
*ffmt
;
1220 dbg("pad%d: code: 0x%x, %dx%d",
1221 fmt
->pad
, mf
->code
, mf
->width
, mf
->height
);
1223 if (fmt
->pad
== FIMC_SD_PAD_SOURCE
&&
1224 vb2_is_busy(&fimc
->vid_cap
.vbq
))
1227 mutex_lock(&fimc
->lock
);
1228 ffmt
= fimc_capture_try_format(ctx
, &mf
->width
, &mf
->height
,
1229 &mf
->code
, NULL
, fmt
->pad
);
1230 mutex_unlock(&fimc
->lock
);
1231 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1233 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1234 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1238 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ffmt
->color
));
1240 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
?
1241 &ctx
->s_frame
: &ctx
->d_frame
;
1243 mutex_lock(&fimc
->lock
);
1244 set_frame_bounds(ff
, mf
->width
, mf
->height
);
1247 /* Reset the crop rectangle if required. */
1248 if (!(fmt
->pad
== FIMC_SD_PAD_SOURCE
&& (ctx
->state
& FIMC_DST_CROP
)))
1249 set_frame_crop(ff
, 0, 0, mf
->width
, mf
->height
);
1251 if (fmt
->pad
== FIMC_SD_PAD_SINK
)
1252 ctx
->state
&= ~FIMC_DST_CROP
;
1253 mutex_unlock(&fimc
->lock
);
1257 static int fimc_subdev_get_crop(struct v4l2_subdev
*sd
,
1258 struct v4l2_subdev_fh
*fh
,
1259 struct v4l2_subdev_crop
*crop
)
1261 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1262 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1263 struct v4l2_rect
*r
= &crop
->rect
;
1264 struct fimc_frame
*ff
;
1266 if (crop
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1267 crop
->rect
= *v4l2_subdev_get_try_crop(fh
, crop
->pad
);
1270 ff
= crop
->pad
== FIMC_SD_PAD_SINK
?
1271 &ctx
->s_frame
: &ctx
->d_frame
;
1273 mutex_lock(&fimc
->lock
);
1274 r
->left
= ff
->offs_h
;
1275 r
->top
= ff
->offs_v
;
1276 r
->width
= ff
->width
;
1277 r
->height
= ff
->height
;
1278 mutex_unlock(&fimc
->lock
);
1280 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1281 ff
, crop
->pad
, r
->left
, r
->top
, r
->width
, r
->height
,
1282 ff
->f_width
, ff
->f_height
);
1287 static int fimc_subdev_set_crop(struct v4l2_subdev
*sd
,
1288 struct v4l2_subdev_fh
*fh
,
1289 struct v4l2_subdev_crop
*crop
)
1291 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1292 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1293 struct v4l2_rect
*r
= &crop
->rect
;
1294 struct fimc_frame
*ff
;
1295 unsigned long flags
;
1297 dbg("(%d,%d)/%dx%d", r
->left
, r
->top
, r
->width
, r
->height
);
1299 ff
= crop
->pad
== FIMC_SD_PAD_SOURCE
?
1300 &ctx
->d_frame
: &ctx
->s_frame
;
1302 mutex_lock(&fimc
->lock
);
1303 fimc_capture_try_crop(ctx
, r
, crop
->pad
);
1305 if (crop
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1306 mutex_lock(&fimc
->lock
);
1307 *v4l2_subdev_get_try_crop(fh
, crop
->pad
) = *r
;
1310 spin_lock_irqsave(&fimc
->slock
, flags
);
1311 set_frame_crop(ff
, r
->left
, r
->top
, r
->width
, r
->height
);
1312 if (crop
->pad
== FIMC_SD_PAD_SOURCE
)
1313 ctx
->state
|= FIMC_DST_CROP
;
1315 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1316 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1318 dbg("pad%d: (%d,%d)/%dx%d", crop
->pad
, r
->left
, r
->top
,
1319 r
->width
, r
->height
);
1321 mutex_unlock(&fimc
->lock
);
1325 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops
= {
1326 .enum_mbus_code
= fimc_subdev_enum_mbus_code
,
1327 .get_fmt
= fimc_subdev_get_fmt
,
1328 .set_fmt
= fimc_subdev_set_fmt
,
1329 .get_crop
= fimc_subdev_get_crop
,
1330 .set_crop
= fimc_subdev_set_crop
,
1333 static struct v4l2_subdev_ops fimc_subdev_ops
= {
1334 .pad
= &fimc_subdev_pad_ops
,
1337 static int fimc_create_capture_subdev(struct fimc_dev
*fimc
,
1338 struct v4l2_device
*v4l2_dev
)
1340 struct v4l2_subdev
*sd
;
1343 sd
= kzalloc(sizeof(*sd
), GFP_KERNEL
);
1347 v4l2_subdev_init(sd
, &fimc_subdev_ops
);
1348 sd
->flags
= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1349 snprintf(sd
->name
, sizeof(sd
->name
), "FIMC.%d", fimc
->pdev
->id
);
1351 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SINK
].flags
= MEDIA_PAD_FL_SINK
;
1352 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SOURCE
].flags
= MEDIA_PAD_FL_SOURCE
;
1353 ret
= media_entity_init(&sd
->entity
, FIMC_SD_PADS_NUM
,
1354 fimc
->vid_cap
.sd_pads
, 0);
1357 ret
= v4l2_device_register_subdev(v4l2_dev
, sd
);
1361 fimc
->vid_cap
.subdev
= sd
;
1362 v4l2_set_subdevdata(sd
, fimc
);
1363 sd
->entity
.ops
= &fimc_sd_media_ops
;
1366 media_entity_cleanup(&sd
->entity
);
1372 static void fimc_destroy_capture_subdev(struct fimc_dev
*fimc
)
1374 struct v4l2_subdev
*sd
= fimc
->vid_cap
.subdev
;
1378 media_entity_cleanup(&sd
->entity
);
1379 v4l2_device_unregister_subdev(sd
);
1381 fimc
->vid_cap
.subdev
= NULL
;
1384 /* Set default format at the sensor and host interface */
1385 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
)
1387 struct v4l2_format fmt
= {
1388 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
,
1392 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1393 .field
= V4L2_FIELD_NONE
,
1394 .colorspace
= V4L2_COLORSPACE_JPEG
,
1398 return fimc_capture_set_format(fimc
, &fmt
);
1401 /* fimc->lock must be already initialized */
1402 int fimc_register_capture_device(struct fimc_dev
*fimc
,
1403 struct v4l2_device
*v4l2_dev
)
1405 struct video_device
*vfd
;
1406 struct fimc_vid_cap
*vid_cap
;
1407 struct fimc_ctx
*ctx
;
1408 struct vb2_queue
*q
;
1411 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
1415 ctx
->fimc_dev
= fimc
;
1416 ctx
->in_path
= FIMC_CAMERA
;
1417 ctx
->out_path
= FIMC_DMA
;
1418 ctx
->state
= FIMC_CTX_CAP
;
1419 ctx
->s_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1420 ctx
->d_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1422 vfd
= video_device_alloc();
1424 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
1428 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s.capture",
1429 dev_name(&fimc
->pdev
->dev
));
1431 vfd
->fops
= &fimc_capture_fops
;
1432 vfd
->ioctl_ops
= &fimc_capture_ioctl_ops
;
1433 vfd
->v4l2_dev
= v4l2_dev
;
1435 vfd
->release
= video_device_release
;
1436 vfd
->lock
= &fimc
->lock
;
1437 video_set_drvdata(vfd
, fimc
);
1439 vid_cap
= &fimc
->vid_cap
;
1441 vid_cap
->active_buf_cnt
= 0;
1442 vid_cap
->reqbufs_count
= 0;
1443 vid_cap
->refcnt
= 0;
1445 INIT_LIST_HEAD(&vid_cap
->pending_buf_q
);
1446 INIT_LIST_HEAD(&vid_cap
->active_buf_q
);
1447 spin_lock_init(&ctx
->slock
);
1450 q
= &fimc
->vid_cap
.vbq
;
1451 memset(q
, 0, sizeof(*q
));
1452 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1453 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1454 q
->drv_priv
= fimc
->vid_cap
.ctx
;
1455 q
->ops
= &fimc_capture_qops
;
1456 q
->mem_ops
= &vb2_dma_contig_memops
;
1457 q
->buf_struct_size
= sizeof(struct fimc_vid_buffer
);
1461 fimc
->vid_cap
.vd_pad
.flags
= MEDIA_PAD_FL_SINK
;
1462 ret
= media_entity_init(&vfd
->entity
, 1, &fimc
->vid_cap
.vd_pad
, 0);
1465 ret
= fimc_create_capture_subdev(fimc
, v4l2_dev
);
1469 vfd
->ctrl_handler
= &ctx
->ctrl_handler
;
1473 media_entity_cleanup(&vfd
->entity
);
1475 video_device_release(vfd
);
1481 void fimc_unregister_capture_device(struct fimc_dev
*fimc
)
1483 struct video_device
*vfd
= fimc
->vid_cap
.vfd
;
1486 media_entity_cleanup(&vfd
->entity
);
1487 /* Can also be called if video device was
1489 video_unregister_device(vfd
);
1491 fimc_destroy_capture_subdev(fimc
);
1492 kfree(fimc
->vid_cap
.ctx
);
1493 fimc
->vid_cap
.ctx
= NULL
;