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 fimc_hw_set_camera_offset(fimc
, &ctx
->s_frame
);
152 ret
= fimc_set_scaler_info(ctx
);
156 fimc_hw_set_prescaler(ctx
);
157 fimc_hw_set_mainscaler(ctx
);
158 fimc_hw_set_target_format(ctx
);
159 fimc_hw_set_rotation(ctx
);
160 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
161 fimc_hw_set_out_dma(ctx
);
162 if (fimc
->variant
->has_alpha
)
163 fimc_hw_set_rgb_alpha(ctx
);
165 clear_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
169 static int start_streaming(struct vb2_queue
*q
, unsigned int count
)
171 struct fimc_ctx
*ctx
= q
->drv_priv
;
172 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
173 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
177 vid_cap
->frame_count
= 0;
179 ret
= fimc_init_capture(fimc
);
183 set_bit(ST_CAPT_PEND
, &fimc
->state
);
185 min_bufs
= fimc
->vid_cap
.reqbufs_count
> 1 ? 2 : 1;
187 if (vid_cap
->active_buf_cnt
>= min_bufs
&&
188 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
189 fimc_activate_capture(ctx
);
191 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
192 fimc_pipeline_s_stream(fimc
, 1);
197 fimc_capture_state_cleanup(fimc
, false);
201 static int stop_streaming(struct vb2_queue
*q
)
203 struct fimc_ctx
*ctx
= q
->drv_priv
;
204 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
206 if (!fimc_capture_active(fimc
))
209 return fimc_stop_capture(fimc
, false);
212 int fimc_capture_suspend(struct fimc_dev
*fimc
)
214 bool suspend
= fimc_capture_busy(fimc
);
216 int ret
= fimc_stop_capture(fimc
, suspend
);
219 return fimc_pipeline_shutdown(fimc
);
222 static void buffer_queue(struct vb2_buffer
*vb
);
224 int fimc_capture_resume(struct fimc_dev
*fimc
)
226 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
227 struct fimc_vid_buffer
*buf
;
230 if (!test_and_clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
))
233 INIT_LIST_HEAD(&fimc
->vid_cap
.active_buf_q
);
234 vid_cap
->buf_index
= 0;
235 fimc_pipeline_initialize(fimc
, &fimc
->vid_cap
.vfd
->entity
,
237 fimc_init_capture(fimc
);
239 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
241 for (i
= 0; i
< vid_cap
->reqbufs_count
; i
++) {
242 if (list_empty(&vid_cap
->pending_buf_q
))
244 buf
= fimc_pending_queue_pop(vid_cap
);
245 buffer_queue(&buf
->vb
);
251 static unsigned int get_plane_size(struct fimc_frame
*fr
, unsigned int plane
)
253 if (!fr
|| plane
>= fr
->fmt
->memplanes
)
255 return fr
->f_width
* fr
->f_height
* fr
->fmt
->depth
[plane
] / 8;
258 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*pfmt
,
259 unsigned int *num_buffers
, unsigned int *num_planes
,
260 unsigned int sizes
[], void *allocators
[])
262 struct fimc_ctx
*ctx
= vq
->drv_priv
;
263 struct fimc_fmt
*fmt
= ctx
->d_frame
.fmt
;
269 *num_planes
= fmt
->memplanes
;
271 for (i
= 0; i
< fmt
->memplanes
; i
++) {
272 sizes
[i
] = get_plane_size(&ctx
->d_frame
, i
);
273 allocators
[i
] = ctx
->fimc_dev
->alloc_ctx
;
279 static int buffer_prepare(struct vb2_buffer
*vb
)
281 struct vb2_queue
*vq
= vb
->vb2_queue
;
282 struct fimc_ctx
*ctx
= vq
->drv_priv
;
285 if (ctx
->d_frame
.fmt
== NULL
)
288 for (i
= 0; i
< ctx
->d_frame
.fmt
->memplanes
; i
++) {
289 unsigned long size
= ctx
->d_frame
.payload
[i
];
291 if (vb2_plane_size(vb
, i
) < size
) {
292 v4l2_err(ctx
->fimc_dev
->vid_cap
.vfd
,
293 "User buffer too small (%ld < %ld)\n",
294 vb2_plane_size(vb
, i
), size
);
297 vb2_set_plane_payload(vb
, i
, size
);
303 static void buffer_queue(struct vb2_buffer
*vb
)
305 struct fimc_vid_buffer
*buf
306 = container_of(vb
, struct fimc_vid_buffer
, vb
);
307 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
308 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
309 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
313 spin_lock_irqsave(&fimc
->slock
, flags
);
314 fimc_prepare_addr(ctx
, &buf
->vb
, &ctx
->d_frame
, &buf
->paddr
);
316 if (!test_bit(ST_CAPT_SUSPENDED
, &fimc
->state
) &&
317 !test_bit(ST_CAPT_STREAM
, &fimc
->state
) &&
318 vid_cap
->active_buf_cnt
< FIMC_MAX_OUT_BUFS
) {
319 /* Setup the buffer directly for processing. */
320 int buf_id
= (vid_cap
->reqbufs_count
== 1) ? -1 :
323 fimc_hw_set_output_addr(fimc
, &buf
->paddr
, buf_id
);
324 buf
->index
= vid_cap
->buf_index
;
325 fimc_active_queue_add(vid_cap
, buf
);
327 if (++vid_cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
328 vid_cap
->buf_index
= 0;
330 fimc_pending_queue_add(vid_cap
, buf
);
333 min_bufs
= vid_cap
->reqbufs_count
> 1 ? 2 : 1;
336 if (vb2_is_streaming(&vid_cap
->vbq
) &&
337 vid_cap
->active_buf_cnt
>= min_bufs
&&
338 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
339 fimc_activate_capture(ctx
);
340 spin_unlock_irqrestore(&fimc
->slock
, flags
);
342 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
343 fimc_pipeline_s_stream(fimc
, 1);
346 spin_unlock_irqrestore(&fimc
->slock
, flags
);
349 static void fimc_lock(struct vb2_queue
*vq
)
351 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
352 mutex_lock(&ctx
->fimc_dev
->lock
);
355 static void fimc_unlock(struct vb2_queue
*vq
)
357 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
358 mutex_unlock(&ctx
->fimc_dev
->lock
);
361 static struct vb2_ops fimc_capture_qops
= {
362 .queue_setup
= queue_setup
,
363 .buf_prepare
= buffer_prepare
,
364 .buf_queue
= buffer_queue
,
365 .wait_prepare
= fimc_unlock
,
366 .wait_finish
= fimc_lock
,
367 .start_streaming
= start_streaming
,
368 .stop_streaming
= stop_streaming
,
372 * fimc_capture_ctrls_create - initialize the control handler
373 * Initialize the capture video node control handler and fill it
374 * with the FIMC controls. Inherit any sensor's controls if the
375 * 'user_subdev_api' flag is false (default behaviour).
376 * This function need to be called with the graph mutex held.
378 int fimc_capture_ctrls_create(struct fimc_dev
*fimc
)
380 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
383 if (WARN_ON(vid_cap
->ctx
== NULL
))
385 if (vid_cap
->ctx
->ctrls_rdy
)
388 ret
= fimc_ctrls_create(vid_cap
->ctx
);
389 if (ret
|| vid_cap
->user_subdev_api
)
392 return v4l2_ctrl_add_handler(&vid_cap
->ctx
->ctrl_handler
,
393 fimc
->pipeline
.sensor
->ctrl_handler
);
396 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
);
398 static int fimc_capture_open(struct file
*file
)
400 struct fimc_dev
*fimc
= video_drvdata(file
);
401 int ret
= v4l2_fh_open(file
);
406 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
408 /* Return if the corresponding video mem2mem node is already opened. */
409 if (fimc_m2m_active(fimc
))
412 set_bit(ST_CAPT_BUSY
, &fimc
->state
);
413 pm_runtime_get_sync(&fimc
->pdev
->dev
);
415 if (++fimc
->vid_cap
.refcnt
== 1) {
416 ret
= fimc_pipeline_initialize(fimc
,
417 &fimc
->vid_cap
.vfd
->entity
, true);
419 dev_err(&fimc
->pdev
->dev
,
420 "Video pipeline initialization failed\n");
421 pm_runtime_put_sync(&fimc
->pdev
->dev
);
422 fimc
->vid_cap
.refcnt
--;
423 v4l2_fh_release(file
);
424 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
427 ret
= fimc_capture_ctrls_create(fimc
);
429 if (!ret
&& !fimc
->vid_cap
.user_subdev_api
)
430 ret
= fimc_capture_set_default_format(fimc
);
435 static int fimc_capture_close(struct file
*file
)
437 struct fimc_dev
*fimc
= video_drvdata(file
);
439 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
441 if (--fimc
->vid_cap
.refcnt
== 0) {
442 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
443 fimc_stop_capture(fimc
, false);
444 fimc_pipeline_shutdown(fimc
);
445 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
448 pm_runtime_put(&fimc
->pdev
->dev
);
450 if (fimc
->vid_cap
.refcnt
== 0) {
451 vb2_queue_release(&fimc
->vid_cap
.vbq
);
452 fimc_ctrls_delete(fimc
->vid_cap
.ctx
);
454 return v4l2_fh_release(file
);
457 static unsigned int fimc_capture_poll(struct file
*file
,
458 struct poll_table_struct
*wait
)
460 struct fimc_dev
*fimc
= video_drvdata(file
);
462 return vb2_poll(&fimc
->vid_cap
.vbq
, file
, wait
);
465 static int fimc_capture_mmap(struct file
*file
, struct vm_area_struct
*vma
)
467 struct fimc_dev
*fimc
= video_drvdata(file
);
469 return vb2_mmap(&fimc
->vid_cap
.vbq
, vma
);
472 static const struct v4l2_file_operations fimc_capture_fops
= {
473 .owner
= THIS_MODULE
,
474 .open
= fimc_capture_open
,
475 .release
= fimc_capture_close
,
476 .poll
= fimc_capture_poll
,
477 .unlocked_ioctl
= video_ioctl2
,
478 .mmap
= fimc_capture_mmap
,
482 * Format and crop negotiation helpers
485 static struct fimc_fmt
*fimc_capture_try_format(struct fimc_ctx
*ctx
,
486 u32
*width
, u32
*height
,
487 u32
*code
, u32
*fourcc
, int pad
)
489 bool rotation
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
490 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
491 struct samsung_fimc_variant
*var
= fimc
->variant
;
492 struct fimc_pix_limit
*pl
= var
->pix_limit
;
493 struct fimc_frame
*dst
= &ctx
->d_frame
;
494 u32 depth
, min_w
, max_w
, min_h
, align_h
= 3;
495 u32 mask
= FMT_FLAGS_CAM
;
496 struct fimc_fmt
*ffmt
;
498 /* Color conversion from/to JPEG is not supported */
499 if (code
&& ctx
->s_frame
.fmt
&& pad
== FIMC_SD_PAD_SOURCE
&&
500 fimc_fmt_is_jpeg(ctx
->s_frame
.fmt
->color
))
501 *code
= V4L2_MBUS_FMT_JPEG_1X8
;
503 if (fourcc
&& *fourcc
!= V4L2_PIX_FMT_JPEG
&& pad
!= FIMC_SD_PAD_SINK
)
504 mask
|= FMT_FLAGS_M2M
;
506 ffmt
= fimc_find_format(fourcc
, code
, mask
, 0);
510 *code
= ffmt
->mbus_code
;
512 *fourcc
= ffmt
->fourcc
;
514 if (pad
== FIMC_SD_PAD_SINK
) {
515 max_w
= fimc_fmt_is_jpeg(ffmt
->color
) ?
516 pl
->scaler_dis_w
: pl
->scaler_en_w
;
517 /* Apply the camera input interface pixel constraints */
518 v4l_bound_align_image(width
, max_t(u32
, *width
, 32), max_w
, 4,
519 height
, max_t(u32
, *height
, 32),
520 FIMC_CAMIF_MAX_HEIGHT
,
521 fimc_fmt_is_jpeg(ffmt
->color
) ? 3 : 1,
525 /* Can't scale or crop in transparent (JPEG) transfer mode */
526 if (fimc_fmt_is_jpeg(ffmt
->color
)) {
527 *width
= ctx
->s_frame
.f_width
;
528 *height
= ctx
->s_frame
.f_height
;
531 /* Apply the scaler and the output DMA constraints */
532 max_w
= rotation
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
;
533 min_w
= ctx
->state
& FIMC_DST_CROP
? dst
->width
: var
->min_out_pixsize
;
534 min_h
= ctx
->state
& FIMC_DST_CROP
? dst
->height
: var
->min_out_pixsize
;
535 if (var
->min_vsize_align
== 1 && !rotation
)
536 align_h
= fimc_fmt_is_rgb(ffmt
->color
) ? 0 : 1;
538 depth
= fimc_get_format_depth(ffmt
);
539 v4l_bound_align_image(width
, min_w
, max_w
,
540 ffs(var
->min_out_pixsize
) - 1,
541 height
, min_h
, FIMC_CAMIF_MAX_HEIGHT
,
543 64/(ALIGN(depth
, 8)));
545 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
546 pad
, code
? *code
: 0, *width
, *height
,
547 dst
->f_width
, dst
->f_height
);
552 static void fimc_capture_try_crop(struct fimc_ctx
*ctx
, struct v4l2_rect
*r
,
555 bool rotate
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
556 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
557 struct samsung_fimc_variant
*var
= fimc
->variant
;
558 struct fimc_pix_limit
*pl
= var
->pix_limit
;
559 struct fimc_frame
*sink
= &ctx
->s_frame
;
560 u32 max_w
, max_h
, min_w
= 0, min_h
= 0, min_sz
;
561 u32 align_sz
= 0, align_h
= 4;
562 u32 max_sc_h
, max_sc_v
;
564 /* In JPEG transparent transfer mode cropping is not supported */
565 if (fimc_fmt_is_jpeg(ctx
->d_frame
.fmt
->color
)) {
566 r
->width
= sink
->f_width
;
567 r
->height
= sink
->f_height
;
568 r
->left
= r
->top
= 0;
571 if (pad
== FIMC_SD_PAD_SOURCE
) {
572 if (ctx
->rotation
!= 90 && ctx
->rotation
!= 270)
574 max_sc_h
= min(SCALER_MAX_HRATIO
, 1 << (ffs(sink
->width
) - 3));
575 max_sc_v
= min(SCALER_MAX_VRATIO
, 1 << (ffs(sink
->height
) - 1));
576 min_sz
= var
->min_out_pixsize
;
578 u32 depth
= fimc_get_format_depth(sink
->fmt
);
579 align_sz
= 64/ALIGN(depth
, 8);
580 min_sz
= var
->min_inp_pixsize
;
581 min_w
= min_h
= min_sz
;
582 max_sc_h
= max_sc_v
= 1;
585 * For the crop rectangle at source pad the following constraints
587 * - it must fit in the sink pad format rectangle (f_width/f_height);
588 * - maximum downscaling ratio is 64;
589 * - maximum crop size depends if the rotator is used or not;
590 * - the sink pad format width/height must be 4 multiple of the
591 * prescaler ratios determined by sink pad size and source pad crop,
592 * the prescaler ratio is returned by fimc_get_scaler_factor().
595 rotate
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
,
596 rotate
? sink
->f_height
: sink
->f_width
);
597 max_h
= min_t(u32
, FIMC_CAMIF_MAX_HEIGHT
, sink
->f_height
);
598 if (pad
== FIMC_SD_PAD_SOURCE
) {
599 min_w
= min_t(u32
, max_w
, sink
->f_width
/ max_sc_h
);
600 min_h
= min_t(u32
, max_h
, sink
->f_height
/ max_sc_v
);
602 swap(max_sc_h
, max_sc_v
);
606 v4l_bound_align_image(&r
->width
, min_w
, max_w
, ffs(min_sz
) - 1,
607 &r
->height
, min_h
, max_h
, align_h
,
609 /* Adjust left/top if cropping rectangle is out of bounds */
610 r
->left
= clamp_t(u32
, r
->left
, 0, sink
->f_width
- r
->width
);
611 r
->top
= clamp_t(u32
, r
->top
, 0, sink
->f_height
- r
->height
);
612 r
->left
= round_down(r
->left
, var
->hor_offs_align
);
614 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
615 pad
, r
->left
, r
->top
, r
->width
, r
->height
,
616 sink
->f_width
, sink
->f_height
);
620 * The video node ioctl operations
622 static int fimc_vidioc_querycap_capture(struct file
*file
, void *priv
,
623 struct v4l2_capability
*cap
)
625 struct fimc_dev
*fimc
= video_drvdata(file
);
627 strncpy(cap
->driver
, fimc
->pdev
->name
, sizeof(cap
->driver
) - 1);
628 strncpy(cap
->card
, fimc
->pdev
->name
, sizeof(cap
->card
) - 1);
629 cap
->bus_info
[0] = 0;
630 cap
->capabilities
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_CAPTURE_MPLANE
;
635 static int fimc_cap_enum_fmt_mplane(struct file
*file
, void *priv
,
636 struct v4l2_fmtdesc
*f
)
638 struct fimc_fmt
*fmt
;
640 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
| FMT_FLAGS_M2M
,
644 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
645 f
->pixelformat
= fmt
->fourcc
;
646 if (fmt
->fourcc
== V4L2_MBUS_FMT_JPEG_1X8
)
647 f
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
652 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
654 * @ctx: FIMC capture context
655 * @tfmt: media bus format to try/set on subdevs
656 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
657 * @set: true to set format on subdevs, false to try only
659 static int fimc_pipeline_try_format(struct fimc_ctx
*ctx
,
660 struct v4l2_mbus_framefmt
*tfmt
,
661 struct fimc_fmt
**fmt_id
,
664 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
665 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
666 struct v4l2_subdev
*csis
= fimc
->pipeline
.csis
;
667 struct v4l2_subdev_format sfmt
;
668 struct v4l2_mbus_framefmt
*mf
= &sfmt
.format
;
669 struct fimc_fmt
*ffmt
= NULL
;
672 if (WARN_ON(!sd
|| !tfmt
))
675 memset(&sfmt
, 0, sizeof(sfmt
));
678 sfmt
.which
= set
? V4L2_SUBDEV_FORMAT_ACTIVE
: V4L2_SUBDEV_FORMAT_TRY
;
680 ffmt
= fimc_find_format(NULL
, mf
->code
!= 0 ? &mf
->code
: NULL
,
684 * Notify user-space if common pixel code for
685 * host and sensor does not exist.
689 mf
->code
= tfmt
->code
= ffmt
->mbus_code
;
691 ret
= v4l2_subdev_call(sd
, pad
, set_fmt
, NULL
, &sfmt
);
694 if (mf
->code
!= tfmt
->code
) {
698 if (mf
->width
!= tfmt
->width
|| mf
->height
!= tfmt
->height
) {
699 u32 fcc
= ffmt
->fourcc
;
700 tfmt
->width
= mf
->width
;
701 tfmt
->height
= mf
->height
;
702 ffmt
= fimc_capture_try_format(ctx
,
703 &tfmt
->width
, &tfmt
->height
,
704 NULL
, &fcc
, FIMC_SD_PAD_SOURCE
);
705 if (ffmt
&& ffmt
->mbus_code
)
706 mf
->code
= ffmt
->mbus_code
;
707 if (mf
->width
!= tfmt
->width
||
708 mf
->height
!= tfmt
->height
)
710 tfmt
->code
= mf
->code
;
713 ret
= v4l2_subdev_call(csis
, pad
, set_fmt
, NULL
, &sfmt
);
715 if (mf
->code
== tfmt
->code
&&
716 mf
->width
== tfmt
->width
&& mf
->height
== tfmt
->height
)
724 dbg("code: 0x%x, %dx%d, %p", mf
->code
, mf
->width
, mf
->height
, ffmt
);
728 static int fimc_cap_g_fmt_mplane(struct file
*file
, void *fh
,
729 struct v4l2_format
*f
)
731 struct fimc_dev
*fimc
= video_drvdata(file
);
732 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
734 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
737 return fimc_fill_format(&ctx
->d_frame
, f
);
740 static int fimc_cap_try_fmt_mplane(struct file
*file
, void *fh
,
741 struct v4l2_format
*f
)
743 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
744 struct fimc_dev
*fimc
= video_drvdata(file
);
745 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
746 struct v4l2_mbus_framefmt mf
;
747 struct fimc_fmt
*ffmt
= NULL
;
749 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
752 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
753 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
754 NULL
, &pix
->pixelformat
,
756 ctx
->s_frame
.f_width
= pix
->width
;
757 ctx
->s_frame
.f_height
= pix
->height
;
759 ffmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
760 NULL
, &pix
->pixelformat
,
765 if (!fimc
->vid_cap
.user_subdev_api
) {
766 mf
.width
= pix
->width
;
767 mf
.height
= pix
->height
;
768 mf
.code
= ffmt
->mbus_code
;
769 fimc_md_graph_lock(fimc
);
770 fimc_pipeline_try_format(ctx
, &mf
, &ffmt
, false);
771 fimc_md_graph_unlock(fimc
);
773 pix
->width
= mf
.width
;
774 pix
->height
= mf
.height
;
776 pix
->pixelformat
= ffmt
->fourcc
;
779 fimc_adjust_mplane_format(ffmt
, pix
->width
, pix
->height
, pix
);
783 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx
*ctx
, bool jpeg
)
785 ctx
->scaler
.enabled
= !jpeg
;
786 fimc_ctrls_activate(ctx
, !jpeg
);
789 set_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
791 clear_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
794 static int fimc_capture_set_format(struct fimc_dev
*fimc
, struct v4l2_format
*f
)
796 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
797 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
798 struct v4l2_mbus_framefmt
*mf
= &fimc
->vid_cap
.mf
;
799 struct fimc_frame
*ff
= &ctx
->d_frame
;
800 struct fimc_fmt
*s_fmt
= NULL
;
803 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
805 if (vb2_is_busy(&fimc
->vid_cap
.vbq
))
808 /* Pre-configure format at camera interface input, for JPEG only */
809 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
810 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
811 NULL
, &pix
->pixelformat
,
813 ctx
->s_frame
.f_width
= pix
->width
;
814 ctx
->s_frame
.f_height
= pix
->height
;
816 /* Try the format at the scaler and the DMA output */
817 ff
->fmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
818 NULL
, &pix
->pixelformat
,
823 /* Update RGB Alpha control state and value range */
824 fimc_alpha_ctrl_update(ctx
);
826 /* Try to match format at the host and the sensor */
827 if (!fimc
->vid_cap
.user_subdev_api
) {
828 mf
->code
= ff
->fmt
->mbus_code
;
829 mf
->width
= pix
->width
;
830 mf
->height
= pix
->height
;
832 fimc_md_graph_lock(fimc
);
833 ret
= fimc_pipeline_try_format(ctx
, mf
, &s_fmt
, true);
834 fimc_md_graph_unlock(fimc
);
837 pix
->width
= mf
->width
;
838 pix
->height
= mf
->height
;
840 fimc_adjust_mplane_format(ff
->fmt
, pix
->width
, pix
->height
, pix
);
841 for (i
= 0; i
< ff
->fmt
->colplanes
; i
++)
843 (pix
->width
* pix
->height
* ff
->fmt
->depth
[i
]) / 8;
845 set_frame_bounds(ff
, pix
->width
, pix
->height
);
846 /* Reset the composition rectangle if not yet configured */
847 if (!(ctx
->state
& FIMC_DST_CROP
))
848 set_frame_crop(ff
, 0, 0, pix
->width
, pix
->height
);
850 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ff
->fmt
->color
));
852 /* Reset cropping and set format at the camera interface input */
853 if (!fimc
->vid_cap
.user_subdev_api
) {
854 ctx
->s_frame
.fmt
= s_fmt
;
855 set_frame_bounds(&ctx
->s_frame
, pix
->width
, pix
->height
);
856 set_frame_crop(&ctx
->s_frame
, 0, 0, pix
->width
, pix
->height
);
862 static int fimc_cap_s_fmt_mplane(struct file
*file
, void *priv
,
863 struct v4l2_format
*f
)
865 struct fimc_dev
*fimc
= video_drvdata(file
);
867 return fimc_capture_set_format(fimc
, f
);
870 static int fimc_cap_enum_input(struct file
*file
, void *priv
,
871 struct v4l2_input
*i
)
873 struct fimc_dev
*fimc
= video_drvdata(file
);
874 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
879 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
881 strlcpy(i
->name
, sd
->name
, sizeof(i
->name
));
885 static int fimc_cap_s_input(struct file
*file
, void *priv
, unsigned int i
)
887 return i
== 0 ? i
: -EINVAL
;
890 static int fimc_cap_g_input(struct file
*file
, void *priv
, unsigned int *i
)
897 * fimc_pipeline_validate - check for formats inconsistencies
898 * between source and sink pad of each link
900 * Return 0 if all formats match or -EPIPE otherwise.
902 static int fimc_pipeline_validate(struct fimc_dev
*fimc
)
904 struct v4l2_subdev_format sink_fmt
, src_fmt
;
905 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
906 struct v4l2_subdev
*sd
;
907 struct media_pad
*pad
;
910 /* Start with the video capture node pad */
911 pad
= media_entity_remote_source(&vid_cap
->vd_pad
);
914 /* FIMC.{N} subdevice */
915 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
918 /* Retrieve format at the sink pad */
919 pad
= &sd
->entity
.pads
[0];
920 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
922 /* Don't call FIMC subdev operation to avoid nested locking */
923 if (sd
== fimc
->vid_cap
.subdev
) {
924 struct fimc_frame
*ff
= &vid_cap
->ctx
->s_frame
;
925 sink_fmt
.format
.width
= ff
->f_width
;
926 sink_fmt
.format
.height
= ff
->f_height
;
927 sink_fmt
.format
.code
= ff
->fmt
? ff
->fmt
->mbus_code
: 0;
929 sink_fmt
.pad
= pad
->index
;
930 sink_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
931 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &sink_fmt
);
932 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
935 /* Retrieve format at the source pad */
936 pad
= media_entity_remote_source(pad
);
938 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
941 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
942 src_fmt
.pad
= pad
->index
;
943 src_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
944 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &src_fmt
);
945 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
948 if (src_fmt
.format
.width
!= sink_fmt
.format
.width
||
949 src_fmt
.format
.height
!= sink_fmt
.format
.height
||
950 src_fmt
.format
.code
!= sink_fmt
.format
.code
)
956 static int fimc_cap_streamon(struct file
*file
, void *priv
,
957 enum v4l2_buf_type type
)
959 struct fimc_dev
*fimc
= video_drvdata(file
);
960 struct fimc_pipeline
*p
= &fimc
->pipeline
;
963 if (fimc_capture_active(fimc
))
966 media_entity_pipeline_start(&p
->sensor
->entity
, p
->pipe
);
968 if (fimc
->vid_cap
.user_subdev_api
) {
969 ret
= fimc_pipeline_validate(fimc
);
973 return vb2_streamon(&fimc
->vid_cap
.vbq
, type
);
976 static int fimc_cap_streamoff(struct file
*file
, void *priv
,
977 enum v4l2_buf_type type
)
979 struct fimc_dev
*fimc
= video_drvdata(file
);
980 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
983 ret
= vb2_streamoff(&fimc
->vid_cap
.vbq
, type
);
985 media_entity_pipeline_stop(&sd
->entity
);
989 static int fimc_cap_reqbufs(struct file
*file
, void *priv
,
990 struct v4l2_requestbuffers
*reqbufs
)
992 struct fimc_dev
*fimc
= video_drvdata(file
);
993 int ret
= vb2_reqbufs(&fimc
->vid_cap
.vbq
, reqbufs
);
996 fimc
->vid_cap
.reqbufs_count
= reqbufs
->count
;
1000 static int fimc_cap_querybuf(struct file
*file
, void *priv
,
1001 struct v4l2_buffer
*buf
)
1003 struct fimc_dev
*fimc
= video_drvdata(file
);
1005 return vb2_querybuf(&fimc
->vid_cap
.vbq
, buf
);
1008 static int fimc_cap_qbuf(struct file
*file
, void *priv
,
1009 struct v4l2_buffer
*buf
)
1011 struct fimc_dev
*fimc
= video_drvdata(file
);
1013 return vb2_qbuf(&fimc
->vid_cap
.vbq
, buf
);
1016 static int fimc_cap_dqbuf(struct file
*file
, void *priv
,
1017 struct v4l2_buffer
*buf
)
1019 struct fimc_dev
*fimc
= video_drvdata(file
);
1021 return vb2_dqbuf(&fimc
->vid_cap
.vbq
, buf
, file
->f_flags
& O_NONBLOCK
);
1024 static int fimc_cap_create_bufs(struct file
*file
, void *priv
,
1025 struct v4l2_create_buffers
*create
)
1027 struct fimc_dev
*fimc
= video_drvdata(file
);
1029 return vb2_create_bufs(&fimc
->vid_cap
.vbq
, create
);
1032 static int fimc_cap_prepare_buf(struct file
*file
, void *priv
,
1033 struct v4l2_buffer
*b
)
1035 struct fimc_dev
*fimc
= video_drvdata(file
);
1037 return vb2_prepare_buf(&fimc
->vid_cap
.vbq
, b
);
1040 static int fimc_cap_g_selection(struct file
*file
, void *fh
,
1041 struct v4l2_selection
*s
)
1043 struct fimc_dev
*fimc
= video_drvdata(file
);
1044 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1045 struct fimc_frame
*f
= &ctx
->s_frame
;
1047 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1050 switch (s
->target
) {
1051 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
1052 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1054 case V4L2_SEL_TGT_CROP_BOUNDS
:
1055 case V4L2_SEL_TGT_CROP_DEFAULT
:
1058 s
->r
.width
= f
->o_width
;
1059 s
->r
.height
= f
->o_height
;
1062 case V4L2_SEL_TGT_COMPOSE_ACTIVE
:
1064 case V4L2_SEL_TGT_CROP_ACTIVE
:
1065 s
->r
.left
= f
->offs_h
;
1066 s
->r
.top
= f
->offs_v
;
1067 s
->r
.width
= f
->width
;
1068 s
->r
.height
= f
->height
;
1075 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1076 int enclosed_rectangle(struct v4l2_rect
*a
, struct v4l2_rect
*b
)
1078 if (a
->left
< b
->left
|| a
->top
< b
->top
)
1080 if (a
->left
+ a
->width
> b
->left
+ b
->width
)
1082 if (a
->top
+ a
->height
> b
->top
+ b
->height
)
1088 static int fimc_cap_s_selection(struct file
*file
, void *fh
,
1089 struct v4l2_selection
*s
)
1091 struct fimc_dev
*fimc
= video_drvdata(file
);
1092 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1093 struct v4l2_rect rect
= s
->r
;
1094 struct fimc_frame
*f
;
1095 unsigned long flags
;
1098 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1101 switch (s
->target
) {
1102 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
1103 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1104 case V4L2_SEL_TGT_COMPOSE_ACTIVE
:
1106 pad
= FIMC_SD_PAD_SOURCE
;
1108 case V4L2_SEL_TGT_CROP_BOUNDS
:
1109 case V4L2_SEL_TGT_CROP_DEFAULT
:
1110 case V4L2_SEL_TGT_CROP_ACTIVE
:
1112 pad
= FIMC_SD_PAD_SINK
;
1118 fimc_capture_try_crop(ctx
, &rect
, pad
);
1120 if (s
->flags
& V4L2_SEL_FLAG_LE
&&
1121 !enclosed_rectangle(&rect
, &s
->r
))
1124 if (s
->flags
& V4L2_SEL_FLAG_GE
&&
1125 !enclosed_rectangle(&s
->r
, &rect
))
1129 spin_lock_irqsave(&fimc
->slock
, flags
);
1130 set_frame_crop(f
, s
->r
.left
, s
->r
.top
, s
->r
.width
,
1132 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1134 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1138 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops
= {
1139 .vidioc_querycap
= fimc_vidioc_querycap_capture
,
1141 .vidioc_enum_fmt_vid_cap_mplane
= fimc_cap_enum_fmt_mplane
,
1142 .vidioc_try_fmt_vid_cap_mplane
= fimc_cap_try_fmt_mplane
,
1143 .vidioc_s_fmt_vid_cap_mplane
= fimc_cap_s_fmt_mplane
,
1144 .vidioc_g_fmt_vid_cap_mplane
= fimc_cap_g_fmt_mplane
,
1146 .vidioc_reqbufs
= fimc_cap_reqbufs
,
1147 .vidioc_querybuf
= fimc_cap_querybuf
,
1149 .vidioc_qbuf
= fimc_cap_qbuf
,
1150 .vidioc_dqbuf
= fimc_cap_dqbuf
,
1152 .vidioc_prepare_buf
= fimc_cap_prepare_buf
,
1153 .vidioc_create_bufs
= fimc_cap_create_bufs
,
1155 .vidioc_streamon
= fimc_cap_streamon
,
1156 .vidioc_streamoff
= fimc_cap_streamoff
,
1158 .vidioc_g_selection
= fimc_cap_g_selection
,
1159 .vidioc_s_selection
= fimc_cap_s_selection
,
1161 .vidioc_enum_input
= fimc_cap_enum_input
,
1162 .vidioc_s_input
= fimc_cap_s_input
,
1163 .vidioc_g_input
= fimc_cap_g_input
,
1166 /* Capture subdev media entity operations */
1167 static int fimc_link_setup(struct media_entity
*entity
,
1168 const struct media_pad
*local
,
1169 const struct media_pad
*remote
, u32 flags
)
1171 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
1172 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1174 if (media_entity_type(remote
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
1177 if (WARN_ON(fimc
== NULL
))
1180 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1181 local
->entity
->name
, remote
->entity
->name
, flags
,
1182 fimc
->vid_cap
.input
);
1184 if (flags
& MEDIA_LNK_FL_ENABLED
) {
1185 if (fimc
->vid_cap
.input
!= 0)
1187 fimc
->vid_cap
.input
= sd
->grp_id
;
1191 fimc
->vid_cap
.input
= 0;
1195 static const struct media_entity_operations fimc_sd_media_ops
= {
1196 .link_setup
= fimc_link_setup
,
1200 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1201 * @sd: pointer to a subdev generating the notification
1202 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1203 * @arg: pointer to an u32 type integer that stores the frame payload value
1205 * The End Of Frame notification sent by sensor subdev in its still capture
1206 * mode. If there is only a single VSYNC generated by the sensor at the
1207 * beginning of a frame transmission, FIMC does not issue the LastIrq
1208 * (end of frame) interrupt. And this notification is used to complete the
1209 * frame capture and returning a buffer to user-space. Subdev drivers should
1210 * call this notification from their last 'End of frame capture' interrupt.
1212 void fimc_sensor_notify(struct v4l2_subdev
*sd
, unsigned int notification
,
1215 struct fimc_sensor_info
*sensor
;
1216 struct fimc_vid_buffer
*buf
;
1217 struct fimc_md
*fmd
;
1218 struct fimc_dev
*fimc
;
1219 unsigned long flags
;
1224 sensor
= v4l2_get_subdev_hostdata(sd
);
1225 fmd
= entity_to_fimc_mdev(&sd
->entity
);
1227 spin_lock_irqsave(&fmd
->slock
, flags
);
1228 fimc
= sensor
? sensor
->host
: NULL
;
1230 if (fimc
&& arg
&& notification
== S5P_FIMC_TX_END_NOTIFY
&&
1231 test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
1232 unsigned long irq_flags
;
1233 spin_lock_irqsave(&fimc
->slock
, irq_flags
);
1234 if (!list_empty(&fimc
->vid_cap
.active_buf_q
)) {
1235 buf
= list_entry(fimc
->vid_cap
.active_buf_q
.next
,
1236 struct fimc_vid_buffer
, list
);
1237 vb2_set_plane_payload(&buf
->vb
, 0, *((u32
*)arg
));
1239 fimc_capture_irq_handler(fimc
, true);
1240 fimc_deactivate_capture(fimc
);
1241 spin_unlock_irqrestore(&fimc
->slock
, irq_flags
);
1243 spin_unlock_irqrestore(&fmd
->slock
, flags
);
1246 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev
*sd
,
1247 struct v4l2_subdev_fh
*fh
,
1248 struct v4l2_subdev_mbus_code_enum
*code
)
1250 struct fimc_fmt
*fmt
;
1252 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, code
->index
);
1255 code
->code
= fmt
->mbus_code
;
1259 static int fimc_subdev_get_fmt(struct v4l2_subdev
*sd
,
1260 struct v4l2_subdev_fh
*fh
,
1261 struct v4l2_subdev_format
*fmt
)
1263 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1264 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1265 struct v4l2_mbus_framefmt
*mf
;
1266 struct fimc_frame
*ff
;
1268 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1269 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1274 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1275 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
? &ctx
->s_frame
: &ctx
->d_frame
;
1277 mutex_lock(&fimc
->lock
);
1278 /* The pixel code is same on both input and output pad */
1279 if (!WARN_ON(ctx
->s_frame
.fmt
== NULL
))
1280 mf
->code
= ctx
->s_frame
.fmt
->mbus_code
;
1281 mf
->width
= ff
->f_width
;
1282 mf
->height
= ff
->f_height
;
1283 mutex_unlock(&fimc
->lock
);
1288 static int fimc_subdev_set_fmt(struct v4l2_subdev
*sd
,
1289 struct v4l2_subdev_fh
*fh
,
1290 struct v4l2_subdev_format
*fmt
)
1292 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1293 struct v4l2_mbus_framefmt
*mf
= &fmt
->format
;
1294 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1295 struct fimc_frame
*ff
;
1296 struct fimc_fmt
*ffmt
;
1298 dbg("pad%d: code: 0x%x, %dx%d",
1299 fmt
->pad
, mf
->code
, mf
->width
, mf
->height
);
1301 if (fmt
->pad
== FIMC_SD_PAD_SOURCE
&&
1302 vb2_is_busy(&fimc
->vid_cap
.vbq
))
1305 mutex_lock(&fimc
->lock
);
1306 ffmt
= fimc_capture_try_format(ctx
, &mf
->width
, &mf
->height
,
1307 &mf
->code
, NULL
, fmt
->pad
);
1308 mutex_unlock(&fimc
->lock
);
1309 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1311 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1312 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1316 /* Update RGB Alpha control state and value range */
1317 fimc_alpha_ctrl_update(ctx
);
1319 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ffmt
->color
));
1321 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
?
1322 &ctx
->s_frame
: &ctx
->d_frame
;
1324 mutex_lock(&fimc
->lock
);
1325 set_frame_bounds(ff
, mf
->width
, mf
->height
);
1326 fimc
->vid_cap
.mf
= *mf
;
1329 /* Reset the crop rectangle if required. */
1330 if (!(fmt
->pad
== FIMC_SD_PAD_SOURCE
&& (ctx
->state
& FIMC_DST_CROP
)))
1331 set_frame_crop(ff
, 0, 0, mf
->width
, mf
->height
);
1333 if (fmt
->pad
== FIMC_SD_PAD_SINK
)
1334 ctx
->state
&= ~FIMC_DST_CROP
;
1335 mutex_unlock(&fimc
->lock
);
1339 static int fimc_subdev_get_crop(struct v4l2_subdev
*sd
,
1340 struct v4l2_subdev_fh
*fh
,
1341 struct v4l2_subdev_crop
*crop
)
1343 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1344 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1345 struct v4l2_rect
*r
= &crop
->rect
;
1346 struct fimc_frame
*ff
;
1348 if (crop
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1349 crop
->rect
= *v4l2_subdev_get_try_crop(fh
, crop
->pad
);
1352 ff
= crop
->pad
== FIMC_SD_PAD_SINK
?
1353 &ctx
->s_frame
: &ctx
->d_frame
;
1355 mutex_lock(&fimc
->lock
);
1356 r
->left
= ff
->offs_h
;
1357 r
->top
= ff
->offs_v
;
1358 r
->width
= ff
->width
;
1359 r
->height
= ff
->height
;
1360 mutex_unlock(&fimc
->lock
);
1362 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1363 ff
, crop
->pad
, r
->left
, r
->top
, r
->width
, r
->height
,
1364 ff
->f_width
, ff
->f_height
);
1369 static int fimc_subdev_set_crop(struct v4l2_subdev
*sd
,
1370 struct v4l2_subdev_fh
*fh
,
1371 struct v4l2_subdev_crop
*crop
)
1373 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1374 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1375 struct v4l2_rect
*r
= &crop
->rect
;
1376 struct fimc_frame
*ff
;
1377 unsigned long flags
;
1379 dbg("(%d,%d)/%dx%d", r
->left
, r
->top
, r
->width
, r
->height
);
1381 ff
= crop
->pad
== FIMC_SD_PAD_SOURCE
?
1382 &ctx
->d_frame
: &ctx
->s_frame
;
1384 mutex_lock(&fimc
->lock
);
1385 fimc_capture_try_crop(ctx
, r
, crop
->pad
);
1387 if (crop
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1388 mutex_lock(&fimc
->lock
);
1389 *v4l2_subdev_get_try_crop(fh
, crop
->pad
) = *r
;
1392 spin_lock_irqsave(&fimc
->slock
, flags
);
1393 set_frame_crop(ff
, r
->left
, r
->top
, r
->width
, r
->height
);
1394 if (crop
->pad
== FIMC_SD_PAD_SOURCE
)
1395 ctx
->state
|= FIMC_DST_CROP
;
1397 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1398 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1400 dbg("pad%d: (%d,%d)/%dx%d", crop
->pad
, r
->left
, r
->top
,
1401 r
->width
, r
->height
);
1403 mutex_unlock(&fimc
->lock
);
1407 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops
= {
1408 .enum_mbus_code
= fimc_subdev_enum_mbus_code
,
1409 .get_fmt
= fimc_subdev_get_fmt
,
1410 .set_fmt
= fimc_subdev_set_fmt
,
1411 .get_crop
= fimc_subdev_get_crop
,
1412 .set_crop
= fimc_subdev_set_crop
,
1415 static struct v4l2_subdev_ops fimc_subdev_ops
= {
1416 .pad
= &fimc_subdev_pad_ops
,
1419 static int fimc_create_capture_subdev(struct fimc_dev
*fimc
,
1420 struct v4l2_device
*v4l2_dev
)
1422 struct v4l2_subdev
*sd
;
1425 sd
= kzalloc(sizeof(*sd
), GFP_KERNEL
);
1429 v4l2_subdev_init(sd
, &fimc_subdev_ops
);
1430 sd
->flags
= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1431 snprintf(sd
->name
, sizeof(sd
->name
), "FIMC.%d", fimc
->pdev
->id
);
1433 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SINK
].flags
= MEDIA_PAD_FL_SINK
;
1434 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SOURCE
].flags
= MEDIA_PAD_FL_SOURCE
;
1435 ret
= media_entity_init(&sd
->entity
, FIMC_SD_PADS_NUM
,
1436 fimc
->vid_cap
.sd_pads
, 0);
1439 ret
= v4l2_device_register_subdev(v4l2_dev
, sd
);
1443 fimc
->vid_cap
.subdev
= sd
;
1444 v4l2_set_subdevdata(sd
, fimc
);
1445 sd
->entity
.ops
= &fimc_sd_media_ops
;
1448 media_entity_cleanup(&sd
->entity
);
1454 static void fimc_destroy_capture_subdev(struct fimc_dev
*fimc
)
1456 struct v4l2_subdev
*sd
= fimc
->vid_cap
.subdev
;
1460 media_entity_cleanup(&sd
->entity
);
1461 v4l2_device_unregister_subdev(sd
);
1463 fimc
->vid_cap
.subdev
= NULL
;
1466 /* Set default format at the sensor and host interface */
1467 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
)
1469 struct v4l2_format fmt
= {
1470 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
,
1474 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1475 .field
= V4L2_FIELD_NONE
,
1476 .colorspace
= V4L2_COLORSPACE_JPEG
,
1480 return fimc_capture_set_format(fimc
, &fmt
);
1483 /* fimc->lock must be already initialized */
1484 int fimc_register_capture_device(struct fimc_dev
*fimc
,
1485 struct v4l2_device
*v4l2_dev
)
1487 struct video_device
*vfd
;
1488 struct fimc_vid_cap
*vid_cap
;
1489 struct fimc_ctx
*ctx
;
1490 struct vb2_queue
*q
;
1493 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
1497 ctx
->fimc_dev
= fimc
;
1498 ctx
->in_path
= FIMC_CAMERA
;
1499 ctx
->out_path
= FIMC_DMA
;
1500 ctx
->state
= FIMC_CTX_CAP
;
1501 ctx
->s_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1502 ctx
->d_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1504 vfd
= video_device_alloc();
1506 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
1510 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s.capture",
1511 dev_name(&fimc
->pdev
->dev
));
1513 vfd
->fops
= &fimc_capture_fops
;
1514 vfd
->ioctl_ops
= &fimc_capture_ioctl_ops
;
1515 vfd
->v4l2_dev
= v4l2_dev
;
1517 vfd
->release
= video_device_release
;
1518 vfd
->lock
= &fimc
->lock
;
1519 video_set_drvdata(vfd
, fimc
);
1521 vid_cap
= &fimc
->vid_cap
;
1523 vid_cap
->active_buf_cnt
= 0;
1524 vid_cap
->reqbufs_count
= 0;
1525 vid_cap
->refcnt
= 0;
1527 INIT_LIST_HEAD(&vid_cap
->pending_buf_q
);
1528 INIT_LIST_HEAD(&vid_cap
->active_buf_q
);
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
;