2 * S5P camera interface (video postprocessor) driver
4 * Copyright (c) 2010 Samsung Electronics Co., Ltd
6 * Sylwester Nawrocki, <s.nawrocki@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation, either version 2 of the License,
11 * or (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/version.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/bug.h>
20 #include <linux/interrupt.h>
21 #include <linux/device.h>
22 #include <linux/platform_device.h>
23 #include <linux/list.h>
25 #include <linux/slab.h>
26 #include <linux/clk.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/videobuf2-core.h>
29 #include <media/videobuf2-dma-contig.h>
31 #include "fimc-core.h"
33 static char *fimc_clock_name
[NUM_FIMC_CLOCKS
] = { "sclk_fimc", "fimc" };
35 static struct fimc_fmt fimc_formats
[] = {
38 .fourcc
= V4L2_PIX_FMT_RGB565X
,
40 .color
= S5P_FIMC_RGB565
,
43 .mbus_code
= V4L2_MBUS_FMT_RGB565_2X8_BE
,
44 .flags
= FMT_FLAGS_M2M
,
47 .fourcc
= V4L2_PIX_FMT_BGR666
,
49 .color
= S5P_FIMC_RGB666
,
52 .flags
= FMT_FLAGS_M2M
,
54 .name
= "XRGB-8-8-8-8, 32 bpp",
55 .fourcc
= V4L2_PIX_FMT_RGB32
,
57 .color
= S5P_FIMC_RGB888
,
60 .flags
= FMT_FLAGS_M2M
,
62 .name
= "YUV 4:2:2 packed, YCbYCr",
63 .fourcc
= V4L2_PIX_FMT_YUYV
,
65 .color
= S5P_FIMC_YCBYCR422
,
68 .mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
,
69 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
71 .name
= "YUV 4:2:2 packed, CbYCrY",
72 .fourcc
= V4L2_PIX_FMT_UYVY
,
74 .color
= S5P_FIMC_CBYCRY422
,
77 .mbus_code
= V4L2_MBUS_FMT_UYVY8_2X8
,
78 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
80 .name
= "YUV 4:2:2 packed, CrYCbY",
81 .fourcc
= V4L2_PIX_FMT_VYUY
,
83 .color
= S5P_FIMC_CRYCBY422
,
86 .mbus_code
= V4L2_MBUS_FMT_VYUY8_2X8
,
87 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
89 .name
= "YUV 4:2:2 packed, YCrYCb",
90 .fourcc
= V4L2_PIX_FMT_YVYU
,
92 .color
= S5P_FIMC_YCRYCB422
,
95 .mbus_code
= V4L2_MBUS_FMT_YVYU8_2X8
,
96 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
98 .name
= "YUV 4:2:2 planar, Y/Cb/Cr",
99 .fourcc
= V4L2_PIX_FMT_YUV422P
,
101 .color
= S5P_FIMC_YCBYCR422
,
104 .flags
= FMT_FLAGS_M2M
,
106 .name
= "YUV 4:2:2 planar, Y/CbCr",
107 .fourcc
= V4L2_PIX_FMT_NV16
,
109 .color
= S5P_FIMC_YCBYCR422
,
112 .flags
= FMT_FLAGS_M2M
,
114 .name
= "YUV 4:2:2 planar, Y/CrCb",
115 .fourcc
= V4L2_PIX_FMT_NV61
,
117 .color
= S5P_FIMC_YCRYCB422
,
120 .flags
= FMT_FLAGS_M2M
,
122 .name
= "YUV 4:2:0 planar, YCbCr",
123 .fourcc
= V4L2_PIX_FMT_YUV420
,
125 .color
= S5P_FIMC_YCBCR420
,
128 .flags
= FMT_FLAGS_M2M
,
130 .name
= "YUV 4:2:0 planar, Y/CbCr",
131 .fourcc
= V4L2_PIX_FMT_NV12
,
133 .color
= S5P_FIMC_YCBCR420
,
136 .flags
= FMT_FLAGS_M2M
,
138 .name
= "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr",
139 .fourcc
= V4L2_PIX_FMT_NV12M
,
140 .color
= S5P_FIMC_YCBCR420
,
144 .flags
= FMT_FLAGS_M2M
,
146 .name
= "YUV 4:2:0 non-contiguous 3-planar, Y/Cb/Cr",
147 .fourcc
= V4L2_PIX_FMT_YUV420M
,
148 .color
= S5P_FIMC_YCBCR420
,
149 .depth
= { 8, 2, 2 },
152 .flags
= FMT_FLAGS_M2M
,
154 .name
= "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr, tiled",
155 .fourcc
= V4L2_PIX_FMT_NV12MT
,
156 .color
= S5P_FIMC_YCBCR420
,
160 .flags
= FMT_FLAGS_M2M
,
164 static struct v4l2_queryctrl fimc_ctrls
[] = {
166 .id
= V4L2_CID_HFLIP
,
167 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
168 .name
= "Horizontal flip",
173 .id
= V4L2_CID_VFLIP
,
174 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
175 .name
= "Vertical flip",
180 .id
= V4L2_CID_ROTATE
,
181 .type
= V4L2_CTRL_TYPE_INTEGER
,
182 .name
= "Rotation (CCW)",
191 static struct v4l2_queryctrl
*get_ctrl(int id
)
195 for (i
= 0; i
< ARRAY_SIZE(fimc_ctrls
); ++i
)
196 if (id
== fimc_ctrls
[i
].id
)
197 return &fimc_ctrls
[i
];
201 int fimc_check_scaler_ratio(struct v4l2_rect
*r
, struct fimc_frame
*f
)
203 if (r
->width
> f
->width
) {
204 if (f
->width
> (r
->width
* SCALER_MAX_HRATIO
))
207 if ((f
->width
* SCALER_MAX_HRATIO
) < r
->width
)
211 if (r
->height
> f
->height
) {
212 if (f
->height
> (r
->height
* SCALER_MAX_VRATIO
))
215 if ((f
->height
* SCALER_MAX_VRATIO
) < r
->height
)
222 static int fimc_get_scaler_factor(u32 src
, u32 tar
, u32
*ratio
, u32
*shift
)
231 if (src
>= tar
* tmp
) {
232 *shift
= sh
, *ratio
= tmp
;
237 *shift
= 0, *ratio
= 1;
239 dbg("s: %d, t: %d, shift: %d, ratio: %d",
240 src
, tar
, *shift
, *ratio
);
244 int fimc_set_scaler_info(struct fimc_ctx
*ctx
)
246 struct fimc_scaler
*sc
= &ctx
->scaler
;
247 struct fimc_frame
*s_frame
= &ctx
->s_frame
;
248 struct fimc_frame
*d_frame
= &ctx
->d_frame
;
252 if (ctx
->rotation
== 90 || ctx
->rotation
== 270) {
254 tx
= d_frame
->height
;
257 ty
= d_frame
->height
;
259 if (tx
<= 0 || ty
<= 0) {
260 v4l2_err(&ctx
->fimc_dev
->m2m
.v4l2_dev
,
261 "invalid target size: %d x %d", tx
, ty
);
266 sy
= s_frame
->height
;
267 if (sx
<= 0 || sy
<= 0) {
268 err("invalid source size: %d x %d", sx
, sy
);
273 sc
->real_height
= sy
;
274 dbg("sx= %d, sy= %d, tx= %d, ty= %d", sx
, sy
, tx
, ty
);
276 ret
= fimc_get_scaler_factor(sx
, tx
, &sc
->pre_hratio
, &sc
->hfactor
);
280 ret
= fimc_get_scaler_factor(sy
, ty
, &sc
->pre_vratio
, &sc
->vfactor
);
284 sc
->pre_dst_width
= sx
/ sc
->pre_hratio
;
285 sc
->pre_dst_height
= sy
/ sc
->pre_vratio
;
287 sc
->main_hratio
= (sx
<< 8) / (tx
<< sc
->hfactor
);
288 sc
->main_vratio
= (sy
<< 8) / (ty
<< sc
->vfactor
);
290 sc
->scaleup_h
= (tx
>= sx
) ? 1 : 0;
291 sc
->scaleup_v
= (ty
>= sy
) ? 1 : 0;
293 /* check to see if input and output size/format differ */
294 if (s_frame
->fmt
->color
== d_frame
->fmt
->color
295 && s_frame
->width
== d_frame
->width
296 && s_frame
->height
== d_frame
->height
)
304 static void fimc_capture_handler(struct fimc_dev
*fimc
)
306 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
307 struct fimc_vid_buffer
*v_buf
= NULL
;
309 if (!list_empty(&cap
->active_buf_q
)) {
310 v_buf
= active_queue_pop(cap
);
311 vb2_buffer_done(&v_buf
->vb
, VB2_BUF_STATE_DONE
);
314 if (test_and_clear_bit(ST_CAPT_SHUT
, &fimc
->state
)) {
315 wake_up(&fimc
->irq_queue
);
319 if (!list_empty(&cap
->pending_buf_q
)) {
321 v_buf
= pending_queue_pop(cap
);
322 fimc_hw_set_output_addr(fimc
, &v_buf
->paddr
, cap
->buf_index
);
323 v_buf
->index
= cap
->buf_index
;
325 dbg("hw ptr: %d, sw ptr: %d",
326 fimc_hw_get_frame_index(fimc
), cap
->buf_index
);
328 /* Move the buffer to the capture active queue */
329 active_queue_add(cap
, v_buf
);
331 dbg("next frame: %d, done frame: %d",
332 fimc_hw_get_frame_index(fimc
), v_buf
->index
);
334 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
337 } else if (test_and_clear_bit(ST_CAPT_STREAM
, &fimc
->state
) &&
338 cap
->active_buf_cnt
<= 1) {
339 fimc_deactivate_capture(fimc
);
342 dbg("frame: %d, active_buf_cnt= %d",
343 fimc_hw_get_frame_index(fimc
), cap
->active_buf_cnt
);
346 static irqreturn_t
fimc_isr(int irq
, void *priv
)
348 struct fimc_dev
*fimc
= priv
;
351 fimc_hw_clear_irq(fimc
);
353 spin_lock(&fimc
->slock
);
355 if (test_and_clear_bit(ST_M2M_PEND
, &fimc
->state
)) {
356 struct vb2_buffer
*src_vb
, *dst_vb
;
357 struct fimc_ctx
*ctx
= v4l2_m2m_get_curr_priv(fimc
->m2m
.m2m_dev
);
359 if (!ctx
|| !ctx
->m2m_ctx
)
362 src_vb
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
363 dst_vb
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
);
364 if (src_vb
&& dst_vb
) {
365 v4l2_m2m_buf_done(src_vb
, VB2_BUF_STATE_DONE
);
366 v4l2_m2m_buf_done(dst_vb
, VB2_BUF_STATE_DONE
);
367 v4l2_m2m_job_finish(fimc
->m2m
.m2m_dev
, ctx
->m2m_ctx
);
373 if (test_bit(ST_CAPT_RUN
, &fimc
->state
))
374 fimc_capture_handler(fimc
);
376 if (test_and_clear_bit(ST_CAPT_PEND
, &fimc
->state
)) {
377 set_bit(ST_CAPT_RUN
, &fimc
->state
);
378 wake_up(&fimc
->irq_queue
);
382 spin_unlock(&fimc
->slock
);
386 /* The color format (colplanes, memplanes) must be already configured. */
387 int fimc_prepare_addr(struct fimc_ctx
*ctx
, struct vb2_buffer
*vb
,
388 struct fimc_frame
*frame
, struct fimc_addr
*paddr
)
393 if (vb
== NULL
|| frame
== NULL
)
396 pix_size
= frame
->width
* frame
->height
;
398 dbg("memplanes= %d, colplanes= %d, pix_size= %d",
399 frame
->fmt
->memplanes
, frame
->fmt
->colplanes
, pix_size
);
401 paddr
->y
= vb2_dma_contig_plane_paddr(vb
, 0);
403 if (frame
->fmt
->memplanes
== 1) {
404 switch (frame
->fmt
->colplanes
) {
410 /* decompose Y into Y/Cb */
411 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
415 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
416 /* decompose Y into Y/Cb/Cr */
417 if (S5P_FIMC_YCBCR420
== frame
->fmt
->color
)
418 paddr
->cr
= (u32
)(paddr
->cb
421 paddr
->cr
= (u32
)(paddr
->cb
428 if (frame
->fmt
->memplanes
>= 2)
429 paddr
->cb
= vb2_dma_contig_plane_paddr(vb
, 1);
431 if (frame
->fmt
->memplanes
== 3)
432 paddr
->cr
= vb2_dma_contig_plane_paddr(vb
, 2);
435 dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
436 paddr
->y
, paddr
->cb
, paddr
->cr
, ret
);
441 /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
442 static void fimc_set_yuv_order(struct fimc_ctx
*ctx
)
444 /* The one only mode supported in SoC. */
445 ctx
->in_order_2p
= S5P_FIMC_LSB_CRCB
;
446 ctx
->out_order_2p
= S5P_FIMC_LSB_CRCB
;
448 /* Set order for 1 plane input formats. */
449 switch (ctx
->s_frame
.fmt
->color
) {
450 case S5P_FIMC_YCRYCB422
:
451 ctx
->in_order_1p
= S5P_FIMC_IN_YCRYCB
;
453 case S5P_FIMC_CBYCRY422
:
454 ctx
->in_order_1p
= S5P_FIMC_IN_CBYCRY
;
456 case S5P_FIMC_CRYCBY422
:
457 ctx
->in_order_1p
= S5P_FIMC_IN_CRYCBY
;
459 case S5P_FIMC_YCBYCR422
:
461 ctx
->in_order_1p
= S5P_FIMC_IN_YCBYCR
;
464 dbg("ctx->in_order_1p= %d", ctx
->in_order_1p
);
466 switch (ctx
->d_frame
.fmt
->color
) {
467 case S5P_FIMC_YCRYCB422
:
468 ctx
->out_order_1p
= S5P_FIMC_OUT_YCRYCB
;
470 case S5P_FIMC_CBYCRY422
:
471 ctx
->out_order_1p
= S5P_FIMC_OUT_CBYCRY
;
473 case S5P_FIMC_CRYCBY422
:
474 ctx
->out_order_1p
= S5P_FIMC_OUT_CRYCBY
;
476 case S5P_FIMC_YCBYCR422
:
478 ctx
->out_order_1p
= S5P_FIMC_OUT_YCBYCR
;
481 dbg("ctx->out_order_1p= %d", ctx
->out_order_1p
);
484 static void fimc_prepare_dma_offset(struct fimc_ctx
*ctx
, struct fimc_frame
*f
)
486 struct samsung_fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
489 for (i
= 0; i
< f
->fmt
->colplanes
; i
++)
490 depth
+= f
->fmt
->depth
[i
];
492 f
->dma_offset
.y_h
= f
->offs_h
;
493 if (!variant
->pix_hoff
)
494 f
->dma_offset
.y_h
*= (depth
>> 3);
496 f
->dma_offset
.y_v
= f
->offs_v
;
498 f
->dma_offset
.cb_h
= f
->offs_h
;
499 f
->dma_offset
.cb_v
= f
->offs_v
;
501 f
->dma_offset
.cr_h
= f
->offs_h
;
502 f
->dma_offset
.cr_v
= f
->offs_v
;
504 if (!variant
->pix_hoff
) {
505 if (f
->fmt
->colplanes
== 3) {
506 f
->dma_offset
.cb_h
>>= 1;
507 f
->dma_offset
.cr_h
>>= 1;
509 if (f
->fmt
->color
== S5P_FIMC_YCBCR420
) {
510 f
->dma_offset
.cb_v
>>= 1;
511 f
->dma_offset
.cr_v
>>= 1;
515 dbg("in_offset: color= %d, y_h= %d, y_v= %d",
516 f
->fmt
->color
, f
->dma_offset
.y_h
, f
->dma_offset
.y_v
);
520 * fimc_prepare_config - check dimensions, operation and color mode
521 * and pre-calculate offset and the scaling coefficients.
523 * @ctx: hardware context information
524 * @flags: flags indicating which parameters to check/update
526 * Return: 0 if dimensions are valid or non zero otherwise.
528 int fimc_prepare_config(struct fimc_ctx
*ctx
, u32 flags
)
530 struct fimc_frame
*s_frame
, *d_frame
;
531 struct vb2_buffer
*vb
= NULL
;
534 s_frame
= &ctx
->s_frame
;
535 d_frame
= &ctx
->d_frame
;
537 if (flags
& FIMC_PARAMS
) {
538 /* Prepare the DMA offset ratios for scaler. */
539 fimc_prepare_dma_offset(ctx
, &ctx
->s_frame
);
540 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
542 if (s_frame
->height
> (SCALER_MAX_VRATIO
* d_frame
->height
) ||
543 s_frame
->width
> (SCALER_MAX_HRATIO
* d_frame
->width
)) {
544 err("out of scaler range");
547 fimc_set_yuv_order(ctx
);
550 /* Input DMA mode is not allowed when the scaler is disabled. */
551 ctx
->scaler
.enabled
= 1;
553 if (flags
& FIMC_SRC_ADDR
) {
554 vb
= v4l2_m2m_next_src_buf(ctx
->m2m_ctx
);
555 ret
= fimc_prepare_addr(ctx
, vb
, s_frame
, &s_frame
->paddr
);
560 if (flags
& FIMC_DST_ADDR
) {
561 vb
= v4l2_m2m_next_dst_buf(ctx
->m2m_ctx
);
562 ret
= fimc_prepare_addr(ctx
, vb
, d_frame
, &d_frame
->paddr
);
568 static void fimc_dma_run(void *priv
)
570 struct fimc_ctx
*ctx
= priv
;
571 struct fimc_dev
*fimc
;
575 if (WARN(!ctx
, "null hardware context\n"))
578 fimc
= ctx
->fimc_dev
;
580 spin_lock_irqsave(&ctx
->slock
, flags
);
581 set_bit(ST_M2M_PEND
, &fimc
->state
);
583 ctx
->state
|= (FIMC_SRC_ADDR
| FIMC_DST_ADDR
);
584 ret
= fimc_prepare_config(ctx
, ctx
->state
);
586 err("Wrong parameters");
589 /* Reconfigure hardware if the context has changed. */
590 if (fimc
->m2m
.ctx
!= ctx
) {
591 ctx
->state
|= FIMC_PARAMS
;
595 fimc_hw_set_input_addr(fimc
, &ctx
->s_frame
.paddr
);
597 if (ctx
->state
& FIMC_PARAMS
) {
598 fimc_hw_set_input_path(ctx
);
599 fimc_hw_set_in_dma(ctx
);
600 if (fimc_set_scaler_info(ctx
)) {
601 err("Scaler setup error");
604 fimc_hw_set_scaler(ctx
);
605 fimc_hw_set_target_format(ctx
);
606 fimc_hw_set_rotation(ctx
);
607 fimc_hw_set_effect(ctx
);
610 fimc_hw_set_output_path(ctx
);
611 if (ctx
->state
& (FIMC_DST_ADDR
| FIMC_PARAMS
))
612 fimc_hw_set_output_addr(fimc
, &ctx
->d_frame
.paddr
, -1);
614 if (ctx
->state
& FIMC_PARAMS
)
615 fimc_hw_set_out_dma(ctx
);
617 fimc_activate_capture(ctx
);
619 ctx
->state
&= (FIMC_CTX_M2M
| FIMC_CTX_CAP
|
620 FIMC_SRC_FMT
| FIMC_DST_FMT
);
621 fimc_hw_activate_input_dma(fimc
, true);
624 spin_unlock_irqrestore(&ctx
->slock
, flags
);
627 static void fimc_job_abort(void *priv
)
629 /* Nothing done in job_abort. */
632 static int fimc_queue_setup(struct vb2_queue
*vq
, unsigned int *num_buffers
,
633 unsigned int *num_planes
, unsigned long sizes
[],
636 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
637 struct fimc_frame
*f
;
640 f
= ctx_get_frame(ctx
, vq
->type
);
645 * Return number of non-contigous planes (plane buffers)
646 * depending on the configured color format.
649 *num_planes
= f
->fmt
->memplanes
;
651 for (i
= 0; i
< f
->fmt
->memplanes
; i
++) {
652 sizes
[i
] = (f
->width
* f
->height
* f
->fmt
->depth
[i
]) >> 3;
653 allocators
[i
] = ctx
->fimc_dev
->alloc_ctx
;
656 if (*num_buffers
== 0)
662 static int fimc_buf_prepare(struct vb2_buffer
*vb
)
664 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
665 struct fimc_frame
*frame
;
668 frame
= ctx_get_frame(ctx
, vb
->vb2_queue
->type
);
670 return PTR_ERR(frame
);
672 for (i
= 0; i
< frame
->fmt
->memplanes
; i
++)
673 vb2_set_plane_payload(vb
, i
, frame
->payload
[i
]);
678 static void fimc_buf_queue(struct vb2_buffer
*vb
)
680 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
682 dbg("ctx: %p, ctx->state: 0x%x", ctx
, ctx
->state
);
685 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, vb
);
688 static void fimc_lock(struct vb2_queue
*vq
)
690 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
691 mutex_lock(&ctx
->fimc_dev
->lock
);
694 static void fimc_unlock(struct vb2_queue
*vq
)
696 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
697 mutex_unlock(&ctx
->fimc_dev
->lock
);
700 struct vb2_ops fimc_qops
= {
701 .queue_setup
= fimc_queue_setup
,
702 .buf_prepare
= fimc_buf_prepare
,
703 .buf_queue
= fimc_buf_queue
,
704 .wait_prepare
= fimc_unlock
,
705 .wait_finish
= fimc_lock
,
708 static int fimc_m2m_querycap(struct file
*file
, void *priv
,
709 struct v4l2_capability
*cap
)
711 struct fimc_ctx
*ctx
= file
->private_data
;
712 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
714 strncpy(cap
->driver
, fimc
->pdev
->name
, sizeof(cap
->driver
) - 1);
715 strncpy(cap
->card
, fimc
->pdev
->name
, sizeof(cap
->card
) - 1);
716 cap
->bus_info
[0] = 0;
717 cap
->version
= KERNEL_VERSION(1, 0, 0);
718 cap
->capabilities
= V4L2_CAP_STREAMING
|
719 V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_VIDEO_OUTPUT
|
720 V4L2_CAP_VIDEO_CAPTURE_MPLANE
| V4L2_CAP_VIDEO_OUTPUT_MPLANE
;
725 int fimc_vidioc_enum_fmt_mplane(struct file
*file
, void *priv
,
726 struct v4l2_fmtdesc
*f
)
728 struct fimc_fmt
*fmt
;
730 if (f
->index
>= ARRAY_SIZE(fimc_formats
))
733 fmt
= &fimc_formats
[f
->index
];
734 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
735 f
->pixelformat
= fmt
->fourcc
;
740 int fimc_vidioc_g_fmt_mplane(struct file
*file
, void *priv
,
741 struct v4l2_format
*f
)
743 struct fimc_ctx
*ctx
= priv
;
744 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
745 struct fimc_frame
*frame
;
747 frame
= ctx_get_frame(ctx
, f
->type
);
749 return PTR_ERR(frame
);
751 if (mutex_lock_interruptible(&fimc
->lock
))
754 f
->fmt
.pix
.width
= frame
->width
;
755 f
->fmt
.pix
.height
= frame
->height
;
756 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
757 f
->fmt
.pix
.pixelformat
= frame
->fmt
->fourcc
;
759 mutex_unlock(&fimc
->lock
);
763 struct fimc_fmt
*find_format(struct v4l2_format
*f
, unsigned int mask
)
765 struct fimc_fmt
*fmt
;
768 for (i
= 0; i
< ARRAY_SIZE(fimc_formats
); ++i
) {
769 fmt
= &fimc_formats
[i
];
770 if (fmt
->fourcc
== f
->fmt
.pix
.pixelformat
&&
775 return (i
== ARRAY_SIZE(fimc_formats
)) ? NULL
: fmt
;
778 struct fimc_fmt
*find_mbus_format(struct v4l2_mbus_framefmt
*f
,
781 struct fimc_fmt
*fmt
;
784 for (i
= 0; i
< ARRAY_SIZE(fimc_formats
); ++i
) {
785 fmt
= &fimc_formats
[i
];
786 if (fmt
->mbus_code
== f
->code
&& (fmt
->flags
& mask
))
790 return (i
== ARRAY_SIZE(fimc_formats
)) ? NULL
: fmt
;
794 int fimc_vidioc_try_fmt_mplane(struct file
*file
, void *priv
,
795 struct v4l2_format
*f
)
797 struct fimc_ctx
*ctx
= priv
;
798 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
799 struct samsung_fimc_variant
*variant
= fimc
->variant
;
800 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
801 struct fimc_fmt
*fmt
;
802 u32 max_width
, mod_x
, mod_y
, mask
;
803 int ret
, i
, is_output
= 0;
805 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
806 if (ctx
->state
& FIMC_CTX_CAP
)
809 } else if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
813 if (mutex_lock_interruptible(&fimc
->lock
))
815 dbg("w: %d, h: %d", pix
->width
, pix
->height
);
817 mask
= is_output
? FMT_FLAGS_M2M
: FMT_FLAGS_M2M
| FMT_FLAGS_CAM
;
818 fmt
= find_format(f
, mask
);
820 v4l2_err(&fimc
->m2m
.v4l2_dev
, "Fourcc format (0x%X) invalid.\n",
825 if (pix
->field
== V4L2_FIELD_ANY
)
826 pix
->field
= V4L2_FIELD_NONE
;
827 else if (V4L2_FIELD_NONE
!= pix
->field
)
831 max_width
= variant
->pix_limit
->scaler_dis_w
;
832 mod_x
= ffs(variant
->min_inp_pixsize
) - 1;
834 max_width
= variant
->pix_limit
->out_rot_dis_w
;
835 mod_x
= ffs(variant
->min_out_pixsize
) - 1;
838 if (tiled_fmt(fmt
)) {
839 mod_x
= 6; /* 64 x 32 pixels tile */
842 if (fimc
->id
== 1 && variant
->pix_hoff
)
843 mod_y
= fimc_fmt_is_rgb(fmt
->color
) ? 0 : 1;
848 dbg("mod_x: %d, mod_y: %d, max_w: %d", mod_x
, mod_y
, max_width
);
850 v4l_bound_align_image(&pix
->width
, 16, max_width
, mod_x
,
851 &pix
->height
, 8, variant
->pix_limit
->scaler_dis_w
, mod_y
, 0);
853 pix
->num_planes
= fmt
->memplanes
;
855 for (i
= 0; i
< pix
->num_planes
; ++i
) {
856 int bpl
= pix
->plane_fmt
[i
].bytesperline
;
858 dbg("[%d] bpl: %d, depth: %d, w: %d, h: %d",
859 i
, bpl
, fmt
->depth
[i
], pix
->width
, pix
->height
);
861 if (!bpl
|| (bpl
* 8 / fmt
->depth
[i
]) > pix
->width
)
862 bpl
= (pix
->width
* fmt
->depth
[0]) >> 3;
864 if (!pix
->plane_fmt
[i
].sizeimage
)
865 pix
->plane_fmt
[i
].sizeimage
= pix
->height
* bpl
;
867 pix
->plane_fmt
[i
].bytesperline
= bpl
;
869 dbg("[%d]: bpl: %d, sizeimage: %d",
870 i
, pix
->plane_fmt
[i
].bytesperline
,
871 pix
->plane_fmt
[i
].sizeimage
);
877 mutex_unlock(&fimc
->lock
);
881 static int fimc_m2m_s_fmt_mplane(struct file
*file
, void *priv
,
882 struct v4l2_format
*f
)
884 struct fimc_ctx
*ctx
= priv
;
885 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
886 struct vb2_queue
*vq
;
887 struct fimc_frame
*frame
;
888 struct v4l2_pix_format_mplane
*pix
;
892 ret
= fimc_vidioc_try_fmt_mplane(file
, priv
, f
);
896 if (mutex_lock_interruptible(&fimc
->lock
))
899 vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
, f
->type
);
901 if (vb2_is_streaming(vq
)) {
902 v4l2_err(&fimc
->vid_cap
.v4l2_dev
, "%s: queue (%d) busy\n",
908 spin_lock_irqsave(&ctx
->slock
, flags
);
909 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
910 frame
= &ctx
->s_frame
;
911 ctx
->state
|= FIMC_SRC_FMT
;
912 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
913 frame
= &ctx
->d_frame
;
914 ctx
->state
|= FIMC_DST_FMT
;
916 spin_unlock_irqrestore(&ctx
->slock
, flags
);
917 v4l2_err(&fimc
->m2m
.v4l2_dev
,
918 "Wrong buffer/video queue type (%d)\n", f
->type
);
922 spin_unlock_irqrestore(&ctx
->slock
, flags
);
924 pix
= &f
->fmt
.pix_mp
;
925 frame
->fmt
= find_format(f
, FMT_FLAGS_M2M
);
931 for (i
= 0; i
< frame
->fmt
->colplanes
; i
++)
932 frame
->payload
[i
] = pix
->plane_fmt
[i
].bytesperline
* pix
->height
;
934 frame
->f_width
= pix
->plane_fmt
[0].bytesperline
* 8 /
935 frame
->fmt
->depth
[0];
936 frame
->f_height
= pix
->height
;
937 frame
->width
= pix
->width
;
938 frame
->height
= pix
->height
;
939 frame
->o_width
= pix
->width
;
940 frame
->o_height
= pix
->height
;
944 spin_lock_irqsave(&ctx
->slock
, flags
);
945 ctx
->state
|= FIMC_PARAMS
;
946 spin_unlock_irqrestore(&ctx
->slock
, flags
);
948 dbg("f_w: %d, f_h: %d", frame
->f_width
, frame
->f_height
);
951 mutex_unlock(&fimc
->lock
);
955 static int fimc_m2m_reqbufs(struct file
*file
, void *priv
,
956 struct v4l2_requestbuffers
*reqbufs
)
958 struct fimc_ctx
*ctx
= priv
;
959 return v4l2_m2m_reqbufs(file
, ctx
->m2m_ctx
, reqbufs
);
962 static int fimc_m2m_querybuf(struct file
*file
, void *priv
,
963 struct v4l2_buffer
*buf
)
965 struct fimc_ctx
*ctx
= priv
;
966 return v4l2_m2m_querybuf(file
, ctx
->m2m_ctx
, buf
);
969 static int fimc_m2m_qbuf(struct file
*file
, void *priv
,
970 struct v4l2_buffer
*buf
)
972 struct fimc_ctx
*ctx
= priv
;
974 return v4l2_m2m_qbuf(file
, ctx
->m2m_ctx
, buf
);
977 static int fimc_m2m_dqbuf(struct file
*file
, void *priv
,
978 struct v4l2_buffer
*buf
)
980 struct fimc_ctx
*ctx
= priv
;
981 return v4l2_m2m_dqbuf(file
, ctx
->m2m_ctx
, buf
);
984 static int fimc_m2m_streamon(struct file
*file
, void *priv
,
985 enum v4l2_buf_type type
)
987 struct fimc_ctx
*ctx
= priv
;
989 /* The source and target color format need to be set */
990 if (V4L2_TYPE_IS_OUTPUT(type
)) {
991 if (~ctx
->state
& FIMC_SRC_FMT
)
993 } else if (~ctx
->state
& FIMC_DST_FMT
) {
997 return v4l2_m2m_streamon(file
, ctx
->m2m_ctx
, type
);
1000 static int fimc_m2m_streamoff(struct file
*file
, void *priv
,
1001 enum v4l2_buf_type type
)
1003 struct fimc_ctx
*ctx
= priv
;
1004 return v4l2_m2m_streamoff(file
, ctx
->m2m_ctx
, type
);
1007 int fimc_vidioc_queryctrl(struct file
*file
, void *priv
,
1008 struct v4l2_queryctrl
*qc
)
1010 struct fimc_ctx
*ctx
= priv
;
1011 struct v4l2_queryctrl
*c
;
1014 c
= get_ctrl(qc
->id
);
1020 if (ctx
->state
& FIMC_CTX_CAP
) {
1021 if (mutex_lock_interruptible(&ctx
->fimc_dev
->lock
))
1022 return -ERESTARTSYS
;
1023 ret
= v4l2_subdev_call(ctx
->fimc_dev
->vid_cap
.sd
,
1024 core
, queryctrl
, qc
);
1025 mutex_unlock(&ctx
->fimc_dev
->lock
);
1030 int fimc_vidioc_g_ctrl(struct file
*file
, void *priv
,
1031 struct v4l2_control
*ctrl
)
1033 struct fimc_ctx
*ctx
= priv
;
1034 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1037 if (mutex_lock_interruptible(&fimc
->lock
))
1038 return -ERESTARTSYS
;
1041 case V4L2_CID_HFLIP
:
1042 ctrl
->value
= (FLIP_X_AXIS
& ctx
->flip
) ? 1 : 0;
1044 case V4L2_CID_VFLIP
:
1045 ctrl
->value
= (FLIP_Y_AXIS
& ctx
->flip
) ? 1 : 0;
1047 case V4L2_CID_ROTATE
:
1048 ctrl
->value
= ctx
->rotation
;
1051 if (ctx
->state
& FIMC_CTX_CAP
) {
1052 ret
= v4l2_subdev_call(fimc
->vid_cap
.sd
, core
,
1055 v4l2_err(&fimc
->m2m
.v4l2_dev
,
1056 "Invalid control\n");
1060 dbg("ctrl->value= %d", ctrl
->value
);
1062 mutex_unlock(&fimc
->lock
);
1066 int check_ctrl_val(struct fimc_ctx
*ctx
, struct v4l2_control
*ctrl
)
1068 struct v4l2_queryctrl
*c
;
1069 c
= get_ctrl(ctrl
->id
);
1073 if (ctrl
->value
< c
->minimum
|| ctrl
->value
> c
->maximum
1074 || (c
->step
!= 0 && ctrl
->value
% c
->step
!= 0)) {
1075 v4l2_err(&ctx
->fimc_dev
->m2m
.v4l2_dev
,
1076 "Invalid control value\n");
1083 int fimc_s_ctrl(struct fimc_ctx
*ctx
, struct v4l2_control
*ctrl
)
1085 struct samsung_fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
1086 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1087 unsigned long flags
;
1089 if (ctx
->rotation
!= 0 &&
1090 (ctrl
->id
== V4L2_CID_HFLIP
|| ctrl
->id
== V4L2_CID_VFLIP
)) {
1091 v4l2_err(&fimc
->m2m
.v4l2_dev
,
1092 "Simultaneous flip and rotation is not supported\n");
1096 spin_lock_irqsave(&ctx
->slock
, flags
);
1099 case V4L2_CID_HFLIP
:
1101 ctx
->flip
|= FLIP_X_AXIS
;
1103 ctx
->flip
&= ~FLIP_X_AXIS
;
1106 case V4L2_CID_VFLIP
:
1108 ctx
->flip
|= FLIP_Y_AXIS
;
1110 ctx
->flip
&= ~FLIP_Y_AXIS
;
1113 case V4L2_CID_ROTATE
:
1114 /* Check for the output rotator availability */
1115 if ((ctrl
->value
== 90 || ctrl
->value
== 270) &&
1116 (ctx
->in_path
== FIMC_DMA
&& !variant
->has_out_rot
)) {
1117 spin_unlock_irqrestore(&ctx
->slock
, flags
);
1120 ctx
->rotation
= ctrl
->value
;
1125 spin_unlock_irqrestore(&ctx
->slock
, flags
);
1126 v4l2_err(&fimc
->m2m
.v4l2_dev
, "Invalid control\n");
1129 ctx
->state
|= FIMC_PARAMS
;
1130 spin_unlock_irqrestore(&ctx
->slock
, flags
);
1135 static int fimc_m2m_s_ctrl(struct file
*file
, void *priv
,
1136 struct v4l2_control
*ctrl
)
1138 struct fimc_ctx
*ctx
= priv
;
1141 ret
= check_ctrl_val(ctx
, ctrl
);
1145 ret
= fimc_s_ctrl(ctx
, ctrl
);
1149 static int fimc_m2m_cropcap(struct file
*file
, void *fh
,
1150 struct v4l2_cropcap
*cr
)
1152 struct fimc_frame
*frame
;
1153 struct fimc_ctx
*ctx
= fh
;
1154 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1156 frame
= ctx_get_frame(ctx
, cr
->type
);
1158 return PTR_ERR(frame
);
1160 if (mutex_lock_interruptible(&fimc
->lock
))
1161 return -ERESTARTSYS
;
1163 cr
->bounds
.left
= 0;
1165 cr
->bounds
.width
= frame
->f_width
;
1166 cr
->bounds
.height
= frame
->f_height
;
1167 cr
->defrect
= cr
->bounds
;
1169 mutex_unlock(&fimc
->lock
);
1173 static int fimc_m2m_g_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
1175 struct fimc_frame
*frame
;
1176 struct fimc_ctx
*ctx
= file
->private_data
;
1177 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1179 frame
= ctx_get_frame(ctx
, cr
->type
);
1181 return PTR_ERR(frame
);
1183 if (mutex_lock_interruptible(&fimc
->lock
))
1184 return -ERESTARTSYS
;
1186 cr
->c
.left
= frame
->offs_h
;
1187 cr
->c
.top
= frame
->offs_v
;
1188 cr
->c
.width
= frame
->width
;
1189 cr
->c
.height
= frame
->height
;
1191 mutex_unlock(&fimc
->lock
);
1195 int fimc_try_crop(struct fimc_ctx
*ctx
, struct v4l2_crop
*cr
)
1197 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1198 struct fimc_frame
*f
;
1199 u32 min_size
, halign
, depth
= 0;
1202 if (cr
->c
.top
< 0 || cr
->c
.left
< 0) {
1203 v4l2_err(&fimc
->m2m
.v4l2_dev
,
1204 "doesn't support negative values for top & left\n");
1208 if (cr
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1209 f
= (ctx
->state
& FIMC_CTX_CAP
) ? &ctx
->s_frame
: &ctx
->d_frame
;
1210 else if (cr
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
&&
1211 ctx
->state
& FIMC_CTX_M2M
)
1216 min_size
= (f
== &ctx
->s_frame
) ?
1217 fimc
->variant
->min_inp_pixsize
: fimc
->variant
->min_out_pixsize
;
1219 if (ctx
->state
& FIMC_CTX_M2M
) {
1220 if (fimc
->id
== 1 && fimc
->variant
->pix_hoff
)
1221 halign
= fimc_fmt_is_rgb(f
->fmt
->color
) ? 0 : 1;
1223 halign
= ffs(min_size
) - 1;
1224 /* there are more strict aligment requirements at camera interface */
1230 for (i
= 0; i
< f
->fmt
->colplanes
; i
++)
1231 depth
+= f
->fmt
->depth
[i
];
1233 v4l_bound_align_image(&cr
->c
.width
, min_size
, f
->o_width
,
1235 &cr
->c
.height
, min_size
, f
->o_height
,
1236 halign
, 64/(ALIGN(depth
, 8)));
1238 /* adjust left/top if cropping rectangle is out of bounds */
1239 if (cr
->c
.left
+ cr
->c
.width
> f
->o_width
)
1240 cr
->c
.left
= f
->o_width
- cr
->c
.width
;
1241 if (cr
->c
.top
+ cr
->c
.height
> f
->o_height
)
1242 cr
->c
.top
= f
->o_height
- cr
->c
.height
;
1244 cr
->c
.left
= round_down(cr
->c
.left
, min_size
);
1245 cr
->c
.top
= round_down(cr
->c
.top
,
1246 ctx
->state
& FIMC_CTX_M2M
? 8 : 16);
1248 dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
1249 cr
->c
.left
, cr
->c
.top
, cr
->c
.width
, cr
->c
.height
,
1250 f
->f_width
, f
->f_height
);
1256 static int fimc_m2m_s_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
1258 struct fimc_ctx
*ctx
= file
->private_data
;
1259 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1260 unsigned long flags
;
1261 struct fimc_frame
*f
;
1264 ret
= fimc_try_crop(ctx
, cr
);
1268 f
= (cr
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) ?
1269 &ctx
->s_frame
: &ctx
->d_frame
;
1271 if (mutex_lock_interruptible(&fimc
->lock
))
1272 return -ERESTARTSYS
;
1274 spin_lock_irqsave(&ctx
->slock
, flags
);
1275 if (~ctx
->state
& (FIMC_SRC_FMT
| FIMC_DST_FMT
)) {
1276 /* Check to see if scaling ratio is within supported range */
1277 if (cr
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
1278 ret
= fimc_check_scaler_ratio(&cr
->c
, &ctx
->d_frame
);
1280 ret
= fimc_check_scaler_ratio(&cr
->c
, &ctx
->s_frame
);
1282 v4l2_err(&fimc
->m2m
.v4l2_dev
, "Out of scaler range");
1287 ctx
->state
|= FIMC_PARAMS
;
1289 f
->offs_h
= cr
->c
.left
;
1290 f
->offs_v
= cr
->c
.top
;
1291 f
->width
= cr
->c
.width
;
1292 f
->height
= cr
->c
.height
;
1295 spin_unlock_irqrestore(&ctx
->slock
, flags
);
1296 mutex_unlock(&fimc
->lock
);
1300 static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops
= {
1301 .vidioc_querycap
= fimc_m2m_querycap
,
1303 .vidioc_enum_fmt_vid_cap_mplane
= fimc_vidioc_enum_fmt_mplane
,
1304 .vidioc_enum_fmt_vid_out_mplane
= fimc_vidioc_enum_fmt_mplane
,
1306 .vidioc_g_fmt_vid_cap_mplane
= fimc_vidioc_g_fmt_mplane
,
1307 .vidioc_g_fmt_vid_out_mplane
= fimc_vidioc_g_fmt_mplane
,
1309 .vidioc_try_fmt_vid_cap_mplane
= fimc_vidioc_try_fmt_mplane
,
1310 .vidioc_try_fmt_vid_out_mplane
= fimc_vidioc_try_fmt_mplane
,
1312 .vidioc_s_fmt_vid_cap_mplane
= fimc_m2m_s_fmt_mplane
,
1313 .vidioc_s_fmt_vid_out_mplane
= fimc_m2m_s_fmt_mplane
,
1315 .vidioc_reqbufs
= fimc_m2m_reqbufs
,
1316 .vidioc_querybuf
= fimc_m2m_querybuf
,
1318 .vidioc_qbuf
= fimc_m2m_qbuf
,
1319 .vidioc_dqbuf
= fimc_m2m_dqbuf
,
1321 .vidioc_streamon
= fimc_m2m_streamon
,
1322 .vidioc_streamoff
= fimc_m2m_streamoff
,
1324 .vidioc_queryctrl
= fimc_vidioc_queryctrl
,
1325 .vidioc_g_ctrl
= fimc_vidioc_g_ctrl
,
1326 .vidioc_s_ctrl
= fimc_m2m_s_ctrl
,
1328 .vidioc_g_crop
= fimc_m2m_g_crop
,
1329 .vidioc_s_crop
= fimc_m2m_s_crop
,
1330 .vidioc_cropcap
= fimc_m2m_cropcap
1334 static int queue_init(void *priv
, struct vb2_queue
*src_vq
,
1335 struct vb2_queue
*dst_vq
)
1337 struct fimc_ctx
*ctx
= priv
;
1340 memset(src_vq
, 0, sizeof(*src_vq
));
1341 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
1342 src_vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1343 src_vq
->drv_priv
= ctx
;
1344 src_vq
->ops
= &fimc_qops
;
1345 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
1346 src_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
1348 ret
= vb2_queue_init(src_vq
);
1352 memset(dst_vq
, 0, sizeof(*dst_vq
));
1353 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1354 dst_vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1355 dst_vq
->drv_priv
= ctx
;
1356 dst_vq
->ops
= &fimc_qops
;
1357 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1358 dst_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
1360 return vb2_queue_init(dst_vq
);
1363 static int fimc_m2m_open(struct file
*file
)
1365 struct fimc_dev
*fimc
= video_drvdata(file
);
1366 struct fimc_ctx
*ctx
= NULL
;
1369 if (mutex_lock_interruptible(&fimc
->lock
))
1370 return -ERESTARTSYS
;
1372 dbg("pid: %d, state: 0x%lx, refcnt: %d",
1373 task_pid_nr(current
), fimc
->state
, fimc
->vid_cap
.refcnt
);
1376 * Return if the corresponding video capture node
1377 * is already opened.
1379 if (fimc
->vid_cap
.refcnt
> 0) {
1385 set_bit(ST_OUTDMA_RUN
, &fimc
->state
);
1387 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
1393 file
->private_data
= ctx
;
1394 ctx
->fimc_dev
= fimc
;
1395 /* Default color format */
1396 ctx
->s_frame
.fmt
= &fimc_formats
[0];
1397 ctx
->d_frame
.fmt
= &fimc_formats
[0];
1398 /* Setup the device context for mem2mem mode. */
1399 ctx
->state
= FIMC_CTX_M2M
;
1401 ctx
->in_path
= FIMC_DMA
;
1402 ctx
->out_path
= FIMC_DMA
;
1403 spin_lock_init(&ctx
->slock
);
1405 ctx
->m2m_ctx
= v4l2_m2m_ctx_init(fimc
->m2m
.m2m_dev
, ctx
, queue_init
);
1406 if (IS_ERR(ctx
->m2m_ctx
)) {
1407 err
= PTR_ERR(ctx
->m2m_ctx
);
1412 mutex_unlock(&fimc
->lock
);
1416 static int fimc_m2m_release(struct file
*file
)
1418 struct fimc_ctx
*ctx
= file
->private_data
;
1419 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1421 mutex_lock(&fimc
->lock
);
1423 dbg("pid: %d, state: 0x%lx, refcnt= %d",
1424 task_pid_nr(current
), fimc
->state
, fimc
->m2m
.refcnt
);
1426 v4l2_m2m_ctx_release(ctx
->m2m_ctx
);
1428 if (--fimc
->m2m
.refcnt
<= 0)
1429 clear_bit(ST_OUTDMA_RUN
, &fimc
->state
);
1431 mutex_unlock(&fimc
->lock
);
1435 static unsigned int fimc_m2m_poll(struct file
*file
,
1436 struct poll_table_struct
*wait
)
1438 struct fimc_ctx
*ctx
= file
->private_data
;
1440 return v4l2_m2m_poll(file
, ctx
->m2m_ctx
, wait
);
1444 static int fimc_m2m_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1446 struct fimc_ctx
*ctx
= file
->private_data
;
1448 return v4l2_m2m_mmap(file
, ctx
->m2m_ctx
, vma
);
1451 static const struct v4l2_file_operations fimc_m2m_fops
= {
1452 .owner
= THIS_MODULE
,
1453 .open
= fimc_m2m_open
,
1454 .release
= fimc_m2m_release
,
1455 .poll
= fimc_m2m_poll
,
1456 .unlocked_ioctl
= video_ioctl2
,
1457 .mmap
= fimc_m2m_mmap
,
1460 static struct v4l2_m2m_ops m2m_ops
= {
1461 .device_run
= fimc_dma_run
,
1462 .job_abort
= fimc_job_abort
,
1465 static int fimc_register_m2m_device(struct fimc_dev
*fimc
)
1467 struct video_device
*vfd
;
1468 struct platform_device
*pdev
;
1469 struct v4l2_device
*v4l2_dev
;
1476 v4l2_dev
= &fimc
->m2m
.v4l2_dev
;
1478 /* set name if it is empty */
1479 if (!v4l2_dev
->name
[0])
1480 snprintf(v4l2_dev
->name
, sizeof(v4l2_dev
->name
),
1481 "%s.m2m", dev_name(&pdev
->dev
));
1483 ret
= v4l2_device_register(&pdev
->dev
, v4l2_dev
);
1487 vfd
= video_device_alloc();
1489 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
1493 vfd
->fops
= &fimc_m2m_fops
;
1494 vfd
->ioctl_ops
= &fimc_m2m_ioctl_ops
;
1496 vfd
->release
= video_device_release
;
1498 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s:m2m", dev_name(&pdev
->dev
));
1500 video_set_drvdata(vfd
, fimc
);
1501 platform_set_drvdata(pdev
, fimc
);
1503 fimc
->m2m
.vfd
= vfd
;
1504 fimc
->m2m
.m2m_dev
= v4l2_m2m_init(&m2m_ops
);
1505 if (IS_ERR(fimc
->m2m
.m2m_dev
)) {
1506 v4l2_err(v4l2_dev
, "failed to initialize v4l2-m2m device\n");
1507 ret
= PTR_ERR(fimc
->m2m
.m2m_dev
);
1511 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, -1);
1514 "%s(): failed to register video device\n", __func__
);
1518 "FIMC m2m driver registered as /dev/video%d\n", vfd
->num
);
1523 v4l2_m2m_release(fimc
->m2m
.m2m_dev
);
1525 video_device_release(fimc
->m2m
.vfd
);
1527 v4l2_device_unregister(v4l2_dev
);
1532 static void fimc_unregister_m2m_device(struct fimc_dev
*fimc
)
1535 v4l2_m2m_release(fimc
->m2m
.m2m_dev
);
1536 video_unregister_device(fimc
->m2m
.vfd
);
1538 v4l2_device_unregister(&fimc
->m2m
.v4l2_dev
);
1542 static void fimc_clk_release(struct fimc_dev
*fimc
)
1545 for (i
= 0; i
< NUM_FIMC_CLOCKS
; i
++) {
1546 if (fimc
->clock
[i
]) {
1547 clk_disable(fimc
->clock
[i
]);
1548 clk_put(fimc
->clock
[i
]);
1553 static int fimc_clk_get(struct fimc_dev
*fimc
)
1556 for (i
= 0; i
< NUM_FIMC_CLOCKS
; i
++) {
1557 fimc
->clock
[i
] = clk_get(&fimc
->pdev
->dev
, fimc_clock_name
[i
]);
1558 if (IS_ERR(fimc
->clock
[i
])) {
1559 dev_err(&fimc
->pdev
->dev
,
1560 "failed to get fimc clock: %s\n",
1561 fimc_clock_name
[i
]);
1564 clk_enable(fimc
->clock
[i
]);
1569 static int fimc_probe(struct platform_device
*pdev
)
1571 struct fimc_dev
*fimc
;
1572 struct resource
*res
;
1573 struct samsung_fimc_driverdata
*drv_data
;
1576 dev_dbg(&pdev
->dev
, "%s():\n", __func__
);
1578 drv_data
= (struct samsung_fimc_driverdata
*)
1579 platform_get_device_id(pdev
)->driver_data
;
1581 if (pdev
->id
>= drv_data
->num_entities
) {
1582 dev_err(&pdev
->dev
, "Invalid platform device id: %d\n",
1587 fimc
= kzalloc(sizeof(struct fimc_dev
), GFP_KERNEL
);
1591 fimc
->id
= pdev
->id
;
1592 fimc
->variant
= drv_data
->variant
[fimc
->id
];
1594 fimc
->pdata
= pdev
->dev
.platform_data
;
1595 fimc
->state
= ST_IDLE
;
1597 init_waitqueue_head(&fimc
->irq_queue
);
1598 spin_lock_init(&fimc
->slock
);
1600 mutex_init(&fimc
->lock
);
1602 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1604 dev_err(&pdev
->dev
, "failed to find the registers\n");
1609 fimc
->regs_res
= request_mem_region(res
->start
, resource_size(res
),
1610 dev_name(&pdev
->dev
));
1611 if (!fimc
->regs_res
) {
1612 dev_err(&pdev
->dev
, "failed to obtain register region\n");
1617 fimc
->regs
= ioremap(res
->start
, resource_size(res
));
1619 dev_err(&pdev
->dev
, "failed to map registers\n");
1621 goto err_req_region
;
1624 ret
= fimc_clk_get(fimc
);
1626 goto err_regs_unmap
;
1627 clk_set_rate(fimc
->clock
[0], drv_data
->lclk_frequency
);
1629 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1631 dev_err(&pdev
->dev
, "failed to get IRQ resource\n");
1635 fimc
->irq
= res
->start
;
1637 fimc_hw_reset(fimc
);
1639 ret
= request_irq(fimc
->irq
, fimc_isr
, 0, pdev
->name
, fimc
);
1641 dev_err(&pdev
->dev
, "failed to install irq (%d)\n", ret
);
1645 /* Initialize contiguous memory allocator */
1646 fimc
->alloc_ctx
= vb2_dma_contig_init_ctx(&fimc
->pdev
->dev
);
1647 if (IS_ERR(fimc
->alloc_ctx
)) {
1648 ret
= PTR_ERR(fimc
->alloc_ctx
);
1652 ret
= fimc_register_m2m_device(fimc
);
1656 /* At least one camera sensor is required to register capture node */
1659 for (i
= 0; i
< FIMC_MAX_CAMIF_CLIENTS
; ++i
)
1660 if (fimc
->pdata
->isp_info
[i
])
1663 if (i
< FIMC_MAX_CAMIF_CLIENTS
) {
1664 ret
= fimc_register_capture_device(fimc
);
1671 * Exclude the additional output DMA address registers by masking
1672 * them out on HW revisions that provide extended capabilites.
1674 if (fimc
->variant
->out_buf_count
> 4)
1675 fimc_hw_set_dma_seq(fimc
, 0xF);
1677 dev_dbg(&pdev
->dev
, "%s(): fimc-%d registered successfully\n",
1678 __func__
, fimc
->id
);
1683 fimc_unregister_m2m_device(fimc
);
1685 free_irq(fimc
->irq
, fimc
);
1687 fimc_clk_release(fimc
);
1689 iounmap(fimc
->regs
);
1691 release_resource(fimc
->regs_res
);
1692 kfree(fimc
->regs_res
);
1699 static int __devexit
fimc_remove(struct platform_device
*pdev
)
1701 struct fimc_dev
*fimc
=
1702 (struct fimc_dev
*)platform_get_drvdata(pdev
);
1704 free_irq(fimc
->irq
, fimc
);
1705 fimc_hw_reset(fimc
);
1707 fimc_unregister_m2m_device(fimc
);
1708 fimc_unregister_capture_device(fimc
);
1710 fimc_clk_release(fimc
);
1712 vb2_dma_contig_cleanup_ctx(fimc
->alloc_ctx
);
1714 iounmap(fimc
->regs
);
1715 release_resource(fimc
->regs_res
);
1716 kfree(fimc
->regs_res
);
1719 dev_info(&pdev
->dev
, "%s driver unloaded\n", pdev
->name
);
1723 /* Image pixel limits, similar across several FIMC HW revisions. */
1724 static struct fimc_pix_limit s5p_pix_limit
[3] = {
1726 .scaler_en_w
= 3264,
1727 .scaler_dis_w
= 8192,
1728 .in_rot_en_h
= 1920,
1729 .in_rot_dis_w
= 8192,
1730 .out_rot_en_w
= 1920,
1731 .out_rot_dis_w
= 4224,
1734 .scaler_en_w
= 4224,
1735 .scaler_dis_w
= 8192,
1736 .in_rot_en_h
= 1920,
1737 .in_rot_dis_w
= 8192,
1738 .out_rot_en_w
= 1920,
1739 .out_rot_dis_w
= 4224,
1742 .scaler_en_w
= 1920,
1743 .scaler_dis_w
= 8192,
1744 .in_rot_en_h
= 1280,
1745 .in_rot_dis_w
= 8192,
1746 .out_rot_en_w
= 1280,
1747 .out_rot_dis_w
= 1920,
1751 static struct samsung_fimc_variant fimc0_variant_s5p
= {
1754 .min_inp_pixsize
= 16,
1755 .min_out_pixsize
= 16,
1756 .hor_offs_align
= 8,
1758 .pix_limit
= &s5p_pix_limit
[0],
1761 static struct samsung_fimc_variant fimc2_variant_s5p
= {
1762 .min_inp_pixsize
= 16,
1763 .min_out_pixsize
= 16,
1764 .hor_offs_align
= 8,
1766 .pix_limit
= &s5p_pix_limit
[1],
1769 static struct samsung_fimc_variant fimc0_variant_s5pv210
= {
1773 .min_inp_pixsize
= 16,
1774 .min_out_pixsize
= 16,
1775 .hor_offs_align
= 8,
1777 .pix_limit
= &s5p_pix_limit
[1],
1780 static struct samsung_fimc_variant fimc1_variant_s5pv210
= {
1784 .min_inp_pixsize
= 16,
1785 .min_out_pixsize
= 16,
1786 .hor_offs_align
= 1,
1788 .pix_limit
= &s5p_pix_limit
[2],
1791 static struct samsung_fimc_variant fimc2_variant_s5pv210
= {
1793 .min_inp_pixsize
= 16,
1794 .min_out_pixsize
= 16,
1795 .hor_offs_align
= 8,
1797 .pix_limit
= &s5p_pix_limit
[2],
1800 static struct samsung_fimc_variant fimc0_variant_s5pv310
= {
1805 .min_inp_pixsize
= 16,
1806 .min_out_pixsize
= 16,
1807 .hor_offs_align
= 1,
1808 .out_buf_count
= 32,
1809 .pix_limit
= &s5p_pix_limit
[1],
1812 static struct samsung_fimc_variant fimc2_variant_s5pv310
= {
1815 .min_inp_pixsize
= 16,
1816 .min_out_pixsize
= 16,
1817 .hor_offs_align
= 1,
1818 .out_buf_count
= 32,
1819 .pix_limit
= &s5p_pix_limit
[2],
1823 static struct samsung_fimc_driverdata fimc_drvdata_s5p
= {
1825 [0] = &fimc0_variant_s5p
,
1826 [1] = &fimc0_variant_s5p
,
1827 [2] = &fimc2_variant_s5p
,
1830 .lclk_frequency
= 133000000UL,
1833 /* S5PV210, S5PC110 */
1834 static struct samsung_fimc_driverdata fimc_drvdata_s5pv210
= {
1836 [0] = &fimc0_variant_s5pv210
,
1837 [1] = &fimc1_variant_s5pv210
,
1838 [2] = &fimc2_variant_s5pv210
,
1841 .lclk_frequency
= 166000000UL,
1844 /* S5PV310, S5PC210 */
1845 static struct samsung_fimc_driverdata fimc_drvdata_s5pv310
= {
1847 [0] = &fimc0_variant_s5pv310
,
1848 [1] = &fimc0_variant_s5pv310
,
1849 [2] = &fimc0_variant_s5pv310
,
1850 [3] = &fimc2_variant_s5pv310
,
1853 .lclk_frequency
= 166000000UL,
1856 static struct platform_device_id fimc_driver_ids
[] = {
1859 .driver_data
= (unsigned long)&fimc_drvdata_s5p
,
1861 .name
= "s5pv210-fimc",
1862 .driver_data
= (unsigned long)&fimc_drvdata_s5pv210
,
1864 .name
= "s5pv310-fimc",
1865 .driver_data
= (unsigned long)&fimc_drvdata_s5pv310
,
1869 MODULE_DEVICE_TABLE(platform
, fimc_driver_ids
);
1871 static struct platform_driver fimc_driver
= {
1872 .probe
= fimc_probe
,
1873 .remove
= __devexit_p(fimc_remove
),
1874 .id_table
= fimc_driver_ids
,
1876 .name
= MODULE_NAME
,
1877 .owner
= THIS_MODULE
,
1881 static int __init
fimc_init(void)
1883 int ret
= platform_driver_register(&fimc_driver
);
1885 err("platform_driver_register failed: %d\n", ret
);
1889 static void __exit
fimc_exit(void)
1891 platform_driver_unregister(&fimc_driver
);
1894 module_init(fimc_init
);
1895 module_exit(fimc_exit
);
1897 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1898 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1899 MODULE_LICENSE("GPL");