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_PEND
| 1 << ST_CAPT_SUSPENDED
);
89 /* Release unused buffers */
90 while (!suspend
&& !list_empty(&cap
->pending_buf_q
)) {
91 buf
= fimc_pending_queue_pop(cap
);
92 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
94 /* If suspending put unused buffers onto pending queue */
95 while (!list_empty(&cap
->active_buf_q
)) {
96 buf
= fimc_active_queue_pop(cap
);
98 fimc_pending_queue_add(cap
, buf
);
100 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
102 set_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
107 spin_unlock_irqrestore(&fimc
->slock
, flags
);
110 return fimc_pipeline_s_stream(fimc
, 0);
115 static int fimc_stop_capture(struct fimc_dev
*fimc
, bool suspend
)
119 if (!fimc_capture_active(fimc
))
122 spin_lock_irqsave(&fimc
->slock
, flags
);
123 set_bit(ST_CAPT_SHUT
, &fimc
->state
);
124 fimc_deactivate_capture(fimc
);
125 spin_unlock_irqrestore(&fimc
->slock
, flags
);
127 wait_event_timeout(fimc
->irq_queue
,
128 !test_bit(ST_CAPT_SHUT
, &fimc
->state
),
129 (2*HZ
/10)); /* 200 ms */
131 return fimc_capture_state_cleanup(fimc
, suspend
);
135 * fimc_capture_config_update - apply the camera interface configuration
137 * To be called from within the interrupt handler with fimc.slock
138 * spinlock held. It updates the camera pixel crop, rotation and
141 int fimc_capture_config_update(struct fimc_ctx
*ctx
)
143 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
146 if (!test_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
))
149 spin_lock(&ctx
->slock
);
150 fimc_hw_set_camera_offset(fimc
, &ctx
->s_frame
);
151 ret
= fimc_set_scaler_info(ctx
);
153 fimc_hw_set_prescaler(ctx
);
154 fimc_hw_set_mainscaler(ctx
);
155 fimc_hw_set_target_format(ctx
);
156 fimc_hw_set_rotation(ctx
);
157 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
158 fimc_hw_set_out_dma(ctx
);
159 if (fimc
->variant
->has_alpha
)
160 fimc_hw_set_rgb_alpha(ctx
);
161 clear_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
163 spin_unlock(&ctx
->slock
);
167 static int start_streaming(struct vb2_queue
*q
, unsigned int count
)
169 struct fimc_ctx
*ctx
= q
->drv_priv
;
170 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
171 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
175 vid_cap
->frame_count
= 0;
177 ret
= fimc_init_capture(fimc
);
181 set_bit(ST_CAPT_PEND
, &fimc
->state
);
183 min_bufs
= fimc
->vid_cap
.reqbufs_count
> 1 ? 2 : 1;
185 if (vid_cap
->active_buf_cnt
>= min_bufs
&&
186 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
187 fimc_activate_capture(ctx
);
189 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
190 fimc_pipeline_s_stream(fimc
, 1);
195 fimc_capture_state_cleanup(fimc
, false);
199 static int stop_streaming(struct vb2_queue
*q
)
201 struct fimc_ctx
*ctx
= q
->drv_priv
;
202 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
204 if (!fimc_capture_active(fimc
))
207 return fimc_stop_capture(fimc
, false);
210 int fimc_capture_suspend(struct fimc_dev
*fimc
)
212 bool suspend
= fimc_capture_busy(fimc
);
214 int ret
= fimc_stop_capture(fimc
, suspend
);
217 return fimc_pipeline_shutdown(fimc
);
220 static void buffer_queue(struct vb2_buffer
*vb
);
222 int fimc_capture_resume(struct fimc_dev
*fimc
)
224 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
225 struct fimc_vid_buffer
*buf
;
228 if (!test_and_clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
))
231 INIT_LIST_HEAD(&fimc
->vid_cap
.active_buf_q
);
232 vid_cap
->buf_index
= 0;
233 fimc_pipeline_initialize(fimc
, &fimc
->vid_cap
.vfd
->entity
,
235 fimc_init_capture(fimc
);
237 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
239 for (i
= 0; i
< vid_cap
->reqbufs_count
; i
++) {
240 if (list_empty(&vid_cap
->pending_buf_q
))
242 buf
= fimc_pending_queue_pop(vid_cap
);
243 buffer_queue(&buf
->vb
);
249 static unsigned int get_plane_size(struct fimc_frame
*fr
, unsigned int plane
)
251 if (!fr
|| plane
>= fr
->fmt
->memplanes
)
253 return fr
->f_width
* fr
->f_height
* fr
->fmt
->depth
[plane
] / 8;
256 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*pfmt
,
257 unsigned int *num_buffers
, unsigned int *num_planes
,
258 unsigned int sizes
[], void *allocators
[])
260 struct fimc_ctx
*ctx
= vq
->drv_priv
;
261 struct fimc_fmt
*fmt
= ctx
->d_frame
.fmt
;
267 *num_planes
= fmt
->memplanes
;
269 for (i
= 0; i
< fmt
->memplanes
; i
++) {
270 sizes
[i
] = get_plane_size(&ctx
->d_frame
, i
);
271 allocators
[i
] = ctx
->fimc_dev
->alloc_ctx
;
277 static int buffer_prepare(struct vb2_buffer
*vb
)
279 struct vb2_queue
*vq
= vb
->vb2_queue
;
280 struct fimc_ctx
*ctx
= vq
->drv_priv
;
283 if (ctx
->d_frame
.fmt
== NULL
)
286 for (i
= 0; i
< ctx
->d_frame
.fmt
->memplanes
; i
++) {
287 unsigned long size
= ctx
->d_frame
.payload
[i
];
289 if (vb2_plane_size(vb
, i
) < size
) {
290 v4l2_err(ctx
->fimc_dev
->vid_cap
.vfd
,
291 "User buffer too small (%ld < %ld)\n",
292 vb2_plane_size(vb
, i
), size
);
295 vb2_set_plane_payload(vb
, i
, size
);
301 static void buffer_queue(struct vb2_buffer
*vb
)
303 struct fimc_vid_buffer
*buf
304 = container_of(vb
, struct fimc_vid_buffer
, vb
);
305 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
306 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
307 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
311 spin_lock_irqsave(&fimc
->slock
, flags
);
312 fimc_prepare_addr(ctx
, &buf
->vb
, &ctx
->d_frame
, &buf
->paddr
);
314 if (!test_bit(ST_CAPT_SUSPENDED
, &fimc
->state
) &&
315 !test_bit(ST_CAPT_STREAM
, &fimc
->state
) &&
316 vid_cap
->active_buf_cnt
< FIMC_MAX_OUT_BUFS
) {
317 /* Setup the buffer directly for processing. */
318 int buf_id
= (vid_cap
->reqbufs_count
== 1) ? -1 :
321 fimc_hw_set_output_addr(fimc
, &buf
->paddr
, buf_id
);
322 buf
->index
= vid_cap
->buf_index
;
323 fimc_active_queue_add(vid_cap
, buf
);
325 if (++vid_cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
326 vid_cap
->buf_index
= 0;
328 fimc_pending_queue_add(vid_cap
, buf
);
331 min_bufs
= vid_cap
->reqbufs_count
> 1 ? 2 : 1;
334 if (vb2_is_streaming(&vid_cap
->vbq
) &&
335 vid_cap
->active_buf_cnt
>= min_bufs
&&
336 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
337 fimc_activate_capture(ctx
);
338 spin_unlock_irqrestore(&fimc
->slock
, flags
);
340 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
341 fimc_pipeline_s_stream(fimc
, 1);
344 spin_unlock_irqrestore(&fimc
->slock
, flags
);
347 static void fimc_lock(struct vb2_queue
*vq
)
349 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
350 mutex_lock(&ctx
->fimc_dev
->lock
);
353 static void fimc_unlock(struct vb2_queue
*vq
)
355 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
356 mutex_unlock(&ctx
->fimc_dev
->lock
);
359 static struct vb2_ops fimc_capture_qops
= {
360 .queue_setup
= queue_setup
,
361 .buf_prepare
= buffer_prepare
,
362 .buf_queue
= buffer_queue
,
363 .wait_prepare
= fimc_unlock
,
364 .wait_finish
= fimc_lock
,
365 .start_streaming
= start_streaming
,
366 .stop_streaming
= stop_streaming
,
370 * fimc_capture_ctrls_create - initialize the control handler
371 * Initialize the capture video node control handler and fill it
372 * with the FIMC controls. Inherit any sensor's controls if the
373 * 'user_subdev_api' flag is false (default behaviour).
374 * This function need to be called with the graph mutex held.
376 int fimc_capture_ctrls_create(struct fimc_dev
*fimc
)
378 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
381 if (WARN_ON(vid_cap
->ctx
== NULL
))
383 if (vid_cap
->ctx
->ctrls_rdy
)
386 ret
= fimc_ctrls_create(vid_cap
->ctx
);
387 if (ret
|| vid_cap
->user_subdev_api
)
390 return v4l2_ctrl_add_handler(&vid_cap
->ctx
->ctrl_handler
,
391 fimc
->pipeline
.sensor
->ctrl_handler
);
394 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
);
396 static int fimc_capture_open(struct file
*file
)
398 struct fimc_dev
*fimc
= video_drvdata(file
);
399 int ret
= v4l2_fh_open(file
);
404 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
406 /* Return if the corresponding video mem2mem node is already opened. */
407 if (fimc_m2m_active(fimc
))
410 set_bit(ST_CAPT_BUSY
, &fimc
->state
);
411 pm_runtime_get_sync(&fimc
->pdev
->dev
);
413 if (++fimc
->vid_cap
.refcnt
== 1) {
414 ret
= fimc_pipeline_initialize(fimc
,
415 &fimc
->vid_cap
.vfd
->entity
, true);
417 dev_err(&fimc
->pdev
->dev
,
418 "Video pipeline initialization failed\n");
419 pm_runtime_put_sync(&fimc
->pdev
->dev
);
420 fimc
->vid_cap
.refcnt
--;
421 v4l2_fh_release(file
);
422 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
425 ret
= fimc_capture_ctrls_create(fimc
);
427 if (!ret
&& !fimc
->vid_cap
.user_subdev_api
)
428 ret
= fimc_capture_set_default_format(fimc
);
433 static int fimc_capture_close(struct file
*file
)
435 struct fimc_dev
*fimc
= video_drvdata(file
);
437 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
439 if (--fimc
->vid_cap
.refcnt
== 0) {
440 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
441 fimc_stop_capture(fimc
, false);
442 fimc_pipeline_shutdown(fimc
);
443 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
446 pm_runtime_put(&fimc
->pdev
->dev
);
448 if (fimc
->vid_cap
.refcnt
== 0) {
449 vb2_queue_release(&fimc
->vid_cap
.vbq
);
450 fimc_ctrls_delete(fimc
->vid_cap
.ctx
);
452 return v4l2_fh_release(file
);
455 static unsigned int fimc_capture_poll(struct file
*file
,
456 struct poll_table_struct
*wait
)
458 struct fimc_dev
*fimc
= video_drvdata(file
);
460 return vb2_poll(&fimc
->vid_cap
.vbq
, file
, wait
);
463 static int fimc_capture_mmap(struct file
*file
, struct vm_area_struct
*vma
)
465 struct fimc_dev
*fimc
= video_drvdata(file
);
467 return vb2_mmap(&fimc
->vid_cap
.vbq
, vma
);
470 static const struct v4l2_file_operations fimc_capture_fops
= {
471 .owner
= THIS_MODULE
,
472 .open
= fimc_capture_open
,
473 .release
= fimc_capture_close
,
474 .poll
= fimc_capture_poll
,
475 .unlocked_ioctl
= video_ioctl2
,
476 .mmap
= fimc_capture_mmap
,
480 * Format and crop negotiation helpers
483 static struct fimc_fmt
*fimc_capture_try_format(struct fimc_ctx
*ctx
,
484 u32
*width
, u32
*height
,
485 u32
*code
, u32
*fourcc
, int pad
)
487 bool rotation
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
488 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
489 struct samsung_fimc_variant
*var
= fimc
->variant
;
490 struct fimc_pix_limit
*pl
= var
->pix_limit
;
491 struct fimc_frame
*dst
= &ctx
->d_frame
;
492 u32 depth
, min_w
, max_w
, min_h
, align_h
= 3;
493 u32 mask
= FMT_FLAGS_CAM
;
494 struct fimc_fmt
*ffmt
;
496 /* Color conversion from/to JPEG is not supported */
497 if (code
&& ctx
->s_frame
.fmt
&& pad
== FIMC_SD_PAD_SOURCE
&&
498 fimc_fmt_is_jpeg(ctx
->s_frame
.fmt
->color
))
499 *code
= V4L2_MBUS_FMT_JPEG_1X8
;
501 if (fourcc
&& *fourcc
!= V4L2_PIX_FMT_JPEG
&& pad
!= FIMC_SD_PAD_SINK
)
502 mask
|= FMT_FLAGS_M2M
;
504 ffmt
= fimc_find_format(fourcc
, code
, mask
, 0);
508 *code
= ffmt
->mbus_code
;
510 *fourcc
= ffmt
->fourcc
;
512 if (pad
== FIMC_SD_PAD_SINK
) {
513 max_w
= fimc_fmt_is_jpeg(ffmt
->color
) ?
514 pl
->scaler_dis_w
: pl
->scaler_en_w
;
515 /* Apply the camera input interface pixel constraints */
516 v4l_bound_align_image(width
, max_t(u32
, *width
, 32), max_w
, 4,
517 height
, max_t(u32
, *height
, 32),
518 FIMC_CAMIF_MAX_HEIGHT
,
519 fimc_fmt_is_jpeg(ffmt
->color
) ? 3 : 1,
523 /* Can't scale or crop in transparent (JPEG) transfer mode */
524 if (fimc_fmt_is_jpeg(ffmt
->color
)) {
525 *width
= ctx
->s_frame
.f_width
;
526 *height
= ctx
->s_frame
.f_height
;
529 /* Apply the scaler and the output DMA constraints */
530 max_w
= rotation
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
;
531 min_w
= ctx
->state
& FIMC_DST_CROP
? dst
->width
: var
->min_out_pixsize
;
532 min_h
= ctx
->state
& FIMC_DST_CROP
? dst
->height
: var
->min_out_pixsize
;
533 if (var
->min_vsize_align
== 1 && !rotation
)
534 align_h
= fimc_fmt_is_rgb(ffmt
->color
) ? 0 : 1;
536 depth
= fimc_get_format_depth(ffmt
);
537 v4l_bound_align_image(width
, min_w
, max_w
,
538 ffs(var
->min_out_pixsize
) - 1,
539 height
, min_h
, FIMC_CAMIF_MAX_HEIGHT
,
541 64/(ALIGN(depth
, 8)));
543 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
544 pad
, code
? *code
: 0, *width
, *height
,
545 dst
->f_width
, dst
->f_height
);
550 static void fimc_capture_try_crop(struct fimc_ctx
*ctx
, struct v4l2_rect
*r
,
553 bool rotate
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
554 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
555 struct samsung_fimc_variant
*var
= fimc
->variant
;
556 struct fimc_pix_limit
*pl
= var
->pix_limit
;
557 struct fimc_frame
*sink
= &ctx
->s_frame
;
558 u32 max_w
, max_h
, min_w
= 0, min_h
= 0, min_sz
;
559 u32 align_sz
= 0, align_h
= 4;
560 u32 max_sc_h
, max_sc_v
;
562 /* In JPEG transparent transfer mode cropping is not supported */
563 if (fimc_fmt_is_jpeg(ctx
->d_frame
.fmt
->color
)) {
564 r
->width
= sink
->f_width
;
565 r
->height
= sink
->f_height
;
566 r
->left
= r
->top
= 0;
569 if (pad
== FIMC_SD_PAD_SOURCE
) {
570 if (ctx
->rotation
!= 90 && ctx
->rotation
!= 270)
572 max_sc_h
= min(SCALER_MAX_HRATIO
, 1 << (ffs(sink
->width
) - 3));
573 max_sc_v
= min(SCALER_MAX_VRATIO
, 1 << (ffs(sink
->height
) - 1));
574 min_sz
= var
->min_out_pixsize
;
576 u32 depth
= fimc_get_format_depth(sink
->fmt
);
577 align_sz
= 64/ALIGN(depth
, 8);
578 min_sz
= var
->min_inp_pixsize
;
579 min_w
= min_h
= min_sz
;
580 max_sc_h
= max_sc_v
= 1;
583 * For the crop rectangle at source pad the following constraints
585 * - it must fit in the sink pad format rectangle (f_width/f_height);
586 * - maximum downscaling ratio is 64;
587 * - maximum crop size depends if the rotator is used or not;
588 * - the sink pad format width/height must be 4 multiple of the
589 * prescaler ratios determined by sink pad size and source pad crop,
590 * the prescaler ratio is returned by fimc_get_scaler_factor().
593 rotate
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
,
594 rotate
? sink
->f_height
: sink
->f_width
);
595 max_h
= min_t(u32
, FIMC_CAMIF_MAX_HEIGHT
, sink
->f_height
);
596 if (pad
== FIMC_SD_PAD_SOURCE
) {
597 min_w
= min_t(u32
, max_w
, sink
->f_width
/ max_sc_h
);
598 min_h
= min_t(u32
, max_h
, sink
->f_height
/ max_sc_v
);
600 swap(max_sc_h
, max_sc_v
);
604 v4l_bound_align_image(&r
->width
, min_w
, max_w
, ffs(min_sz
) - 1,
605 &r
->height
, min_h
, max_h
, align_h
,
607 /* Adjust left/top if cropping rectangle is out of bounds */
608 r
->left
= clamp_t(u32
, r
->left
, 0, sink
->f_width
- r
->width
);
609 r
->top
= clamp_t(u32
, r
->top
, 0, sink
->f_height
- r
->height
);
610 r
->left
= round_down(r
->left
, var
->hor_offs_align
);
612 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
613 pad
, r
->left
, r
->top
, r
->width
, r
->height
,
614 sink
->f_width
, sink
->f_height
);
618 * The video node ioctl operations
620 static int fimc_vidioc_querycap_capture(struct file
*file
, void *priv
,
621 struct v4l2_capability
*cap
)
623 struct fimc_dev
*fimc
= video_drvdata(file
);
625 strncpy(cap
->driver
, fimc
->pdev
->name
, sizeof(cap
->driver
) - 1);
626 strncpy(cap
->card
, fimc
->pdev
->name
, sizeof(cap
->card
) - 1);
627 cap
->bus_info
[0] = 0;
628 cap
->capabilities
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_CAPTURE_MPLANE
;
633 static int fimc_cap_enum_fmt_mplane(struct file
*file
, void *priv
,
634 struct v4l2_fmtdesc
*f
)
636 struct fimc_fmt
*fmt
;
638 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
| FMT_FLAGS_M2M
,
642 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
643 f
->pixelformat
= fmt
->fourcc
;
644 if (fmt
->fourcc
== V4L2_MBUS_FMT_JPEG_1X8
)
645 f
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
650 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
652 * @ctx: FIMC capture context
653 * @tfmt: media bus format to try/set on subdevs
654 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
655 * @set: true to set format on subdevs, false to try only
657 static int fimc_pipeline_try_format(struct fimc_ctx
*ctx
,
658 struct v4l2_mbus_framefmt
*tfmt
,
659 struct fimc_fmt
**fmt_id
,
662 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
663 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
664 struct v4l2_subdev
*csis
= fimc
->pipeline
.csis
;
665 struct v4l2_subdev_format sfmt
;
666 struct v4l2_mbus_framefmt
*mf
= &sfmt
.format
;
667 struct fimc_fmt
*ffmt
= NULL
;
670 if (WARN_ON(!sd
|| !tfmt
))
673 memset(&sfmt
, 0, sizeof(sfmt
));
676 sfmt
.which
= set
? V4L2_SUBDEV_FORMAT_ACTIVE
: V4L2_SUBDEV_FORMAT_TRY
;
678 ffmt
= fimc_find_format(NULL
, mf
->code
!= 0 ? &mf
->code
: NULL
,
682 * Notify user-space if common pixel code for
683 * host and sensor does not exist.
687 mf
->code
= tfmt
->code
= ffmt
->mbus_code
;
689 ret
= v4l2_subdev_call(sd
, pad
, set_fmt
, NULL
, &sfmt
);
692 if (mf
->code
!= tfmt
->code
) {
696 if (mf
->width
!= tfmt
->width
|| mf
->height
!= tfmt
->height
) {
697 u32 fcc
= ffmt
->fourcc
;
698 tfmt
->width
= mf
->width
;
699 tfmt
->height
= mf
->height
;
700 ffmt
= fimc_capture_try_format(ctx
,
701 &tfmt
->width
, &tfmt
->height
,
702 NULL
, &fcc
, FIMC_SD_PAD_SOURCE
);
703 if (ffmt
&& ffmt
->mbus_code
)
704 mf
->code
= ffmt
->mbus_code
;
705 if (mf
->width
!= tfmt
->width
||
706 mf
->height
!= tfmt
->height
)
708 tfmt
->code
= mf
->code
;
711 ret
= v4l2_subdev_call(csis
, pad
, set_fmt
, NULL
, &sfmt
);
713 if (mf
->code
== tfmt
->code
&&
714 mf
->width
== tfmt
->width
&& mf
->height
== tfmt
->height
)
722 dbg("code: 0x%x, %dx%d, %p", mf
->code
, mf
->width
, mf
->height
, ffmt
);
726 static int fimc_cap_g_fmt_mplane(struct file
*file
, void *fh
,
727 struct v4l2_format
*f
)
729 struct fimc_dev
*fimc
= video_drvdata(file
);
730 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
732 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
735 return fimc_fill_format(&ctx
->d_frame
, f
);
738 static int fimc_cap_try_fmt_mplane(struct file
*file
, void *fh
,
739 struct v4l2_format
*f
)
741 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
742 struct fimc_dev
*fimc
= video_drvdata(file
);
743 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
744 struct v4l2_mbus_framefmt mf
;
745 struct fimc_fmt
*ffmt
= NULL
;
747 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
750 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
751 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
752 NULL
, &pix
->pixelformat
,
754 ctx
->s_frame
.f_width
= pix
->width
;
755 ctx
->s_frame
.f_height
= pix
->height
;
757 ffmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
758 NULL
, &pix
->pixelformat
,
763 if (!fimc
->vid_cap
.user_subdev_api
) {
764 mf
.width
= pix
->width
;
765 mf
.height
= pix
->height
;
766 mf
.code
= ffmt
->mbus_code
;
767 fimc_md_graph_lock(fimc
);
768 fimc_pipeline_try_format(ctx
, &mf
, &ffmt
, false);
769 fimc_md_graph_unlock(fimc
);
771 pix
->width
= mf
.width
;
772 pix
->height
= mf
.height
;
774 pix
->pixelformat
= ffmt
->fourcc
;
777 fimc_adjust_mplane_format(ffmt
, pix
->width
, pix
->height
, pix
);
781 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx
*ctx
, bool jpeg
)
783 ctx
->scaler
.enabled
= !jpeg
;
784 fimc_ctrls_activate(ctx
, !jpeg
);
787 set_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
789 clear_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
792 static int fimc_capture_set_format(struct fimc_dev
*fimc
, struct v4l2_format
*f
)
794 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
795 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
796 struct v4l2_mbus_framefmt
*mf
= &fimc
->vid_cap
.mf
;
797 struct fimc_frame
*ff
= &ctx
->d_frame
;
798 struct fimc_fmt
*s_fmt
= NULL
;
801 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
803 if (vb2_is_busy(&fimc
->vid_cap
.vbq
))
806 /* Pre-configure format at camera interface input, for JPEG only */
807 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
808 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
809 NULL
, &pix
->pixelformat
,
811 ctx
->s_frame
.f_width
= pix
->width
;
812 ctx
->s_frame
.f_height
= pix
->height
;
814 /* Try the format at the scaler and the DMA output */
815 ff
->fmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
816 NULL
, &pix
->pixelformat
,
821 /* Update RGB Alpha control state and value range */
822 fimc_alpha_ctrl_update(ctx
);
824 /* Try to match format at the host and the sensor */
825 if (!fimc
->vid_cap
.user_subdev_api
) {
826 mf
->code
= ff
->fmt
->mbus_code
;
827 mf
->width
= pix
->width
;
828 mf
->height
= pix
->height
;
830 fimc_md_graph_lock(fimc
);
831 ret
= fimc_pipeline_try_format(ctx
, mf
, &s_fmt
, true);
832 fimc_md_graph_unlock(fimc
);
835 pix
->width
= mf
->width
;
836 pix
->height
= mf
->height
;
838 fimc_adjust_mplane_format(ff
->fmt
, pix
->width
, pix
->height
, pix
);
839 for (i
= 0; i
< ff
->fmt
->colplanes
; i
++)
841 (pix
->width
* pix
->height
* ff
->fmt
->depth
[i
]) / 8;
843 set_frame_bounds(ff
, pix
->width
, pix
->height
);
844 /* Reset the composition rectangle if not yet configured */
845 if (!(ctx
->state
& FIMC_DST_CROP
))
846 set_frame_crop(ff
, 0, 0, pix
->width
, pix
->height
);
848 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ff
->fmt
->color
));
850 /* Reset cropping and set format at the camera interface input */
851 if (!fimc
->vid_cap
.user_subdev_api
) {
852 ctx
->s_frame
.fmt
= s_fmt
;
853 set_frame_bounds(&ctx
->s_frame
, pix
->width
, pix
->height
);
854 set_frame_crop(&ctx
->s_frame
, 0, 0, pix
->width
, pix
->height
);
860 static int fimc_cap_s_fmt_mplane(struct file
*file
, void *priv
,
861 struct v4l2_format
*f
)
863 struct fimc_dev
*fimc
= video_drvdata(file
);
865 return fimc_capture_set_format(fimc
, f
);
868 static int fimc_cap_enum_input(struct file
*file
, void *priv
,
869 struct v4l2_input
*i
)
871 struct fimc_dev
*fimc
= video_drvdata(file
);
872 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
877 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
879 strlcpy(i
->name
, sd
->name
, sizeof(i
->name
));
883 static int fimc_cap_s_input(struct file
*file
, void *priv
, unsigned int i
)
885 return i
== 0 ? i
: -EINVAL
;
888 static int fimc_cap_g_input(struct file
*file
, void *priv
, unsigned int *i
)
895 * fimc_pipeline_validate - check for formats inconsistencies
896 * between source and sink pad of each link
898 * Return 0 if all formats match or -EPIPE otherwise.
900 static int fimc_pipeline_validate(struct fimc_dev
*fimc
)
902 struct v4l2_subdev_format sink_fmt
, src_fmt
;
903 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
904 struct v4l2_subdev
*sd
;
905 struct media_pad
*pad
;
908 /* Start with the video capture node pad */
909 pad
= media_entity_remote_source(&vid_cap
->vd_pad
);
912 /* FIMC.{N} subdevice */
913 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
916 /* Retrieve format at the sink pad */
917 pad
= &sd
->entity
.pads
[0];
918 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
920 /* Don't call FIMC subdev operation to avoid nested locking */
921 if (sd
== fimc
->vid_cap
.subdev
) {
922 struct fimc_frame
*ff
= &vid_cap
->ctx
->s_frame
;
923 sink_fmt
.format
.width
= ff
->f_width
;
924 sink_fmt
.format
.height
= ff
->f_height
;
925 sink_fmt
.format
.code
= ff
->fmt
? ff
->fmt
->mbus_code
: 0;
927 sink_fmt
.pad
= pad
->index
;
928 sink_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
929 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &sink_fmt
);
930 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
933 /* Retrieve format at the source pad */
934 pad
= media_entity_remote_source(pad
);
936 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
939 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
940 src_fmt
.pad
= pad
->index
;
941 src_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
942 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &src_fmt
);
943 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
946 if (src_fmt
.format
.width
!= sink_fmt
.format
.width
||
947 src_fmt
.format
.height
!= sink_fmt
.format
.height
||
948 src_fmt
.format
.code
!= sink_fmt
.format
.code
)
954 static int fimc_cap_streamon(struct file
*file
, void *priv
,
955 enum v4l2_buf_type type
)
957 struct fimc_dev
*fimc
= video_drvdata(file
);
958 struct fimc_pipeline
*p
= &fimc
->pipeline
;
961 if (fimc_capture_active(fimc
))
964 media_entity_pipeline_start(&p
->sensor
->entity
, p
->pipe
);
966 if (fimc
->vid_cap
.user_subdev_api
) {
967 ret
= fimc_pipeline_validate(fimc
);
971 return vb2_streamon(&fimc
->vid_cap
.vbq
, type
);
974 static int fimc_cap_streamoff(struct file
*file
, void *priv
,
975 enum v4l2_buf_type type
)
977 struct fimc_dev
*fimc
= video_drvdata(file
);
978 struct v4l2_subdev
*sd
= fimc
->pipeline
.sensor
;
981 ret
= vb2_streamoff(&fimc
->vid_cap
.vbq
, type
);
983 media_entity_pipeline_stop(&sd
->entity
);
987 static int fimc_cap_reqbufs(struct file
*file
, void *priv
,
988 struct v4l2_requestbuffers
*reqbufs
)
990 struct fimc_dev
*fimc
= video_drvdata(file
);
991 int ret
= vb2_reqbufs(&fimc
->vid_cap
.vbq
, reqbufs
);
994 fimc
->vid_cap
.reqbufs_count
= reqbufs
->count
;
998 static int fimc_cap_querybuf(struct file
*file
, void *priv
,
999 struct v4l2_buffer
*buf
)
1001 struct fimc_dev
*fimc
= video_drvdata(file
);
1003 return vb2_querybuf(&fimc
->vid_cap
.vbq
, buf
);
1006 static int fimc_cap_qbuf(struct file
*file
, void *priv
,
1007 struct v4l2_buffer
*buf
)
1009 struct fimc_dev
*fimc
= video_drvdata(file
);
1011 return vb2_qbuf(&fimc
->vid_cap
.vbq
, buf
);
1014 static int fimc_cap_dqbuf(struct file
*file
, void *priv
,
1015 struct v4l2_buffer
*buf
)
1017 struct fimc_dev
*fimc
= video_drvdata(file
);
1019 return vb2_dqbuf(&fimc
->vid_cap
.vbq
, buf
, file
->f_flags
& O_NONBLOCK
);
1022 static int fimc_cap_cropcap(struct file
*file
, void *fh
,
1023 struct v4l2_cropcap
*cr
)
1025 struct fimc_dev
*fimc
= video_drvdata(file
);
1026 struct fimc_frame
*f
= &fimc
->vid_cap
.ctx
->s_frame
;
1028 if (cr
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1031 cr
->bounds
.left
= 0;
1033 cr
->bounds
.width
= f
->o_width
;
1034 cr
->bounds
.height
= f
->o_height
;
1035 cr
->defrect
= cr
->bounds
;
1040 static int fimc_cap_g_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
1042 struct fimc_dev
*fimc
= video_drvdata(file
);
1043 struct fimc_frame
*f
= &fimc
->vid_cap
.ctx
->s_frame
;
1045 cr
->c
.left
= f
->offs_h
;
1046 cr
->c
.top
= f
->offs_v
;
1047 cr
->c
.width
= f
->width
;
1048 cr
->c
.height
= f
->height
;
1053 static int fimc_cap_s_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
1055 struct fimc_dev
*fimc
= video_drvdata(file
);
1056 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1057 struct fimc_frame
*ff
;
1058 unsigned long flags
;
1060 fimc_capture_try_crop(ctx
, &cr
->c
, FIMC_SD_PAD_SINK
);
1063 spin_lock_irqsave(&fimc
->slock
, flags
);
1064 set_frame_crop(ff
, cr
->c
.left
, cr
->c
.top
, cr
->c
.width
, cr
->c
.height
);
1065 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1066 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1071 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops
= {
1072 .vidioc_querycap
= fimc_vidioc_querycap_capture
,
1074 .vidioc_enum_fmt_vid_cap_mplane
= fimc_cap_enum_fmt_mplane
,
1075 .vidioc_try_fmt_vid_cap_mplane
= fimc_cap_try_fmt_mplane
,
1076 .vidioc_s_fmt_vid_cap_mplane
= fimc_cap_s_fmt_mplane
,
1077 .vidioc_g_fmt_vid_cap_mplane
= fimc_cap_g_fmt_mplane
,
1079 .vidioc_reqbufs
= fimc_cap_reqbufs
,
1080 .vidioc_querybuf
= fimc_cap_querybuf
,
1082 .vidioc_qbuf
= fimc_cap_qbuf
,
1083 .vidioc_dqbuf
= fimc_cap_dqbuf
,
1085 .vidioc_streamon
= fimc_cap_streamon
,
1086 .vidioc_streamoff
= fimc_cap_streamoff
,
1088 .vidioc_g_crop
= fimc_cap_g_crop
,
1089 .vidioc_s_crop
= fimc_cap_s_crop
,
1090 .vidioc_cropcap
= fimc_cap_cropcap
,
1092 .vidioc_enum_input
= fimc_cap_enum_input
,
1093 .vidioc_s_input
= fimc_cap_s_input
,
1094 .vidioc_g_input
= fimc_cap_g_input
,
1097 /* Capture subdev media entity operations */
1098 static int fimc_link_setup(struct media_entity
*entity
,
1099 const struct media_pad
*local
,
1100 const struct media_pad
*remote
, u32 flags
)
1102 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
1103 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1105 if (media_entity_type(remote
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
1108 if (WARN_ON(fimc
== NULL
))
1111 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1112 local
->entity
->name
, remote
->entity
->name
, flags
,
1113 fimc
->vid_cap
.input
);
1115 if (flags
& MEDIA_LNK_FL_ENABLED
) {
1116 if (fimc
->vid_cap
.input
!= 0)
1118 fimc
->vid_cap
.input
= sd
->grp_id
;
1122 fimc
->vid_cap
.input
= 0;
1126 static const struct media_entity_operations fimc_sd_media_ops
= {
1127 .link_setup
= fimc_link_setup
,
1131 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1132 * @sd: pointer to a subdev generating the notification
1133 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1134 * @arg: pointer to an u32 type integer that stores the frame payload value
1136 * The End Of Frame notification sent by sensor subdev in its still capture
1137 * mode. If there is only a single VSYNC generated by the sensor at the
1138 * beginning of a frame transmission, FIMC does not issue the LastIrq
1139 * (end of frame) interrupt. And this notification is used to complete the
1140 * frame capture and returning a buffer to user-space. Subdev drivers should
1141 * call this notification from their last 'End of frame capture' interrupt.
1143 void fimc_sensor_notify(struct v4l2_subdev
*sd
, unsigned int notification
,
1146 struct fimc_sensor_info
*sensor
;
1147 struct fimc_vid_buffer
*buf
;
1148 struct fimc_md
*fmd
;
1149 struct fimc_dev
*fimc
;
1150 unsigned long flags
;
1155 sensor
= v4l2_get_subdev_hostdata(sd
);
1156 fmd
= entity_to_fimc_mdev(&sd
->entity
);
1158 spin_lock_irqsave(&fmd
->slock
, flags
);
1159 fimc
= sensor
? sensor
->host
: NULL
;
1161 if (fimc
&& arg
&& notification
== S5P_FIMC_TX_END_NOTIFY
&&
1162 test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
1163 unsigned long irq_flags
;
1164 spin_lock_irqsave(&fimc
->slock
, irq_flags
);
1165 if (!list_empty(&fimc
->vid_cap
.active_buf_q
)) {
1166 buf
= list_entry(fimc
->vid_cap
.active_buf_q
.next
,
1167 struct fimc_vid_buffer
, list
);
1168 vb2_set_plane_payload(&buf
->vb
, 0, *((u32
*)arg
));
1170 fimc_capture_irq_handler(fimc
, true);
1171 fimc_deactivate_capture(fimc
);
1172 spin_unlock_irqrestore(&fimc
->slock
, irq_flags
);
1174 spin_unlock_irqrestore(&fmd
->slock
, flags
);
1177 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev
*sd
,
1178 struct v4l2_subdev_fh
*fh
,
1179 struct v4l2_subdev_mbus_code_enum
*code
)
1181 struct fimc_fmt
*fmt
;
1183 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, code
->index
);
1186 code
->code
= fmt
->mbus_code
;
1190 static int fimc_subdev_get_fmt(struct v4l2_subdev
*sd
,
1191 struct v4l2_subdev_fh
*fh
,
1192 struct v4l2_subdev_format
*fmt
)
1194 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1195 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1196 struct v4l2_mbus_framefmt
*mf
;
1197 struct fimc_frame
*ff
;
1199 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1200 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1205 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1206 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
? &ctx
->s_frame
: &ctx
->d_frame
;
1208 mutex_lock(&fimc
->lock
);
1209 /* The pixel code is same on both input and output pad */
1210 if (!WARN_ON(ctx
->s_frame
.fmt
== NULL
))
1211 mf
->code
= ctx
->s_frame
.fmt
->mbus_code
;
1212 mf
->width
= ff
->f_width
;
1213 mf
->height
= ff
->f_height
;
1214 mutex_unlock(&fimc
->lock
);
1219 static int fimc_subdev_set_fmt(struct v4l2_subdev
*sd
,
1220 struct v4l2_subdev_fh
*fh
,
1221 struct v4l2_subdev_format
*fmt
)
1223 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1224 struct v4l2_mbus_framefmt
*mf
= &fmt
->format
;
1225 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1226 struct fimc_frame
*ff
;
1227 struct fimc_fmt
*ffmt
;
1229 dbg("pad%d: code: 0x%x, %dx%d",
1230 fmt
->pad
, mf
->code
, mf
->width
, mf
->height
);
1232 if (fmt
->pad
== FIMC_SD_PAD_SOURCE
&&
1233 vb2_is_busy(&fimc
->vid_cap
.vbq
))
1236 mutex_lock(&fimc
->lock
);
1237 ffmt
= fimc_capture_try_format(ctx
, &mf
->width
, &mf
->height
,
1238 &mf
->code
, NULL
, fmt
->pad
);
1239 mutex_unlock(&fimc
->lock
);
1240 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1242 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1243 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1247 /* Update RGB Alpha control state and value range */
1248 fimc_alpha_ctrl_update(ctx
);
1250 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ffmt
->color
));
1252 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
?
1253 &ctx
->s_frame
: &ctx
->d_frame
;
1255 mutex_lock(&fimc
->lock
);
1256 set_frame_bounds(ff
, mf
->width
, mf
->height
);
1257 fimc
->vid_cap
.mf
= *mf
;
1260 /* Reset the crop rectangle if required. */
1261 if (!(fmt
->pad
== FIMC_SD_PAD_SOURCE
&& (ctx
->state
& FIMC_DST_CROP
)))
1262 set_frame_crop(ff
, 0, 0, mf
->width
, mf
->height
);
1264 if (fmt
->pad
== FIMC_SD_PAD_SINK
)
1265 ctx
->state
&= ~FIMC_DST_CROP
;
1266 mutex_unlock(&fimc
->lock
);
1270 static int fimc_subdev_get_crop(struct v4l2_subdev
*sd
,
1271 struct v4l2_subdev_fh
*fh
,
1272 struct v4l2_subdev_crop
*crop
)
1274 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1275 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1276 struct v4l2_rect
*r
= &crop
->rect
;
1277 struct fimc_frame
*ff
;
1279 if (crop
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1280 crop
->rect
= *v4l2_subdev_get_try_crop(fh
, crop
->pad
);
1283 ff
= crop
->pad
== FIMC_SD_PAD_SINK
?
1284 &ctx
->s_frame
: &ctx
->d_frame
;
1286 mutex_lock(&fimc
->lock
);
1287 r
->left
= ff
->offs_h
;
1288 r
->top
= ff
->offs_v
;
1289 r
->width
= ff
->width
;
1290 r
->height
= ff
->height
;
1291 mutex_unlock(&fimc
->lock
);
1293 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1294 ff
, crop
->pad
, r
->left
, r
->top
, r
->width
, r
->height
,
1295 ff
->f_width
, ff
->f_height
);
1300 static int fimc_subdev_set_crop(struct v4l2_subdev
*sd
,
1301 struct v4l2_subdev_fh
*fh
,
1302 struct v4l2_subdev_crop
*crop
)
1304 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1305 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1306 struct v4l2_rect
*r
= &crop
->rect
;
1307 struct fimc_frame
*ff
;
1308 unsigned long flags
;
1310 dbg("(%d,%d)/%dx%d", r
->left
, r
->top
, r
->width
, r
->height
);
1312 ff
= crop
->pad
== FIMC_SD_PAD_SOURCE
?
1313 &ctx
->d_frame
: &ctx
->s_frame
;
1315 mutex_lock(&fimc
->lock
);
1316 fimc_capture_try_crop(ctx
, r
, crop
->pad
);
1318 if (crop
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1319 mutex_unlock(&fimc
->lock
);
1320 *v4l2_subdev_get_try_crop(fh
, crop
->pad
) = *r
;
1323 spin_lock_irqsave(&fimc
->slock
, flags
);
1324 set_frame_crop(ff
, r
->left
, r
->top
, r
->width
, r
->height
);
1325 if (crop
->pad
== FIMC_SD_PAD_SOURCE
)
1326 ctx
->state
|= FIMC_DST_CROP
;
1328 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1329 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1331 dbg("pad%d: (%d,%d)/%dx%d", crop
->pad
, r
->left
, r
->top
,
1332 r
->width
, r
->height
);
1334 mutex_unlock(&fimc
->lock
);
1338 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops
= {
1339 .enum_mbus_code
= fimc_subdev_enum_mbus_code
,
1340 .get_fmt
= fimc_subdev_get_fmt
,
1341 .set_fmt
= fimc_subdev_set_fmt
,
1342 .get_crop
= fimc_subdev_get_crop
,
1343 .set_crop
= fimc_subdev_set_crop
,
1346 static struct v4l2_subdev_ops fimc_subdev_ops
= {
1347 .pad
= &fimc_subdev_pad_ops
,
1350 static int fimc_create_capture_subdev(struct fimc_dev
*fimc
,
1351 struct v4l2_device
*v4l2_dev
)
1353 struct v4l2_subdev
*sd
;
1356 sd
= kzalloc(sizeof(*sd
), GFP_KERNEL
);
1360 v4l2_subdev_init(sd
, &fimc_subdev_ops
);
1361 sd
->flags
= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1362 snprintf(sd
->name
, sizeof(sd
->name
), "FIMC.%d", fimc
->pdev
->id
);
1364 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SINK
].flags
= MEDIA_PAD_FL_SINK
;
1365 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SOURCE
].flags
= MEDIA_PAD_FL_SOURCE
;
1366 ret
= media_entity_init(&sd
->entity
, FIMC_SD_PADS_NUM
,
1367 fimc
->vid_cap
.sd_pads
, 0);
1370 ret
= v4l2_device_register_subdev(v4l2_dev
, sd
);
1374 fimc
->vid_cap
.subdev
= sd
;
1375 v4l2_set_subdevdata(sd
, fimc
);
1376 sd
->entity
.ops
= &fimc_sd_media_ops
;
1379 media_entity_cleanup(&sd
->entity
);
1385 static void fimc_destroy_capture_subdev(struct fimc_dev
*fimc
)
1387 struct v4l2_subdev
*sd
= fimc
->vid_cap
.subdev
;
1391 media_entity_cleanup(&sd
->entity
);
1392 v4l2_device_unregister_subdev(sd
);
1394 fimc
->vid_cap
.subdev
= NULL
;
1397 /* Set default format at the sensor and host interface */
1398 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
)
1400 struct v4l2_format fmt
= {
1401 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
,
1405 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1406 .field
= V4L2_FIELD_NONE
,
1407 .colorspace
= V4L2_COLORSPACE_JPEG
,
1411 return fimc_capture_set_format(fimc
, &fmt
);
1414 /* fimc->lock must be already initialized */
1415 int fimc_register_capture_device(struct fimc_dev
*fimc
,
1416 struct v4l2_device
*v4l2_dev
)
1418 struct video_device
*vfd
;
1419 struct fimc_vid_cap
*vid_cap
;
1420 struct fimc_ctx
*ctx
;
1421 struct vb2_queue
*q
;
1424 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
1428 ctx
->fimc_dev
= fimc
;
1429 ctx
->in_path
= FIMC_CAMERA
;
1430 ctx
->out_path
= FIMC_DMA
;
1431 ctx
->state
= FIMC_CTX_CAP
;
1432 ctx
->s_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1433 ctx
->d_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1435 vfd
= video_device_alloc();
1437 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
1441 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s.capture",
1442 dev_name(&fimc
->pdev
->dev
));
1444 vfd
->fops
= &fimc_capture_fops
;
1445 vfd
->ioctl_ops
= &fimc_capture_ioctl_ops
;
1446 vfd
->v4l2_dev
= v4l2_dev
;
1448 vfd
->release
= video_device_release
;
1449 vfd
->lock
= &fimc
->lock
;
1450 video_set_drvdata(vfd
, fimc
);
1452 vid_cap
= &fimc
->vid_cap
;
1454 vid_cap
->active_buf_cnt
= 0;
1455 vid_cap
->reqbufs_count
= 0;
1456 vid_cap
->refcnt
= 0;
1458 INIT_LIST_HEAD(&vid_cap
->pending_buf_q
);
1459 INIT_LIST_HEAD(&vid_cap
->active_buf_q
);
1460 spin_lock_init(&ctx
->slock
);
1463 q
= &fimc
->vid_cap
.vbq
;
1464 memset(q
, 0, sizeof(*q
));
1465 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1466 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1467 q
->drv_priv
= fimc
->vid_cap
.ctx
;
1468 q
->ops
= &fimc_capture_qops
;
1469 q
->mem_ops
= &vb2_dma_contig_memops
;
1470 q
->buf_struct_size
= sizeof(struct fimc_vid_buffer
);
1474 fimc
->vid_cap
.vd_pad
.flags
= MEDIA_PAD_FL_SINK
;
1475 ret
= media_entity_init(&vfd
->entity
, 1, &fimc
->vid_cap
.vd_pad
, 0);
1478 ret
= fimc_create_capture_subdev(fimc
, v4l2_dev
);
1482 vfd
->ctrl_handler
= &ctx
->ctrl_handler
;
1486 media_entity_cleanup(&vfd
->entity
);
1488 video_device_release(vfd
);
1494 void fimc_unregister_capture_device(struct fimc_dev
*fimc
)
1496 struct video_device
*vfd
= fimc
->vid_cap
.vfd
;
1499 media_entity_cleanup(&vfd
->entity
);
1500 /* Can also be called if video device was
1502 video_unregister_device(vfd
);
1504 fimc_destroy_capture_subdev(fimc
);
1505 kfree(fimc
->vid_cap
.ctx
);
1506 fimc
->vid_cap
.ctx
= NULL
;