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/videobuf-dma-contig.h>
30 #include "fimc-core.h"
32 static char *fimc_clock_name
[NUM_FIMC_CLOCKS
] = { "sclk_fimc", "fimc" };
34 static struct fimc_fmt fimc_formats
[] = {
37 .fourcc
= V4L2_PIX_FMT_RGB565X
,
39 .color
= S5P_FIMC_RGB565
,
42 .mbus_code
= V4L2_MBUS_FMT_RGB565_2X8_BE
,
43 .flags
= FMT_FLAGS_M2M
,
46 .fourcc
= V4L2_PIX_FMT_BGR666
,
48 .color
= S5P_FIMC_RGB666
,
51 .flags
= FMT_FLAGS_M2M
,
53 .name
= "XRGB-8-8-8-8, 24 bpp",
54 .fourcc
= V4L2_PIX_FMT_RGB24
,
56 .color
= S5P_FIMC_RGB888
,
59 .flags
= FMT_FLAGS_M2M
,
61 .name
= "YUV 4:2:2 packed, YCbYCr",
62 .fourcc
= V4L2_PIX_FMT_YUYV
,
64 .color
= S5P_FIMC_YCBYCR422
,
67 .mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
,
68 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
70 .name
= "YUV 4:2:2 packed, CbYCrY",
71 .fourcc
= V4L2_PIX_FMT_UYVY
,
73 .color
= S5P_FIMC_CBYCRY422
,
76 .mbus_code
= V4L2_MBUS_FMT_UYVY8_2X8
,
77 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
79 .name
= "YUV 4:2:2 packed, CrYCbY",
80 .fourcc
= V4L2_PIX_FMT_VYUY
,
82 .color
= S5P_FIMC_CRYCBY422
,
85 .mbus_code
= V4L2_MBUS_FMT_VYUY8_2X8
,
86 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
88 .name
= "YUV 4:2:2 packed, YCrYCb",
89 .fourcc
= V4L2_PIX_FMT_YVYU
,
91 .color
= S5P_FIMC_YCRYCB422
,
94 .mbus_code
= V4L2_MBUS_FMT_YVYU8_2X8
,
95 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
97 .name
= "YUV 4:2:2 planar, Y/Cb/Cr",
98 .fourcc
= V4L2_PIX_FMT_YUV422P
,
100 .color
= S5P_FIMC_YCBCR422
,
103 .flags
= FMT_FLAGS_M2M
,
105 .name
= "YUV 4:2:2 planar, Y/CbCr",
106 .fourcc
= V4L2_PIX_FMT_NV16
,
108 .color
= S5P_FIMC_YCBCR422
,
111 .flags
= FMT_FLAGS_M2M
,
113 .name
= "YUV 4:2:2 planar, Y/CrCb",
114 .fourcc
= V4L2_PIX_FMT_NV61
,
116 .color
= S5P_FIMC_RGB565
,
119 .flags
= FMT_FLAGS_M2M
,
121 .name
= "YUV 4:2:0 planar, YCbCr",
122 .fourcc
= V4L2_PIX_FMT_YUV420
,
124 .color
= S5P_FIMC_YCBCR420
,
127 .flags
= FMT_FLAGS_M2M
,
129 .name
= "YUV 4:2:0 planar, Y/CbCr",
130 .fourcc
= V4L2_PIX_FMT_NV12
,
132 .color
= S5P_FIMC_YCBCR420
,
135 .flags
= FMT_FLAGS_M2M
,
139 static struct v4l2_queryctrl fimc_ctrls
[] = {
141 .id
= V4L2_CID_HFLIP
,
142 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
143 .name
= "Horizontal flip",
148 .id
= V4L2_CID_VFLIP
,
149 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
150 .name
= "Vertical flip",
155 .id
= V4L2_CID_ROTATE
,
156 .type
= V4L2_CTRL_TYPE_INTEGER
,
157 .name
= "Rotation (CCW)",
166 static struct v4l2_queryctrl
*get_ctrl(int id
)
170 for (i
= 0; i
< ARRAY_SIZE(fimc_ctrls
); ++i
)
171 if (id
== fimc_ctrls
[i
].id
)
172 return &fimc_ctrls
[i
];
176 int fimc_check_scaler_ratio(struct v4l2_rect
*r
, struct fimc_frame
*f
)
178 if (r
->width
> f
->width
) {
179 if (f
->width
> (r
->width
* SCALER_MAX_HRATIO
))
182 if ((f
->width
* SCALER_MAX_HRATIO
) < r
->width
)
186 if (r
->height
> f
->height
) {
187 if (f
->height
> (r
->height
* SCALER_MAX_VRATIO
))
190 if ((f
->height
* SCALER_MAX_VRATIO
) < r
->height
)
197 static int fimc_get_scaler_factor(u32 src
, u32 tar
, u32
*ratio
, u32
*shift
)
206 if (src
>= tar
* tmp
) {
207 *shift
= sh
, *ratio
= tmp
;
212 *shift
= 0, *ratio
= 1;
214 dbg("s: %d, t: %d, shift: %d, ratio: %d",
215 src
, tar
, *shift
, *ratio
);
219 int fimc_set_scaler_info(struct fimc_ctx
*ctx
)
221 struct fimc_scaler
*sc
= &ctx
->scaler
;
222 struct fimc_frame
*s_frame
= &ctx
->s_frame
;
223 struct fimc_frame
*d_frame
= &ctx
->d_frame
;
227 if (ctx
->rotation
== 90 || ctx
->rotation
== 270) {
229 tx
= d_frame
->height
;
232 ty
= d_frame
->height
;
234 if (tx
<= 0 || ty
<= 0) {
235 v4l2_err(&ctx
->fimc_dev
->m2m
.v4l2_dev
,
236 "invalid target size: %d x %d", tx
, ty
);
241 sy
= s_frame
->height
;
242 if (sx
<= 0 || sy
<= 0) {
243 err("invalid source size: %d x %d", sx
, sy
);
248 sc
->real_height
= sy
;
249 dbg("sx= %d, sy= %d, tx= %d, ty= %d", sx
, sy
, tx
, ty
);
251 ret
= fimc_get_scaler_factor(sx
, tx
, &sc
->pre_hratio
, &sc
->hfactor
);
255 ret
= fimc_get_scaler_factor(sy
, ty
, &sc
->pre_vratio
, &sc
->vfactor
);
259 sc
->pre_dst_width
= sx
/ sc
->pre_hratio
;
260 sc
->pre_dst_height
= sy
/ sc
->pre_vratio
;
262 sc
->main_hratio
= (sx
<< 8) / (tx
<< sc
->hfactor
);
263 sc
->main_vratio
= (sy
<< 8) / (ty
<< sc
->vfactor
);
265 sc
->scaleup_h
= (tx
>= sx
) ? 1 : 0;
266 sc
->scaleup_v
= (ty
>= sy
) ? 1 : 0;
268 /* check to see if input and output size/format differ */
269 if (s_frame
->fmt
->color
== d_frame
->fmt
->color
270 && s_frame
->width
== d_frame
->width
271 && s_frame
->height
== d_frame
->height
)
279 static void fimc_capture_handler(struct fimc_dev
*fimc
)
281 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
282 struct fimc_vid_buffer
*v_buf
= NULL
;
284 if (!list_empty(&cap
->active_buf_q
)) {
285 v_buf
= active_queue_pop(cap
);
286 fimc_buf_finish(fimc
, v_buf
);
289 if (test_and_clear_bit(ST_CAPT_SHUT
, &fimc
->state
)) {
290 wake_up(&fimc
->irq_queue
);
294 if (!list_empty(&cap
->pending_buf_q
)) {
296 v_buf
= pending_queue_pop(cap
);
297 fimc_hw_set_output_addr(fimc
, &v_buf
->paddr
, cap
->buf_index
);
298 v_buf
->index
= cap
->buf_index
;
300 dbg("hw ptr: %d, sw ptr: %d",
301 fimc_hw_get_frame_index(fimc
), cap
->buf_index
);
303 spin_lock(&fimc
->irqlock
);
304 v_buf
->vb
.state
= VIDEOBUF_ACTIVE
;
305 spin_unlock(&fimc
->irqlock
);
307 /* Move the buffer to the capture active queue */
308 active_queue_add(cap
, v_buf
);
310 dbg("next frame: %d, done frame: %d",
311 fimc_hw_get_frame_index(fimc
), v_buf
->index
);
313 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
316 } else if (test_and_clear_bit(ST_CAPT_STREAM
, &fimc
->state
) &&
317 cap
->active_buf_cnt
<= 1) {
318 fimc_deactivate_capture(fimc
);
321 dbg("frame: %d, active_buf_cnt= %d",
322 fimc_hw_get_frame_index(fimc
), cap
->active_buf_cnt
);
325 static irqreturn_t
fimc_isr(int irq
, void *priv
)
327 struct fimc_vid_buffer
*src_buf
, *dst_buf
;
328 struct fimc_ctx
*ctx
;
329 struct fimc_dev
*fimc
= priv
;
332 fimc_hw_clear_irq(fimc
);
334 spin_lock(&fimc
->slock
);
336 if (test_and_clear_bit(ST_M2M_PEND
, &fimc
->state
)) {
337 ctx
= v4l2_m2m_get_curr_priv(fimc
->m2m
.m2m_dev
);
338 if (!ctx
|| !ctx
->m2m_ctx
)
340 src_buf
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
341 dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
);
342 if (src_buf
&& dst_buf
) {
343 spin_lock(&fimc
->irqlock
);
344 src_buf
->vb
.state
= dst_buf
->vb
.state
= VIDEOBUF_DONE
;
345 wake_up(&src_buf
->vb
.done
);
346 wake_up(&dst_buf
->vb
.done
);
347 spin_unlock(&fimc
->irqlock
);
348 v4l2_m2m_job_finish(fimc
->m2m
.m2m_dev
, ctx
->m2m_ctx
);
354 if (test_bit(ST_CAPT_RUN
, &fimc
->state
))
355 fimc_capture_handler(fimc
);
357 if (test_and_clear_bit(ST_CAPT_PEND
, &fimc
->state
)) {
358 set_bit(ST_CAPT_RUN
, &fimc
->state
);
359 wake_up(&fimc
->irq_queue
);
363 spin_unlock(&fimc
->slock
);
367 /* The color format (planes_cnt, buff_cnt) must be already configured. */
368 int fimc_prepare_addr(struct fimc_ctx
*ctx
, struct fimc_vid_buffer
*buf
,
369 struct fimc_frame
*frame
, struct fimc_addr
*paddr
)
374 if (buf
== NULL
|| frame
== NULL
)
377 pix_size
= frame
->width
* frame
->height
;
379 dbg("buff_cnt= %d, planes_cnt= %d, frame->size= %d, pix_size= %d",
380 frame
->fmt
->buff_cnt
, frame
->fmt
->planes_cnt
,
381 frame
->size
, pix_size
);
383 if (frame
->fmt
->buff_cnt
== 1) {
384 paddr
->y
= videobuf_to_dma_contig(&buf
->vb
);
385 switch (frame
->fmt
->planes_cnt
) {
391 /* decompose Y into Y/Cb */
392 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
396 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
397 /* decompose Y into Y/Cb/Cr */
398 if (S5P_FIMC_YCBCR420
== frame
->fmt
->color
)
399 paddr
->cr
= (u32
)(paddr
->cb
402 paddr
->cr
= (u32
)(paddr
->cb
410 dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
411 paddr
->y
, paddr
->cb
, paddr
->cr
, ret
);
416 /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
417 static void fimc_set_yuv_order(struct fimc_ctx
*ctx
)
419 /* The one only mode supported in SoC. */
420 ctx
->in_order_2p
= S5P_FIMC_LSB_CRCB
;
421 ctx
->out_order_2p
= S5P_FIMC_LSB_CRCB
;
423 /* Set order for 1 plane input formats. */
424 switch (ctx
->s_frame
.fmt
->color
) {
425 case S5P_FIMC_YCRYCB422
:
426 ctx
->in_order_1p
= S5P_FIMC_IN_YCRYCB
;
428 case S5P_FIMC_CBYCRY422
:
429 ctx
->in_order_1p
= S5P_FIMC_IN_CBYCRY
;
431 case S5P_FIMC_CRYCBY422
:
432 ctx
->in_order_1p
= S5P_FIMC_IN_CRYCBY
;
434 case S5P_FIMC_YCBYCR422
:
436 ctx
->in_order_1p
= S5P_FIMC_IN_YCBYCR
;
439 dbg("ctx->in_order_1p= %d", ctx
->in_order_1p
);
441 switch (ctx
->d_frame
.fmt
->color
) {
442 case S5P_FIMC_YCRYCB422
:
443 ctx
->out_order_1p
= S5P_FIMC_OUT_YCRYCB
;
445 case S5P_FIMC_CBYCRY422
:
446 ctx
->out_order_1p
= S5P_FIMC_OUT_CBYCRY
;
448 case S5P_FIMC_CRYCBY422
:
449 ctx
->out_order_1p
= S5P_FIMC_OUT_CRYCBY
;
451 case S5P_FIMC_YCBYCR422
:
453 ctx
->out_order_1p
= S5P_FIMC_OUT_YCBYCR
;
456 dbg("ctx->out_order_1p= %d", ctx
->out_order_1p
);
459 static void fimc_prepare_dma_offset(struct fimc_ctx
*ctx
, struct fimc_frame
*f
)
461 struct samsung_fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
463 f
->dma_offset
.y_h
= f
->offs_h
;
464 if (!variant
->pix_hoff
)
465 f
->dma_offset
.y_h
*= (f
->fmt
->depth
>> 3);
467 f
->dma_offset
.y_v
= f
->offs_v
;
469 f
->dma_offset
.cb_h
= f
->offs_h
;
470 f
->dma_offset
.cb_v
= f
->offs_v
;
472 f
->dma_offset
.cr_h
= f
->offs_h
;
473 f
->dma_offset
.cr_v
= f
->offs_v
;
475 if (!variant
->pix_hoff
) {
476 if (f
->fmt
->planes_cnt
== 3) {
477 f
->dma_offset
.cb_h
>>= 1;
478 f
->dma_offset
.cr_h
>>= 1;
480 if (f
->fmt
->color
== S5P_FIMC_YCBCR420
) {
481 f
->dma_offset
.cb_v
>>= 1;
482 f
->dma_offset
.cr_v
>>= 1;
486 dbg("in_offset: color= %d, y_h= %d, y_v= %d",
487 f
->fmt
->color
, f
->dma_offset
.y_h
, f
->dma_offset
.y_v
);
491 * fimc_prepare_config - check dimensions, operation and color mode
492 * and pre-calculate offset and the scaling coefficients.
494 * @ctx: hardware context information
495 * @flags: flags indicating which parameters to check/update
497 * Return: 0 if dimensions are valid or non zero otherwise.
499 int fimc_prepare_config(struct fimc_ctx
*ctx
, u32 flags
)
501 struct fimc_frame
*s_frame
, *d_frame
;
502 struct fimc_vid_buffer
*buf
= NULL
;
505 s_frame
= &ctx
->s_frame
;
506 d_frame
= &ctx
->d_frame
;
508 if (flags
& FIMC_PARAMS
) {
509 /* Prepare the DMA offset ratios for scaler. */
510 fimc_prepare_dma_offset(ctx
, &ctx
->s_frame
);
511 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
513 if (s_frame
->height
> (SCALER_MAX_VRATIO
* d_frame
->height
) ||
514 s_frame
->width
> (SCALER_MAX_HRATIO
* d_frame
->width
)) {
515 err("out of scaler range");
518 fimc_set_yuv_order(ctx
);
521 /* Input DMA mode is not allowed when the scaler is disabled. */
522 ctx
->scaler
.enabled
= 1;
524 if (flags
& FIMC_SRC_ADDR
) {
525 buf
= v4l2_m2m_next_src_buf(ctx
->m2m_ctx
);
526 ret
= fimc_prepare_addr(ctx
, buf
, s_frame
, &s_frame
->paddr
);
531 if (flags
& FIMC_DST_ADDR
) {
532 buf
= v4l2_m2m_next_dst_buf(ctx
->m2m_ctx
);
533 ret
= fimc_prepare_addr(ctx
, buf
, d_frame
, &d_frame
->paddr
);
539 static void fimc_dma_run(void *priv
)
541 struct fimc_ctx
*ctx
= priv
;
542 struct fimc_dev
*fimc
;
546 if (WARN(!ctx
, "null hardware context"))
549 fimc
= ctx
->fimc_dev
;
551 spin_lock_irqsave(&ctx
->slock
, flags
);
552 set_bit(ST_M2M_PEND
, &fimc
->state
);
554 ctx
->state
|= (FIMC_SRC_ADDR
| FIMC_DST_ADDR
);
555 ret
= fimc_prepare_config(ctx
, ctx
->state
);
557 err("Wrong parameters");
560 /* Reconfigure hardware if the context has changed. */
561 if (fimc
->m2m
.ctx
!= ctx
) {
562 ctx
->state
|= FIMC_PARAMS
;
566 fimc_hw_set_input_addr(fimc
, &ctx
->s_frame
.paddr
);
568 if (ctx
->state
& FIMC_PARAMS
) {
569 fimc_hw_set_input_path(ctx
);
570 fimc_hw_set_in_dma(ctx
);
571 if (fimc_set_scaler_info(ctx
)) {
572 err("Scaler setup error");
575 fimc_hw_set_scaler(ctx
);
576 fimc_hw_set_target_format(ctx
);
577 fimc_hw_set_rotation(ctx
);
578 fimc_hw_set_effect(ctx
);
581 fimc_hw_set_output_path(ctx
);
582 if (ctx
->state
& (FIMC_DST_ADDR
| FIMC_PARAMS
))
583 fimc_hw_set_output_addr(fimc
, &ctx
->d_frame
.paddr
, -1);
585 if (ctx
->state
& FIMC_PARAMS
)
586 fimc_hw_set_out_dma(ctx
);
588 fimc_activate_capture(ctx
);
590 ctx
->state
&= (FIMC_CTX_M2M
| FIMC_CTX_CAP
);
591 fimc_hw_activate_input_dma(fimc
, true);
594 spin_unlock_irqrestore(&ctx
->slock
, flags
);
597 static void fimc_job_abort(void *priv
)
599 /* Nothing done in job_abort. */
602 static void fimc_buf_release(struct videobuf_queue
*vq
,
603 struct videobuf_buffer
*vb
)
605 videobuf_dma_contig_free(vq
, vb
);
606 vb
->state
= VIDEOBUF_NEEDS_INIT
;
609 static int fimc_buf_setup(struct videobuf_queue
*vq
, unsigned int *count
,
612 struct fimc_ctx
*ctx
= vq
->priv_data
;
613 struct fimc_frame
*frame
;
615 frame
= ctx_get_frame(ctx
, vq
->type
);
617 return PTR_ERR(frame
);
619 *size
= (frame
->width
* frame
->height
* frame
->fmt
->depth
) >> 3;
625 static int fimc_buf_prepare(struct videobuf_queue
*vq
,
626 struct videobuf_buffer
*vb
, enum v4l2_field field
)
628 struct fimc_ctx
*ctx
= vq
->priv_data
;
629 struct v4l2_device
*v4l2_dev
= &ctx
->fimc_dev
->m2m
.v4l2_dev
;
630 struct fimc_frame
*frame
;
633 frame
= ctx_get_frame(ctx
, vq
->type
);
635 return PTR_ERR(frame
);
638 if (vb
->bsize
< frame
->size
) {
640 "User-provided buffer too small (%d < %d)\n",
641 vb
->bsize
, frame
->size
);
645 } else if (vb
->state
!= VIDEOBUF_NEEDS_INIT
646 && vb
->bsize
< frame
->size
) {
650 vb
->width
= frame
->width
;
651 vb
->height
= frame
->height
;
652 vb
->bytesperline
= (frame
->width
* frame
->fmt
->depth
) >> 3;
653 vb
->size
= frame
->size
;
656 if (VIDEOBUF_NEEDS_INIT
== vb
->state
) {
657 ret
= videobuf_iolock(vq
, vb
, NULL
);
659 v4l2_err(v4l2_dev
, "Iolock failed\n");
660 fimc_buf_release(vq
, vb
);
664 vb
->state
= VIDEOBUF_PREPARED
;
669 static void fimc_buf_queue(struct videobuf_queue
*vq
,
670 struct videobuf_buffer
*vb
)
672 struct fimc_ctx
*ctx
= vq
->priv_data
;
673 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
674 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
677 dbg("ctx: %p, ctx->state: 0x%x", ctx
, ctx
->state
);
679 if ((ctx
->state
& FIMC_CTX_M2M
) && ctx
->m2m_ctx
) {
680 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, vq
, vb
);
681 } else if (ctx
->state
& FIMC_CTX_CAP
) {
682 spin_lock_irqsave(&fimc
->slock
, flags
);
683 fimc_vid_cap_buf_queue(fimc
, (struct fimc_vid_buffer
*)vb
);
685 dbg("fimc->cap.active_buf_cnt: %d",
686 fimc
->vid_cap
.active_buf_cnt
);
688 if (cap
->active_buf_cnt
>= cap
->reqbufs_count
||
689 cap
->active_buf_cnt
>= FIMC_MAX_OUT_BUFS
) {
690 if (!test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
))
691 fimc_activate_capture(ctx
);
693 spin_unlock_irqrestore(&fimc
->slock
, flags
);
697 struct videobuf_queue_ops fimc_qops
= {
698 .buf_setup
= fimc_buf_setup
,
699 .buf_prepare
= fimc_buf_prepare
,
700 .buf_queue
= fimc_buf_queue
,
701 .buf_release
= fimc_buf_release
,
704 static int fimc_m2m_querycap(struct file
*file
, void *priv
,
705 struct v4l2_capability
*cap
)
707 struct fimc_ctx
*ctx
= file
->private_data
;
708 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
710 strncpy(cap
->driver
, fimc
->pdev
->name
, sizeof(cap
->driver
) - 1);
711 strncpy(cap
->card
, fimc
->pdev
->name
, sizeof(cap
->card
) - 1);
712 cap
->bus_info
[0] = 0;
713 cap
->version
= KERNEL_VERSION(1, 0, 0);
714 cap
->capabilities
= V4L2_CAP_STREAMING
|
715 V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_VIDEO_OUTPUT
;
720 int fimc_vidioc_enum_fmt(struct file
*file
, void *priv
,
721 struct v4l2_fmtdesc
*f
)
723 struct fimc_fmt
*fmt
;
725 if (f
->index
>= ARRAY_SIZE(fimc_formats
))
728 fmt
= &fimc_formats
[f
->index
];
729 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
730 f
->pixelformat
= fmt
->fourcc
;
735 int fimc_vidioc_g_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
737 struct fimc_ctx
*ctx
= priv
;
738 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
739 struct fimc_frame
*frame
;
741 frame
= ctx_get_frame(ctx
, f
->type
);
743 return PTR_ERR(frame
);
745 if (mutex_lock_interruptible(&fimc
->lock
))
748 f
->fmt
.pix
.width
= frame
->width
;
749 f
->fmt
.pix
.height
= frame
->height
;
750 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
751 f
->fmt
.pix
.pixelformat
= frame
->fmt
->fourcc
;
753 mutex_unlock(&fimc
->lock
);
757 struct fimc_fmt
*find_format(struct v4l2_format
*f
, unsigned int mask
)
759 struct fimc_fmt
*fmt
;
762 for (i
= 0; i
< ARRAY_SIZE(fimc_formats
); ++i
) {
763 fmt
= &fimc_formats
[i
];
764 if (fmt
->fourcc
== f
->fmt
.pix
.pixelformat
&&
769 return (i
== ARRAY_SIZE(fimc_formats
)) ? NULL
: fmt
;
772 struct fimc_fmt
*find_mbus_format(struct v4l2_mbus_framefmt
*f
,
775 struct fimc_fmt
*fmt
;
778 for (i
= 0; i
< ARRAY_SIZE(fimc_formats
); ++i
) {
779 fmt
= &fimc_formats
[i
];
780 if (fmt
->mbus_code
== f
->code
&& (fmt
->flags
& mask
))
784 return (i
== ARRAY_SIZE(fimc_formats
)) ? NULL
: fmt
;
788 int fimc_vidioc_try_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
790 struct fimc_ctx
*ctx
= priv
;
791 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
792 struct samsung_fimc_variant
*variant
= fimc
->variant
;
793 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
794 struct fimc_fmt
*fmt
;
795 u32 max_width
, mod_x
, mod_y
, mask
;
796 int ret
= -EINVAL
, is_output
= 0;
798 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
799 if (ctx
->state
& FIMC_CTX_CAP
)
802 } else if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
806 dbg("w: %d, h: %d, bpl: %d",
807 pix
->width
, pix
->height
, pix
->bytesperline
);
809 if (mutex_lock_interruptible(&fimc
->lock
))
812 mask
= is_output
? FMT_FLAGS_M2M
: FMT_FLAGS_M2M
| FMT_FLAGS_CAM
;
813 fmt
= find_format(f
, mask
);
815 v4l2_err(&fimc
->m2m
.v4l2_dev
, "Fourcc format (0x%X) invalid.\n",
820 if (pix
->field
== V4L2_FIELD_ANY
)
821 pix
->field
= V4L2_FIELD_NONE
;
822 else if (V4L2_FIELD_NONE
!= pix
->field
)
826 max_width
= variant
->pix_limit
->scaler_dis_w
;
827 mod_x
= ffs(variant
->min_inp_pixsize
) - 1;
829 max_width
= variant
->pix_limit
->out_rot_dis_w
;
830 mod_x
= ffs(variant
->min_out_pixsize
) - 1;
833 if (tiled_fmt(fmt
)) {
834 mod_x
= 6; /* 64 x 32 pixels tile */
837 if (fimc
->id
== 1 && fimc
->variant
->pix_hoff
)
838 mod_y
= fimc_fmt_is_rgb(fmt
->color
) ? 0 : 1;
843 dbg("mod_x: %d, mod_y: %d, max_w: %d", mod_x
, mod_y
, max_width
);
845 v4l_bound_align_image(&pix
->width
, 16, max_width
, mod_x
,
846 &pix
->height
, 8, variant
->pix_limit
->scaler_dis_w
, mod_y
, 0);
848 if (pix
->bytesperline
== 0 ||
849 (pix
->bytesperline
* 8 / fmt
->depth
) > pix
->width
)
850 pix
->bytesperline
= (pix
->width
* fmt
->depth
) >> 3;
852 if (pix
->sizeimage
== 0)
853 pix
->sizeimage
= pix
->height
* pix
->bytesperline
;
855 dbg("w: %d, h: %d, bpl: %d, depth: %d",
856 pix
->width
, pix
->height
, pix
->bytesperline
, fmt
->depth
);
861 mutex_unlock(&fimc
->lock
);
865 static int fimc_m2m_s_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
867 struct fimc_ctx
*ctx
= priv
;
868 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
869 struct v4l2_device
*v4l2_dev
= &fimc
->m2m
.v4l2_dev
;
870 struct videobuf_queue
*vq
;
871 struct fimc_frame
*frame
;
872 struct v4l2_pix_format
*pix
;
876 ret
= fimc_vidioc_try_fmt(file
, priv
, f
);
880 if (mutex_lock_interruptible(&fimc
->lock
))
883 vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
, f
->type
);
884 mutex_lock(&vq
->vb_lock
);
886 if (videobuf_queue_is_busy(vq
)) {
887 v4l2_err(v4l2_dev
, "%s: queue (%d) busy\n", __func__
, f
->type
);
892 spin_lock_irqsave(&ctx
->slock
, flags
);
893 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
894 frame
= &ctx
->s_frame
;
895 ctx
->state
|= FIMC_SRC_FMT
;
896 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
897 frame
= &ctx
->d_frame
;
898 ctx
->state
|= FIMC_DST_FMT
;
900 spin_unlock_irqrestore(&ctx
->slock
, flags
);
901 v4l2_err(&ctx
->fimc_dev
->m2m
.v4l2_dev
,
902 "Wrong buffer/video queue type (%d)\n", f
->type
);
906 spin_unlock_irqrestore(&ctx
->slock
, flags
);
909 frame
->fmt
= find_format(f
, FMT_FLAGS_M2M
);
915 frame
->f_width
= pix
->bytesperline
* 8 / frame
->fmt
->depth
;
916 frame
->f_height
= pix
->height
;
917 frame
->width
= pix
->width
;
918 frame
->height
= pix
->height
;
919 frame
->o_width
= pix
->width
;
920 frame
->o_height
= pix
->height
;
923 frame
->size
= (pix
->width
* pix
->height
* frame
->fmt
->depth
) >> 3;
924 vq
->field
= pix
->field
;
926 spin_lock_irqsave(&ctx
->slock
, flags
);
927 ctx
->state
|= FIMC_PARAMS
;
928 spin_unlock_irqrestore(&ctx
->slock
, flags
);
930 dbg("f_w: %d, f_h: %d", frame
->f_width
, frame
->f_height
);
933 mutex_unlock(&vq
->vb_lock
);
934 mutex_unlock(&fimc
->lock
);
938 static int fimc_m2m_reqbufs(struct file
*file
, void *priv
,
939 struct v4l2_requestbuffers
*reqbufs
)
941 struct fimc_ctx
*ctx
= priv
;
942 return v4l2_m2m_reqbufs(file
, ctx
->m2m_ctx
, reqbufs
);
945 static int fimc_m2m_querybuf(struct file
*file
, void *priv
,
946 struct v4l2_buffer
*buf
)
948 struct fimc_ctx
*ctx
= priv
;
949 return v4l2_m2m_querybuf(file
, ctx
->m2m_ctx
, buf
);
952 static int fimc_m2m_qbuf(struct file
*file
, void *priv
,
953 struct v4l2_buffer
*buf
)
955 struct fimc_ctx
*ctx
= priv
;
957 return v4l2_m2m_qbuf(file
, ctx
->m2m_ctx
, buf
);
960 static int fimc_m2m_dqbuf(struct file
*file
, void *priv
,
961 struct v4l2_buffer
*buf
)
963 struct fimc_ctx
*ctx
= priv
;
964 return v4l2_m2m_dqbuf(file
, ctx
->m2m_ctx
, buf
);
967 static int fimc_m2m_streamon(struct file
*file
, void *priv
,
968 enum v4l2_buf_type type
)
970 struct fimc_ctx
*ctx
= priv
;
971 return v4l2_m2m_streamon(file
, ctx
->m2m_ctx
, type
);
974 static int fimc_m2m_streamoff(struct file
*file
, void *priv
,
975 enum v4l2_buf_type type
)
977 struct fimc_ctx
*ctx
= priv
;
978 return v4l2_m2m_streamoff(file
, ctx
->m2m_ctx
, type
);
981 int fimc_vidioc_queryctrl(struct file
*file
, void *priv
,
982 struct v4l2_queryctrl
*qc
)
984 struct fimc_ctx
*ctx
= priv
;
985 struct v4l2_queryctrl
*c
;
988 c
= get_ctrl(qc
->id
);
994 if (ctx
->state
& FIMC_CTX_CAP
) {
995 if (mutex_lock_interruptible(&ctx
->fimc_dev
->lock
))
997 ret
= v4l2_subdev_call(ctx
->fimc_dev
->vid_cap
.sd
,
998 core
, queryctrl
, qc
);
999 mutex_unlock(&ctx
->fimc_dev
->lock
);
1004 int fimc_vidioc_g_ctrl(struct file
*file
, void *priv
,
1005 struct v4l2_control
*ctrl
)
1007 struct fimc_ctx
*ctx
= priv
;
1008 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1011 if (mutex_lock_interruptible(&fimc
->lock
))
1012 return -ERESTARTSYS
;
1015 case V4L2_CID_HFLIP
:
1016 ctrl
->value
= (FLIP_X_AXIS
& ctx
->flip
) ? 1 : 0;
1018 case V4L2_CID_VFLIP
:
1019 ctrl
->value
= (FLIP_Y_AXIS
& ctx
->flip
) ? 1 : 0;
1021 case V4L2_CID_ROTATE
:
1022 ctrl
->value
= ctx
->rotation
;
1025 if (ctx
->state
& FIMC_CTX_CAP
) {
1026 ret
= v4l2_subdev_call(fimc
->vid_cap
.sd
, core
,
1029 v4l2_err(&fimc
->m2m
.v4l2_dev
,
1030 "Invalid control\n");
1034 dbg("ctrl->value= %d", ctrl
->value
);
1036 mutex_unlock(&fimc
->lock
);
1040 int check_ctrl_val(struct fimc_ctx
*ctx
, struct v4l2_control
*ctrl
)
1042 struct v4l2_queryctrl
*c
;
1043 c
= get_ctrl(ctrl
->id
);
1047 if (ctrl
->value
< c
->minimum
|| ctrl
->value
> c
->maximum
1048 || (c
->step
!= 0 && ctrl
->value
% c
->step
!= 0)) {
1049 v4l2_err(&ctx
->fimc_dev
->m2m
.v4l2_dev
,
1050 "Invalid control value\n");
1057 int fimc_s_ctrl(struct fimc_ctx
*ctx
, struct v4l2_control
*ctrl
)
1059 struct samsung_fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
1060 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1061 unsigned long flags
;
1063 if (ctx
->rotation
!= 0 &&
1064 (ctrl
->id
== V4L2_CID_HFLIP
|| ctrl
->id
== V4L2_CID_VFLIP
)) {
1065 v4l2_err(&fimc
->m2m
.v4l2_dev
,
1066 "Simultaneous flip and rotation is not supported\n");
1070 spin_lock_irqsave(&ctx
->slock
, flags
);
1073 case V4L2_CID_HFLIP
:
1075 ctx
->flip
|= FLIP_X_AXIS
;
1077 ctx
->flip
&= ~FLIP_X_AXIS
;
1080 case V4L2_CID_VFLIP
:
1082 ctx
->flip
|= FLIP_Y_AXIS
;
1084 ctx
->flip
&= ~FLIP_Y_AXIS
;
1087 case V4L2_CID_ROTATE
:
1088 /* Check for the output rotator availability */
1089 if ((ctrl
->value
== 90 || ctrl
->value
== 270) &&
1090 (ctx
->in_path
== FIMC_DMA
&& !variant
->has_out_rot
)) {
1091 spin_unlock_irqrestore(&ctx
->slock
, flags
);
1094 ctx
->rotation
= ctrl
->value
;
1099 spin_unlock_irqrestore(&ctx
->slock
, flags
);
1100 v4l2_err(&fimc
->m2m
.v4l2_dev
, "Invalid control\n");
1103 ctx
->state
|= FIMC_PARAMS
;
1104 spin_unlock_irqrestore(&ctx
->slock
, flags
);
1109 static int fimc_m2m_s_ctrl(struct file
*file
, void *priv
,
1110 struct v4l2_control
*ctrl
)
1112 struct fimc_ctx
*ctx
= priv
;
1115 ret
= check_ctrl_val(ctx
, ctrl
);
1119 ret
= fimc_s_ctrl(ctx
, ctrl
);
1123 static int fimc_m2m_cropcap(struct file
*file
, void *fh
,
1124 struct v4l2_cropcap
*cr
)
1126 struct fimc_frame
*frame
;
1127 struct fimc_ctx
*ctx
= fh
;
1128 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1130 frame
= ctx_get_frame(ctx
, cr
->type
);
1132 return PTR_ERR(frame
);
1134 if (mutex_lock_interruptible(&fimc
->lock
))
1135 return -ERESTARTSYS
;
1137 cr
->bounds
.left
= 0;
1139 cr
->bounds
.width
= frame
->f_width
;
1140 cr
->bounds
.height
= frame
->f_height
;
1141 cr
->defrect
= cr
->bounds
;
1143 mutex_unlock(&fimc
->lock
);
1147 static int fimc_m2m_g_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
1149 struct fimc_frame
*frame
;
1150 struct fimc_ctx
*ctx
= file
->private_data
;
1151 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1153 frame
= ctx_get_frame(ctx
, cr
->type
);
1155 return PTR_ERR(frame
);
1157 if (mutex_lock_interruptible(&fimc
->lock
))
1158 return -ERESTARTSYS
;
1160 cr
->c
.left
= frame
->offs_h
;
1161 cr
->c
.top
= frame
->offs_v
;
1162 cr
->c
.width
= frame
->width
;
1163 cr
->c
.height
= frame
->height
;
1165 mutex_unlock(&fimc
->lock
);
1169 int fimc_try_crop(struct fimc_ctx
*ctx
, struct v4l2_crop
*cr
)
1171 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1172 struct fimc_frame
*f
;
1173 u32 min_size
, halign
;
1175 if (cr
->c
.top
< 0 || cr
->c
.left
< 0) {
1176 v4l2_err(&fimc
->m2m
.v4l2_dev
,
1177 "doesn't support negative values for top & left\n");
1181 if (cr
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1182 f
= (ctx
->state
& FIMC_CTX_CAP
) ? &ctx
->s_frame
: &ctx
->d_frame
;
1183 else if (cr
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
1184 ctx
->state
& FIMC_CTX_M2M
)
1189 min_size
= (f
== &ctx
->s_frame
) ?
1190 fimc
->variant
->min_inp_pixsize
: fimc
->variant
->min_out_pixsize
;
1192 if (ctx
->state
& FIMC_CTX_M2M
) {
1193 if (fimc
->id
== 1 && fimc
->variant
->pix_hoff
)
1194 halign
= fimc_fmt_is_rgb(f
->fmt
->color
) ? 0 : 1;
1196 halign
= ffs(min_size
) - 1;
1197 /* there are more strict aligment requirements at camera interface */
1203 v4l_bound_align_image(&cr
->c
.width
, min_size
, f
->o_width
,
1205 &cr
->c
.height
, min_size
, f
->o_height
,
1206 halign
, 64/(ALIGN(f
->fmt
->depth
, 8)));
1208 /* adjust left/top if cropping rectangle is out of bounds */
1209 if (cr
->c
.left
+ cr
->c
.width
> f
->o_width
)
1210 cr
->c
.left
= f
->o_width
- cr
->c
.width
;
1211 if (cr
->c
.top
+ cr
->c
.height
> f
->o_height
)
1212 cr
->c
.top
= f
->o_height
- cr
->c
.height
;
1214 cr
->c
.left
= round_down(cr
->c
.left
, min_size
);
1215 cr
->c
.top
= round_down(cr
->c
.top
,
1216 ctx
->state
& FIMC_CTX_M2M
? 8 : 16);
1218 dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
1219 cr
->c
.left
, cr
->c
.top
, cr
->c
.width
, cr
->c
.height
,
1220 f
->f_width
, f
->f_height
);
1226 static int fimc_m2m_s_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
1228 struct fimc_ctx
*ctx
= file
->private_data
;
1229 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1230 unsigned long flags
;
1231 struct fimc_frame
*f
;
1234 ret
= fimc_try_crop(ctx
, cr
);
1238 f
= (cr
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) ?
1239 &ctx
->s_frame
: &ctx
->d_frame
;
1241 if (mutex_lock_interruptible(&fimc
->lock
))
1242 return -ERESTARTSYS
;
1244 spin_lock_irqsave(&ctx
->slock
, flags
);
1245 if (~ctx
->state
& (FIMC_SRC_FMT
| FIMC_DST_FMT
)) {
1246 /* Check to see if scaling ratio is within supported range */
1247 if (cr
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
1248 ret
= fimc_check_scaler_ratio(&cr
->c
, &ctx
->d_frame
);
1250 ret
= fimc_check_scaler_ratio(&cr
->c
, &ctx
->s_frame
);
1252 v4l2_err(&fimc
->m2m
.v4l2_dev
, "Out of scaler range");
1257 ctx
->state
|= FIMC_PARAMS
;
1259 f
->offs_h
= cr
->c
.left
;
1260 f
->offs_v
= cr
->c
.top
;
1261 f
->width
= cr
->c
.width
;
1262 f
->height
= cr
->c
.height
;
1265 spin_unlock_irqrestore(&ctx
->slock
, flags
);
1266 mutex_unlock(&fimc
->lock
);
1270 static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops
= {
1271 .vidioc_querycap
= fimc_m2m_querycap
,
1273 .vidioc_enum_fmt_vid_cap
= fimc_vidioc_enum_fmt
,
1274 .vidioc_enum_fmt_vid_out
= fimc_vidioc_enum_fmt
,
1276 .vidioc_g_fmt_vid_cap
= fimc_vidioc_g_fmt
,
1277 .vidioc_g_fmt_vid_out
= fimc_vidioc_g_fmt
,
1279 .vidioc_try_fmt_vid_cap
= fimc_vidioc_try_fmt
,
1280 .vidioc_try_fmt_vid_out
= fimc_vidioc_try_fmt
,
1282 .vidioc_s_fmt_vid_cap
= fimc_m2m_s_fmt
,
1283 .vidioc_s_fmt_vid_out
= fimc_m2m_s_fmt
,
1285 .vidioc_reqbufs
= fimc_m2m_reqbufs
,
1286 .vidioc_querybuf
= fimc_m2m_querybuf
,
1288 .vidioc_qbuf
= fimc_m2m_qbuf
,
1289 .vidioc_dqbuf
= fimc_m2m_dqbuf
,
1291 .vidioc_streamon
= fimc_m2m_streamon
,
1292 .vidioc_streamoff
= fimc_m2m_streamoff
,
1294 .vidioc_queryctrl
= fimc_vidioc_queryctrl
,
1295 .vidioc_g_ctrl
= fimc_vidioc_g_ctrl
,
1296 .vidioc_s_ctrl
= fimc_m2m_s_ctrl
,
1298 .vidioc_g_crop
= fimc_m2m_g_crop
,
1299 .vidioc_s_crop
= fimc_m2m_s_crop
,
1300 .vidioc_cropcap
= fimc_m2m_cropcap
1304 static void queue_init(void *priv
, struct videobuf_queue
*vq
,
1305 enum v4l2_buf_type type
)
1307 struct fimc_ctx
*ctx
= priv
;
1308 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1310 videobuf_queue_dma_contig_init(vq
, &fimc_qops
,
1312 &fimc
->irqlock
, type
, V4L2_FIELD_NONE
,
1313 sizeof(struct fimc_vid_buffer
), priv
, NULL
);
1316 static int fimc_m2m_open(struct file
*file
)
1318 struct fimc_dev
*fimc
= video_drvdata(file
);
1319 struct fimc_ctx
*ctx
= NULL
;
1322 if (mutex_lock_interruptible(&fimc
->lock
))
1323 return -ERESTARTSYS
;
1325 dbg("pid: %d, state: 0x%lx, refcnt: %d",
1326 task_pid_nr(current
), fimc
->state
, fimc
->vid_cap
.refcnt
);
1329 * Return if the corresponding video capture node
1330 * is already opened.
1332 if (fimc
->vid_cap
.refcnt
> 0) {
1338 set_bit(ST_OUTDMA_RUN
, &fimc
->state
);
1340 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
1346 file
->private_data
= ctx
;
1347 ctx
->fimc_dev
= fimc
;
1348 /* Default color format */
1349 ctx
->s_frame
.fmt
= &fimc_formats
[0];
1350 ctx
->d_frame
.fmt
= &fimc_formats
[0];
1351 /* Setup the device context for mem2mem mode. */
1352 ctx
->state
= FIMC_CTX_M2M
;
1354 ctx
->in_path
= FIMC_DMA
;
1355 ctx
->out_path
= FIMC_DMA
;
1356 spin_lock_init(&ctx
->slock
);
1358 ctx
->m2m_ctx
= v4l2_m2m_ctx_init(ctx
, fimc
->m2m
.m2m_dev
, queue_init
);
1359 if (IS_ERR(ctx
->m2m_ctx
)) {
1360 err
= PTR_ERR(ctx
->m2m_ctx
);
1365 mutex_unlock(&fimc
->lock
);
1369 static int fimc_m2m_release(struct file
*file
)
1371 struct fimc_ctx
*ctx
= file
->private_data
;
1372 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1374 mutex_lock(&fimc
->lock
);
1376 dbg("pid: %d, state: 0x%lx, refcnt= %d",
1377 task_pid_nr(current
), fimc
->state
, fimc
->m2m
.refcnt
);
1379 v4l2_m2m_ctx_release(ctx
->m2m_ctx
);
1381 if (--fimc
->m2m
.refcnt
<= 0)
1382 clear_bit(ST_OUTDMA_RUN
, &fimc
->state
);
1384 mutex_unlock(&fimc
->lock
);
1388 static unsigned int fimc_m2m_poll(struct file
*file
,
1389 struct poll_table_struct
*wait
)
1391 struct fimc_ctx
*ctx
= file
->private_data
;
1393 return v4l2_m2m_poll(file
, ctx
->m2m_ctx
, wait
);
1397 static int fimc_m2m_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1399 struct fimc_ctx
*ctx
= file
->private_data
;
1401 return v4l2_m2m_mmap(file
, ctx
->m2m_ctx
, vma
);
1404 static const struct v4l2_file_operations fimc_m2m_fops
= {
1405 .owner
= THIS_MODULE
,
1406 .open
= fimc_m2m_open
,
1407 .release
= fimc_m2m_release
,
1408 .poll
= fimc_m2m_poll
,
1409 .unlocked_ioctl
= video_ioctl2
,
1410 .mmap
= fimc_m2m_mmap
,
1413 static struct v4l2_m2m_ops m2m_ops
= {
1414 .device_run
= fimc_dma_run
,
1415 .job_abort
= fimc_job_abort
,
1419 static int fimc_register_m2m_device(struct fimc_dev
*fimc
)
1421 struct video_device
*vfd
;
1422 struct platform_device
*pdev
;
1423 struct v4l2_device
*v4l2_dev
;
1430 v4l2_dev
= &fimc
->m2m
.v4l2_dev
;
1432 /* set name if it is empty */
1433 if (!v4l2_dev
->name
[0])
1434 snprintf(v4l2_dev
->name
, sizeof(v4l2_dev
->name
),
1435 "%s.m2m", dev_name(&pdev
->dev
));
1437 ret
= v4l2_device_register(&pdev
->dev
, v4l2_dev
);
1441 vfd
= video_device_alloc();
1443 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
1447 vfd
->fops
= &fimc_m2m_fops
;
1448 vfd
->ioctl_ops
= &fimc_m2m_ioctl_ops
;
1450 vfd
->release
= video_device_release
;
1452 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s:m2m", dev_name(&pdev
->dev
));
1454 video_set_drvdata(vfd
, fimc
);
1455 platform_set_drvdata(pdev
, fimc
);
1457 fimc
->m2m
.vfd
= vfd
;
1458 fimc
->m2m
.m2m_dev
= v4l2_m2m_init(&m2m_ops
);
1459 if (IS_ERR(fimc
->m2m
.m2m_dev
)) {
1460 v4l2_err(v4l2_dev
, "failed to initialize v4l2-m2m device\n");
1461 ret
= PTR_ERR(fimc
->m2m
.m2m_dev
);
1465 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, -1);
1468 "%s(): failed to register video device\n", __func__
);
1472 "FIMC m2m driver registered as /dev/video%d\n", vfd
->num
);
1477 v4l2_m2m_release(fimc
->m2m
.m2m_dev
);
1479 video_device_release(fimc
->m2m
.vfd
);
1481 v4l2_device_unregister(v4l2_dev
);
1486 static void fimc_unregister_m2m_device(struct fimc_dev
*fimc
)
1489 v4l2_m2m_release(fimc
->m2m
.m2m_dev
);
1490 video_unregister_device(fimc
->m2m
.vfd
);
1492 v4l2_device_unregister(&fimc
->m2m
.v4l2_dev
);
1496 static void fimc_clk_release(struct fimc_dev
*fimc
)
1499 for (i
= 0; i
< NUM_FIMC_CLOCKS
; i
++) {
1500 if (fimc
->clock
[i
]) {
1501 clk_disable(fimc
->clock
[i
]);
1502 clk_put(fimc
->clock
[i
]);
1507 static int fimc_clk_get(struct fimc_dev
*fimc
)
1510 for (i
= 0; i
< NUM_FIMC_CLOCKS
; i
++) {
1511 fimc
->clock
[i
] = clk_get(&fimc
->pdev
->dev
, fimc_clock_name
[i
]);
1512 if (IS_ERR(fimc
->clock
[i
])) {
1513 dev_err(&fimc
->pdev
->dev
,
1514 "failed to get fimc clock: %s\n",
1515 fimc_clock_name
[i
]);
1518 clk_enable(fimc
->clock
[i
]);
1523 static int fimc_probe(struct platform_device
*pdev
)
1525 struct fimc_dev
*fimc
;
1526 struct resource
*res
;
1527 struct samsung_fimc_driverdata
*drv_data
;
1530 dev_dbg(&pdev
->dev
, "%s():\n", __func__
);
1532 drv_data
= (struct samsung_fimc_driverdata
*)
1533 platform_get_device_id(pdev
)->driver_data
;
1535 if (pdev
->id
>= drv_data
->num_entities
) {
1536 dev_err(&pdev
->dev
, "Invalid platform device id: %d\n",
1541 fimc
= kzalloc(sizeof(struct fimc_dev
), GFP_KERNEL
);
1545 fimc
->id
= pdev
->id
;
1546 fimc
->variant
= drv_data
->variant
[fimc
->id
];
1548 fimc
->pdata
= pdev
->dev
.platform_data
;
1549 fimc
->state
= ST_IDLE
;
1551 spin_lock_init(&fimc
->irqlock
);
1552 init_waitqueue_head(&fimc
->irq_queue
);
1553 spin_lock_init(&fimc
->slock
);
1555 mutex_init(&fimc
->lock
);
1557 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1559 dev_err(&pdev
->dev
, "failed to find the registers\n");
1564 fimc
->regs_res
= request_mem_region(res
->start
, resource_size(res
),
1565 dev_name(&pdev
->dev
));
1566 if (!fimc
->regs_res
) {
1567 dev_err(&pdev
->dev
, "failed to obtain register region\n");
1572 fimc
->regs
= ioremap(res
->start
, resource_size(res
));
1574 dev_err(&pdev
->dev
, "failed to map registers\n");
1576 goto err_req_region
;
1579 ret
= fimc_clk_get(fimc
);
1581 goto err_regs_unmap
;
1582 clk_set_rate(fimc
->clock
[0], drv_data
->lclk_frequency
);
1584 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1586 dev_err(&pdev
->dev
, "failed to get IRQ resource\n");
1590 fimc
->irq
= res
->start
;
1592 fimc_hw_reset(fimc
);
1594 ret
= request_irq(fimc
->irq
, fimc_isr
, 0, pdev
->name
, fimc
);
1596 dev_err(&pdev
->dev
, "failed to install irq (%d)\n", ret
);
1600 ret
= fimc_register_m2m_device(fimc
);
1604 /* At least one camera sensor is required to register capture node */
1607 for (i
= 0; i
< FIMC_MAX_CAMIF_CLIENTS
; ++i
)
1608 if (fimc
->pdata
->isp_info
[i
])
1611 if (i
< FIMC_MAX_CAMIF_CLIENTS
) {
1612 ret
= fimc_register_capture_device(fimc
);
1619 * Exclude the additional output DMA address registers by masking
1620 * them out on HW revisions that provide extended capabilites.
1622 if (fimc
->variant
->out_buf_count
> 4)
1623 fimc_hw_set_dma_seq(fimc
, 0xF);
1625 dev_dbg(&pdev
->dev
, "%s(): fimc-%d registered successfully\n",
1626 __func__
, fimc
->id
);
1631 fimc_unregister_m2m_device(fimc
);
1633 free_irq(fimc
->irq
, fimc
);
1635 fimc_clk_release(fimc
);
1637 iounmap(fimc
->regs
);
1639 release_resource(fimc
->regs_res
);
1640 kfree(fimc
->regs_res
);
1647 static int __devexit
fimc_remove(struct platform_device
*pdev
)
1649 struct fimc_dev
*fimc
=
1650 (struct fimc_dev
*)platform_get_drvdata(pdev
);
1652 free_irq(fimc
->irq
, fimc
);
1653 fimc_hw_reset(fimc
);
1655 fimc_unregister_m2m_device(fimc
);
1656 fimc_unregister_capture_device(fimc
);
1658 fimc_clk_release(fimc
);
1659 iounmap(fimc
->regs
);
1660 release_resource(fimc
->regs_res
);
1661 kfree(fimc
->regs_res
);
1664 dev_info(&pdev
->dev
, "%s driver unloaded\n", pdev
->name
);
1668 /* Image pixel limits, similar across several FIMC HW revisions. */
1669 static struct fimc_pix_limit s5p_pix_limit
[3] = {
1671 .scaler_en_w
= 3264,
1672 .scaler_dis_w
= 8192,
1673 .in_rot_en_h
= 1920,
1674 .in_rot_dis_w
= 8192,
1675 .out_rot_en_w
= 1920,
1676 .out_rot_dis_w
= 4224,
1679 .scaler_en_w
= 4224,
1680 .scaler_dis_w
= 8192,
1681 .in_rot_en_h
= 1920,
1682 .in_rot_dis_w
= 8192,
1683 .out_rot_en_w
= 1920,
1684 .out_rot_dis_w
= 4224,
1687 .scaler_en_w
= 1920,
1688 .scaler_dis_w
= 8192,
1689 .in_rot_en_h
= 1280,
1690 .in_rot_dis_w
= 8192,
1691 .out_rot_en_w
= 1280,
1692 .out_rot_dis_w
= 1920,
1696 static struct samsung_fimc_variant fimc0_variant_s5p
= {
1699 .min_inp_pixsize
= 16,
1700 .min_out_pixsize
= 16,
1701 .hor_offs_align
= 8,
1703 .pix_limit
= &s5p_pix_limit
[0],
1706 static struct samsung_fimc_variant fimc2_variant_s5p
= {
1707 .min_inp_pixsize
= 16,
1708 .min_out_pixsize
= 16,
1709 .hor_offs_align
= 8,
1711 .pix_limit
= &s5p_pix_limit
[1],
1714 static struct samsung_fimc_variant fimc0_variant_s5pv210
= {
1718 .min_inp_pixsize
= 16,
1719 .min_out_pixsize
= 16,
1720 .hor_offs_align
= 8,
1722 .pix_limit
= &s5p_pix_limit
[1],
1725 static struct samsung_fimc_variant fimc1_variant_s5pv210
= {
1729 .min_inp_pixsize
= 16,
1730 .min_out_pixsize
= 16,
1731 .hor_offs_align
= 1,
1733 .pix_limit
= &s5p_pix_limit
[2],
1736 static struct samsung_fimc_variant fimc2_variant_s5pv210
= {
1738 .min_inp_pixsize
= 16,
1739 .min_out_pixsize
= 16,
1740 .hor_offs_align
= 8,
1742 .pix_limit
= &s5p_pix_limit
[2],
1745 static struct samsung_fimc_variant fimc0_variant_s5pv310
= {
1749 .min_inp_pixsize
= 16,
1750 .min_out_pixsize
= 16,
1751 .hor_offs_align
= 1,
1752 .out_buf_count
= 32,
1753 .pix_limit
= &s5p_pix_limit
[1],
1756 static struct samsung_fimc_variant fimc2_variant_s5pv310
= {
1758 .min_inp_pixsize
= 16,
1759 .min_out_pixsize
= 16,
1760 .hor_offs_align
= 1,
1761 .out_buf_count
= 32,
1762 .pix_limit
= &s5p_pix_limit
[2],
1766 static struct samsung_fimc_driverdata fimc_drvdata_s5p
= {
1768 [0] = &fimc0_variant_s5p
,
1769 [1] = &fimc0_variant_s5p
,
1770 [2] = &fimc2_variant_s5p
,
1773 .lclk_frequency
= 133000000UL,
1776 /* S5PV210, S5PC110 */
1777 static struct samsung_fimc_driverdata fimc_drvdata_s5pv210
= {
1779 [0] = &fimc0_variant_s5pv210
,
1780 [1] = &fimc1_variant_s5pv210
,
1781 [2] = &fimc2_variant_s5pv210
,
1784 .lclk_frequency
= 166000000UL,
1787 /* S5PV310, S5PC210 */
1788 static struct samsung_fimc_driverdata fimc_drvdata_s5pv310
= {
1790 [0] = &fimc0_variant_s5pv310
,
1791 [1] = &fimc0_variant_s5pv310
,
1792 [2] = &fimc0_variant_s5pv310
,
1793 [3] = &fimc2_variant_s5pv310
,
1796 .lclk_frequency
= 166000000UL,
1799 static struct platform_device_id fimc_driver_ids
[] = {
1802 .driver_data
= (unsigned long)&fimc_drvdata_s5p
,
1804 .name
= "s5pv210-fimc",
1805 .driver_data
= (unsigned long)&fimc_drvdata_s5pv210
,
1807 .name
= "s5pv310-fimc",
1808 .driver_data
= (unsigned long)&fimc_drvdata_s5pv310
,
1812 MODULE_DEVICE_TABLE(platform
, fimc_driver_ids
);
1814 static struct platform_driver fimc_driver
= {
1815 .probe
= fimc_probe
,
1816 .remove
= __devexit_p(fimc_remove
),
1817 .id_table
= fimc_driver_ids
,
1819 .name
= MODULE_NAME
,
1820 .owner
= THIS_MODULE
,
1824 static int __init
fimc_init(void)
1826 int ret
= platform_driver_register(&fimc_driver
);
1828 err("platform_driver_register failed: %d\n", ret
);
1832 static void __exit
fimc_exit(void)
1834 platform_driver_unregister(&fimc_driver
);
1837 module_init(fimc_init
);
1838 module_exit(fimc_exit
);
1840 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1841 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1842 MODULE_LICENSE("GPL");