1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Samsung S5P/EXYNOS4 SoC series FIMC (CAMIF) driver
5 * Copyright (C) 2010-2012 Samsung Electronics Co., Ltd.
6 * Sylwester Nawrocki <s.nawrocki@samsung.com>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/errno.h>
13 #include <linux/bug.h>
14 #include <linux/interrupt.h>
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/list.h>
19 #include <linux/mfd/syscon.h>
22 #include <linux/of_device.h>
23 #include <linux/slab.h>
24 #include <linux/clk.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/videobuf2-v4l2.h>
27 #include <media/videobuf2-dma-contig.h>
29 #include "fimc-core.h"
31 #include "media-dev.h"
33 static char *fimc_clocks
[MAX_FIMC_CLOCKS
] = {
37 static struct fimc_fmt fimc_formats
[] = {
39 .fourcc
= V4L2_PIX_FMT_RGB565
,
41 .color
= FIMC_FMT_RGB565
,
44 .flags
= FMT_FLAGS_M2M
,
46 .fourcc
= V4L2_PIX_FMT_BGR666
,
48 .color
= FIMC_FMT_RGB666
,
51 .flags
= FMT_FLAGS_M2M
,
53 .fourcc
= V4L2_PIX_FMT_BGR32
,
55 .color
= FIMC_FMT_RGB888
,
58 .flags
= FMT_FLAGS_M2M
| FMT_HAS_ALPHA
,
60 .fourcc
= V4L2_PIX_FMT_RGB555
,
62 .color
= FIMC_FMT_RGB555
,
65 .flags
= FMT_FLAGS_M2M_OUT
| FMT_HAS_ALPHA
,
67 .fourcc
= V4L2_PIX_FMT_RGB444
,
69 .color
= FIMC_FMT_RGB444
,
72 .flags
= FMT_FLAGS_M2M_OUT
| FMT_HAS_ALPHA
,
74 .mbus_code
= MEDIA_BUS_FMT_YUV10_1X30
,
75 .flags
= FMT_FLAGS_WRITEBACK
,
77 .fourcc
= V4L2_PIX_FMT_YUYV
,
79 .color
= FIMC_FMT_YCBYCR422
,
82 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
83 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
85 .fourcc
= V4L2_PIX_FMT_UYVY
,
87 .color
= FIMC_FMT_CBYCRY422
,
90 .mbus_code
= MEDIA_BUS_FMT_UYVY8_2X8
,
91 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
93 .fourcc
= V4L2_PIX_FMT_VYUY
,
95 .color
= FIMC_FMT_CRYCBY422
,
98 .mbus_code
= MEDIA_BUS_FMT_VYUY8_2X8
,
99 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
101 .fourcc
= V4L2_PIX_FMT_YVYU
,
103 .color
= FIMC_FMT_YCRYCB422
,
106 .mbus_code
= MEDIA_BUS_FMT_YVYU8_2X8
,
107 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
109 .fourcc
= V4L2_PIX_FMT_YUV422P
,
111 .color
= FIMC_FMT_YCBYCR422
,
114 .flags
= FMT_FLAGS_M2M
,
116 .fourcc
= V4L2_PIX_FMT_NV16
,
118 .color
= FIMC_FMT_YCBYCR422
,
121 .flags
= FMT_FLAGS_M2M
,
123 .fourcc
= V4L2_PIX_FMT_NV61
,
125 .color
= FIMC_FMT_YCRYCB422
,
128 .flags
= FMT_FLAGS_M2M
,
130 .fourcc
= V4L2_PIX_FMT_YUV420
,
132 .color
= FIMC_FMT_YCBCR420
,
135 .flags
= FMT_FLAGS_M2M
,
137 .fourcc
= V4L2_PIX_FMT_NV12
,
139 .color
= FIMC_FMT_YCBCR420
,
142 .flags
= FMT_FLAGS_M2M
,
144 .fourcc
= V4L2_PIX_FMT_NV12M
,
145 .color
= FIMC_FMT_YCBCR420
,
149 .flags
= FMT_FLAGS_M2M
,
151 .fourcc
= V4L2_PIX_FMT_YUV420M
,
152 .color
= FIMC_FMT_YCBCR420
,
153 .depth
= { 8, 2, 2 },
156 .flags
= FMT_FLAGS_M2M
,
158 .fourcc
= V4L2_PIX_FMT_NV12MT
,
159 .color
= FIMC_FMT_YCBCR420
,
163 .flags
= FMT_FLAGS_M2M
,
165 .fourcc
= V4L2_PIX_FMT_JPEG
,
166 .color
= FIMC_FMT_JPEG
,
170 .mbus_code
= MEDIA_BUS_FMT_JPEG_1X8
,
171 .flags
= FMT_FLAGS_CAM
| FMT_FLAGS_COMPRESSED
,
173 .fourcc
= V4L2_PIX_FMT_S5C_UYVY_JPG
,
174 .color
= FIMC_FMT_YUYV_JPEG
,
178 .mdataplanes
= 0x2, /* plane 1 holds frame meta data */
179 .mbus_code
= MEDIA_BUS_FMT_S5C_UYVY_JPEG_1X8
,
180 .flags
= FMT_FLAGS_CAM
| FMT_FLAGS_COMPRESSED
,
184 struct fimc_fmt
*fimc_get_format(unsigned int index
)
186 if (index
>= ARRAY_SIZE(fimc_formats
))
189 return &fimc_formats
[index
];
192 int fimc_check_scaler_ratio(struct fimc_ctx
*ctx
, int sw
, int sh
,
193 int dw
, int dh
, int rotation
)
195 if (rotation
== 90 || rotation
== 270)
198 if (!ctx
->scaler
.enabled
)
199 return (sw
== dw
&& sh
== dh
) ? 0 : -EINVAL
;
201 if ((sw
>= SCALER_MAX_HRATIO
* dw
) || (sh
>= SCALER_MAX_VRATIO
* dh
))
207 static int fimc_get_scaler_factor(u32 src
, u32 tar
, u32
*ratio
, u32
*shift
)
216 if (src
>= tar
* tmp
) {
227 int fimc_set_scaler_info(struct fimc_ctx
*ctx
)
229 const struct fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
230 struct device
*dev
= &ctx
->fimc_dev
->pdev
->dev
;
231 struct fimc_scaler
*sc
= &ctx
->scaler
;
232 struct fimc_frame
*s_frame
= &ctx
->s_frame
;
233 struct fimc_frame
*d_frame
= &ctx
->d_frame
;
237 if (ctx
->rotation
== 90 || ctx
->rotation
== 270) {
239 tx
= d_frame
->height
;
242 ty
= d_frame
->height
;
244 if (tx
<= 0 || ty
<= 0) {
245 dev_err(dev
, "Invalid target size: %dx%d\n", tx
, ty
);
250 sy
= s_frame
->height
;
251 if (sx
<= 0 || sy
<= 0) {
252 dev_err(dev
, "Invalid source size: %dx%d\n", sx
, sy
);
256 sc
->real_height
= sy
;
258 ret
= fimc_get_scaler_factor(sx
, tx
, &sc
->pre_hratio
, &sc
->hfactor
);
262 ret
= fimc_get_scaler_factor(sy
, ty
, &sc
->pre_vratio
, &sc
->vfactor
);
266 sc
->pre_dst_width
= sx
/ sc
->pre_hratio
;
267 sc
->pre_dst_height
= sy
/ sc
->pre_vratio
;
269 if (variant
->has_mainscaler_ext
) {
270 sc
->main_hratio
= (sx
<< 14) / (tx
<< sc
->hfactor
);
271 sc
->main_vratio
= (sy
<< 14) / (ty
<< sc
->vfactor
);
273 sc
->main_hratio
= (sx
<< 8) / (tx
<< sc
->hfactor
);
274 sc
->main_vratio
= (sy
<< 8) / (ty
<< sc
->vfactor
);
278 sc
->scaleup_h
= (tx
>= sx
) ? 1 : 0;
279 sc
->scaleup_v
= (ty
>= sy
) ? 1 : 0;
281 /* check to see if input and output size/format differ */
282 if (s_frame
->fmt
->color
== d_frame
->fmt
->color
283 && s_frame
->width
== d_frame
->width
284 && s_frame
->height
== d_frame
->height
)
292 static irqreturn_t
fimc_irq_handler(int irq
, void *priv
)
294 struct fimc_dev
*fimc
= priv
;
295 struct fimc_ctx
*ctx
;
297 fimc_hw_clear_irq(fimc
);
299 spin_lock(&fimc
->slock
);
301 if (test_and_clear_bit(ST_M2M_PEND
, &fimc
->state
)) {
302 if (test_and_clear_bit(ST_M2M_SUSPENDING
, &fimc
->state
)) {
303 set_bit(ST_M2M_SUSPENDED
, &fimc
->state
);
304 wake_up(&fimc
->irq_queue
);
307 ctx
= v4l2_m2m_get_curr_priv(fimc
->m2m
.m2m_dev
);
309 spin_unlock(&fimc
->slock
);
310 fimc_m2m_job_finish(ctx
, VB2_BUF_STATE_DONE
);
312 if (ctx
->state
& FIMC_CTX_SHUT
) {
313 ctx
->state
&= ~FIMC_CTX_SHUT
;
314 wake_up(&fimc
->irq_queue
);
318 } else if (test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
319 int last_buf
= test_bit(ST_CAPT_JPEG
, &fimc
->state
) &&
320 fimc
->vid_cap
.reqbufs_count
== 1;
321 fimc_capture_irq_handler(fimc
, !last_buf
);
324 spin_unlock(&fimc
->slock
);
328 /* The color format (colplanes, memplanes) must be already configured. */
329 int fimc_prepare_addr(struct fimc_ctx
*ctx
, struct vb2_buffer
*vb
,
330 struct fimc_frame
*frame
, struct fimc_addr
*addr
)
335 if (vb
== NULL
|| frame
== NULL
)
338 pix_size
= frame
->width
* frame
->height
;
340 dbg("memplanes= %d, colplanes= %d, pix_size= %d",
341 frame
->fmt
->memplanes
, frame
->fmt
->colplanes
, pix_size
);
343 addr
->y
= vb2_dma_contig_plane_dma_addr(vb
, 0);
345 if (frame
->fmt
->memplanes
== 1) {
346 switch (frame
->fmt
->colplanes
) {
352 /* decompose Y into Y/Cb */
353 addr
->cb
= (u32
)(addr
->y
+ pix_size
);
357 addr
->cb
= (u32
)(addr
->y
+ pix_size
);
358 /* decompose Y into Y/Cb/Cr */
359 if (FIMC_FMT_YCBCR420
== frame
->fmt
->color
)
360 addr
->cr
= (u32
)(addr
->cb
+ (pix_size
>> 2));
362 addr
->cr
= (u32
)(addr
->cb
+ (pix_size
>> 1));
367 } else if (!frame
->fmt
->mdataplanes
) {
368 if (frame
->fmt
->memplanes
>= 2)
369 addr
->cb
= vb2_dma_contig_plane_dma_addr(vb
, 1);
371 if (frame
->fmt
->memplanes
== 3)
372 addr
->cr
= vb2_dma_contig_plane_dma_addr(vb
, 2);
375 dbg("DMA ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
376 addr
->y
, addr
->cb
, addr
->cr
, ret
);
381 /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
382 void fimc_set_yuv_order(struct fimc_ctx
*ctx
)
384 /* The one only mode supported in SoC. */
385 ctx
->in_order_2p
= FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB
;
386 ctx
->out_order_2p
= FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB
;
388 /* Set order for 1 plane input formats. */
389 switch (ctx
->s_frame
.fmt
->color
) {
390 case FIMC_FMT_YCRYCB422
:
391 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_YCRYCB
;
393 case FIMC_FMT_CBYCRY422
:
394 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_CBYCRY
;
396 case FIMC_FMT_CRYCBY422
:
397 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_CRYCBY
;
399 case FIMC_FMT_YCBYCR422
:
401 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_YCBYCR
;
404 dbg("ctx->in_order_1p= %d", ctx
->in_order_1p
);
406 switch (ctx
->d_frame
.fmt
->color
) {
407 case FIMC_FMT_YCRYCB422
:
408 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_YCRYCB
;
410 case FIMC_FMT_CBYCRY422
:
411 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_CBYCRY
;
413 case FIMC_FMT_CRYCBY422
:
414 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_CRYCBY
;
416 case FIMC_FMT_YCBYCR422
:
418 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_YCBYCR
;
421 dbg("ctx->out_order_1p= %d", ctx
->out_order_1p
);
424 void fimc_prepare_dma_offset(struct fimc_ctx
*ctx
, struct fimc_frame
*f
)
426 bool pix_hoff
= ctx
->fimc_dev
->drv_data
->dma_pix_hoff
;
429 for (i
= 0; i
< f
->fmt
->memplanes
; i
++)
430 depth
+= f
->fmt
->depth
[i
];
432 f
->dma_offset
.y_h
= f
->offs_h
;
434 f
->dma_offset
.y_h
*= (depth
>> 3);
436 f
->dma_offset
.y_v
= f
->offs_v
;
438 f
->dma_offset
.cb_h
= f
->offs_h
;
439 f
->dma_offset
.cb_v
= f
->offs_v
;
441 f
->dma_offset
.cr_h
= f
->offs_h
;
442 f
->dma_offset
.cr_v
= f
->offs_v
;
445 if (f
->fmt
->colplanes
== 3) {
446 f
->dma_offset
.cb_h
>>= 1;
447 f
->dma_offset
.cr_h
>>= 1;
449 if (f
->fmt
->color
== FIMC_FMT_YCBCR420
) {
450 f
->dma_offset
.cb_v
>>= 1;
451 f
->dma_offset
.cr_v
>>= 1;
455 dbg("in_offset: color= %d, y_h= %d, y_v= %d",
456 f
->fmt
->color
, f
->dma_offset
.y_h
, f
->dma_offset
.y_v
);
459 static int fimc_set_color_effect(struct fimc_ctx
*ctx
, enum v4l2_colorfx colorfx
)
461 struct fimc_effect
*effect
= &ctx
->effect
;
464 case V4L2_COLORFX_NONE
:
465 effect
->type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
467 case V4L2_COLORFX_BW
:
468 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
469 effect
->pat_cb
= 128;
470 effect
->pat_cr
= 128;
472 case V4L2_COLORFX_SEPIA
:
473 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
474 effect
->pat_cb
= 115;
475 effect
->pat_cr
= 145;
477 case V4L2_COLORFX_NEGATIVE
:
478 effect
->type
= FIMC_REG_CIIMGEFF_FIN_NEGATIVE
;
480 case V4L2_COLORFX_EMBOSS
:
481 effect
->type
= FIMC_REG_CIIMGEFF_FIN_EMBOSSING
;
483 case V4L2_COLORFX_ART_FREEZE
:
484 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARTFREEZE
;
486 case V4L2_COLORFX_SILHOUETTE
:
487 effect
->type
= FIMC_REG_CIIMGEFF_FIN_SILHOUETTE
;
489 case V4L2_COLORFX_SET_CBCR
:
490 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
491 effect
->pat_cb
= ctx
->ctrls
.colorfx_cbcr
->val
>> 8;
492 effect
->pat_cr
= ctx
->ctrls
.colorfx_cbcr
->val
& 0xff;
502 * V4L2 controls handling
504 #define ctrl_to_ctx(__ctrl) \
505 container_of((__ctrl)->handler, struct fimc_ctx, ctrls.handler)
507 static int __fimc_s_ctrl(struct fimc_ctx
*ctx
, struct v4l2_ctrl
*ctrl
)
509 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
510 const struct fimc_variant
*variant
= fimc
->variant
;
513 if (ctrl
->flags
& V4L2_CTRL_FLAG_INACTIVE
)
518 ctx
->hflip
= ctrl
->val
;
522 ctx
->vflip
= ctrl
->val
;
525 case V4L2_CID_ROTATE
:
526 if (fimc_capture_pending(fimc
)) {
527 ret
= fimc_check_scaler_ratio(ctx
, ctx
->s_frame
.width
,
528 ctx
->s_frame
.height
, ctx
->d_frame
.width
,
529 ctx
->d_frame
.height
, ctrl
->val
);
533 if ((ctrl
->val
== 90 || ctrl
->val
== 270) &&
534 !variant
->has_out_rot
)
537 ctx
->rotation
= ctrl
->val
;
540 case V4L2_CID_ALPHA_COMPONENT
:
541 ctx
->d_frame
.alpha
= ctrl
->val
;
544 case V4L2_CID_COLORFX
:
545 ret
= fimc_set_color_effect(ctx
, ctrl
->val
);
551 ctx
->state
|= FIMC_PARAMS
;
552 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
556 static int fimc_s_ctrl(struct v4l2_ctrl
*ctrl
)
558 struct fimc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
562 spin_lock_irqsave(&ctx
->fimc_dev
->slock
, flags
);
563 ret
= __fimc_s_ctrl(ctx
, ctrl
);
564 spin_unlock_irqrestore(&ctx
->fimc_dev
->slock
, flags
);
569 static const struct v4l2_ctrl_ops fimc_ctrl_ops
= {
570 .s_ctrl
= fimc_s_ctrl
,
573 int fimc_ctrls_create(struct fimc_ctx
*ctx
)
575 unsigned int max_alpha
= fimc_get_alpha_mask(ctx
->d_frame
.fmt
);
576 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
577 struct v4l2_ctrl_handler
*handler
= &ctrls
->handler
;
579 if (ctx
->ctrls
.ready
)
582 v4l2_ctrl_handler_init(handler
, 6);
584 ctrls
->rotate
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
585 V4L2_CID_ROTATE
, 0, 270, 90, 0);
586 ctrls
->hflip
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
587 V4L2_CID_HFLIP
, 0, 1, 1, 0);
588 ctrls
->vflip
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
589 V4L2_CID_VFLIP
, 0, 1, 1, 0);
591 if (ctx
->fimc_dev
->drv_data
->alpha_color
)
592 ctrls
->alpha
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
593 V4L2_CID_ALPHA_COMPONENT
,
598 ctrls
->colorfx
= v4l2_ctrl_new_std_menu(handler
, &fimc_ctrl_ops
,
599 V4L2_CID_COLORFX
, V4L2_COLORFX_SET_CBCR
,
600 ~0x983f, V4L2_COLORFX_NONE
);
602 ctrls
->colorfx_cbcr
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
603 V4L2_CID_COLORFX_CBCR
, 0, 0xffff, 1, 0);
605 ctx
->effect
.type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
607 if (!handler
->error
) {
608 v4l2_ctrl_cluster(2, &ctrls
->colorfx
);
612 return handler
->error
;
615 void fimc_ctrls_delete(struct fimc_ctx
*ctx
)
617 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
620 v4l2_ctrl_handler_free(&ctrls
->handler
);
621 ctrls
->ready
= false;
626 void fimc_ctrls_activate(struct fimc_ctx
*ctx
, bool active
)
628 unsigned int has_alpha
= ctx
->d_frame
.fmt
->flags
& FMT_HAS_ALPHA
;
629 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
634 mutex_lock(ctrls
->handler
.lock
);
635 v4l2_ctrl_activate(ctrls
->rotate
, active
);
636 v4l2_ctrl_activate(ctrls
->hflip
, active
);
637 v4l2_ctrl_activate(ctrls
->vflip
, active
);
638 v4l2_ctrl_activate(ctrls
->colorfx
, active
);
640 v4l2_ctrl_activate(ctrls
->alpha
, active
&& has_alpha
);
643 fimc_set_color_effect(ctx
, ctrls
->colorfx
->cur
.val
);
644 ctx
->rotation
= ctrls
->rotate
->val
;
645 ctx
->hflip
= ctrls
->hflip
->val
;
646 ctx
->vflip
= ctrls
->vflip
->val
;
648 ctx
->effect
.type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
653 mutex_unlock(ctrls
->handler
.lock
);
656 /* Update maximum value of the alpha color control */
657 void fimc_alpha_ctrl_update(struct fimc_ctx
*ctx
)
659 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
660 struct v4l2_ctrl
*ctrl
= ctx
->ctrls
.alpha
;
662 if (ctrl
== NULL
|| !fimc
->drv_data
->alpha_color
)
665 v4l2_ctrl_lock(ctrl
);
666 ctrl
->maximum
= fimc_get_alpha_mask(ctx
->d_frame
.fmt
);
668 if (ctrl
->cur
.val
> ctrl
->maximum
)
669 ctrl
->cur
.val
= ctrl
->maximum
;
671 v4l2_ctrl_unlock(ctrl
);
674 void __fimc_get_format(struct fimc_frame
*frame
, struct v4l2_format
*f
)
676 struct v4l2_pix_format_mplane
*pixm
= &f
->fmt
.pix_mp
;
679 pixm
->width
= frame
->o_width
;
680 pixm
->height
= frame
->o_height
;
681 pixm
->field
= V4L2_FIELD_NONE
;
682 pixm
->pixelformat
= frame
->fmt
->fourcc
;
683 pixm
->colorspace
= V4L2_COLORSPACE_JPEG
;
684 pixm
->num_planes
= frame
->fmt
->memplanes
;
686 for (i
= 0; i
< pixm
->num_planes
; ++i
) {
687 pixm
->plane_fmt
[i
].bytesperline
= frame
->bytesperline
[i
];
688 pixm
->plane_fmt
[i
].sizeimage
= frame
->payload
[i
];
693 * fimc_adjust_mplane_format - adjust bytesperline/sizeimage for each plane
694 * @fmt: fimc pixel format description (input)
695 * @width: requested pixel width
696 * @height: requested pixel height
697 * @pix: multi-plane format to adjust
699 void fimc_adjust_mplane_format(struct fimc_fmt
*fmt
, u32 width
, u32 height
,
700 struct v4l2_pix_format_mplane
*pix
)
702 u32 bytesperline
= 0;
705 pix
->colorspace
= V4L2_COLORSPACE_JPEG
;
706 pix
->field
= V4L2_FIELD_NONE
;
707 pix
->num_planes
= fmt
->memplanes
;
708 pix
->pixelformat
= fmt
->fourcc
;
709 pix
->height
= height
;
712 for (i
= 0; i
< pix
->num_planes
; ++i
) {
713 struct v4l2_plane_pix_format
*plane_fmt
= &pix
->plane_fmt
[i
];
714 u32 bpl
= plane_fmt
->bytesperline
;
717 if (fmt
->colplanes
> 1 && (bpl
== 0 || bpl
< pix
->width
))
718 bpl
= pix
->width
; /* Planar */
720 if (fmt
->colplanes
== 1 && /* Packed */
721 (bpl
== 0 || ((bpl
* 8) / fmt
->depth
[i
]) < pix
->width
))
722 bpl
= (pix
->width
* fmt
->depth
[0]) / 8;
724 * Currently bytesperline for each plane is same, except
725 * V4L2_PIX_FMT_YUV420M format. This calculation may need
726 * to be changed when other multi-planar formats are added
727 * to the fimc_formats[] array.
731 else if (i
== 1 && fmt
->memplanes
== 3)
734 plane_fmt
->bytesperline
= bytesperline
;
735 sizeimage
= pix
->width
* pix
->height
* fmt
->depth
[i
] / 8;
737 /* Ensure full last row for tiled formats */
738 if (tiled_fmt(fmt
)) {
739 /* 64 * 32 * plane_fmt->bytesperline / 64 */
740 u32 row_size
= plane_fmt
->bytesperline
* 32;
742 sizeimage
= roundup(sizeimage
, row_size
);
745 plane_fmt
->sizeimage
= max(sizeimage
, plane_fmt
->sizeimage
);
750 * fimc_find_format - lookup fimc color format by fourcc or media bus format
751 * @pixelformat: fourcc to match, ignored if null
752 * @mbus_code: media bus code to match, ignored if null
753 * @mask: the color flags to match
754 * @index: offset in the fimc_formats array, ignored if negative
756 struct fimc_fmt
*fimc_find_format(const u32
*pixelformat
, const u32
*mbus_code
,
757 unsigned int mask
, int index
)
759 struct fimc_fmt
*fmt
, *def_fmt
= NULL
;
763 if (index
>= (int)ARRAY_SIZE(fimc_formats
))
766 for (i
= 0; i
< ARRAY_SIZE(fimc_formats
); ++i
) {
767 fmt
= &fimc_formats
[i
];
768 if (!(fmt
->flags
& mask
))
770 if (pixelformat
&& fmt
->fourcc
== *pixelformat
)
772 if (mbus_code
&& fmt
->mbus_code
== *mbus_code
)
781 static void fimc_clk_put(struct fimc_dev
*fimc
)
784 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++) {
785 if (IS_ERR(fimc
->clock
[i
]))
787 clk_unprepare(fimc
->clock
[i
]);
788 clk_put(fimc
->clock
[i
]);
789 fimc
->clock
[i
] = ERR_PTR(-EINVAL
);
793 static int fimc_clk_get(struct fimc_dev
*fimc
)
797 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++)
798 fimc
->clock
[i
] = ERR_PTR(-EINVAL
);
800 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++) {
801 fimc
->clock
[i
] = clk_get(&fimc
->pdev
->dev
, fimc_clocks
[i
]);
802 if (IS_ERR(fimc
->clock
[i
])) {
803 ret
= PTR_ERR(fimc
->clock
[i
]);
806 ret
= clk_prepare(fimc
->clock
[i
]);
808 clk_put(fimc
->clock
[i
]);
809 fimc
->clock
[i
] = ERR_PTR(-EINVAL
);
816 dev_err(&fimc
->pdev
->dev
, "failed to get clock: %s\n",
822 static int fimc_m2m_suspend(struct fimc_dev
*fimc
)
827 spin_lock_irqsave(&fimc
->slock
, flags
);
828 if (!fimc_m2m_pending(fimc
)) {
829 spin_unlock_irqrestore(&fimc
->slock
, flags
);
832 clear_bit(ST_M2M_SUSPENDED
, &fimc
->state
);
833 set_bit(ST_M2M_SUSPENDING
, &fimc
->state
);
834 spin_unlock_irqrestore(&fimc
->slock
, flags
);
836 timeout
= wait_event_timeout(fimc
->irq_queue
,
837 test_bit(ST_M2M_SUSPENDED
, &fimc
->state
),
838 FIMC_SHUTDOWN_TIMEOUT
);
840 clear_bit(ST_M2M_SUSPENDING
, &fimc
->state
);
841 return timeout
== 0 ? -EAGAIN
: 0;
844 static int fimc_m2m_resume(struct fimc_dev
*fimc
)
846 struct fimc_ctx
*ctx
;
849 spin_lock_irqsave(&fimc
->slock
, flags
);
850 /* Clear for full H/W setup in first run after resume */
852 fimc
->m2m
.ctx
= NULL
;
853 spin_unlock_irqrestore(&fimc
->slock
, flags
);
855 if (test_and_clear_bit(ST_M2M_SUSPENDED
, &fimc
->state
))
856 fimc_m2m_job_finish(ctx
, VB2_BUF_STATE_ERROR
);
860 #endif /* CONFIG_PM */
862 static const struct of_device_id fimc_of_match
[];
864 static int fimc_parse_dt(struct fimc_dev
*fimc
, u32
*clk_freq
)
866 struct device
*dev
= &fimc
->pdev
->dev
;
867 struct device_node
*node
= dev
->of_node
;
868 const struct of_device_id
*of_id
;
869 struct fimc_variant
*v
;
870 struct fimc_pix_limit
*lim
;
871 u32 args
[FIMC_PIX_LIMITS_MAX
];
874 if (of_property_read_bool(node
, "samsung,lcd-wb"))
877 v
= devm_kzalloc(dev
, sizeof(*v
) + sizeof(*lim
), GFP_KERNEL
);
881 of_id
= of_match_node(fimc_of_match
, node
);
884 fimc
->drv_data
= of_id
->data
;
885 ret
= of_property_read_u32_array(node
, "samsung,pix-limits",
886 args
, FIMC_PIX_LIMITS_MAX
);
890 lim
= (struct fimc_pix_limit
*)&v
[1];
892 lim
->scaler_en_w
= args
[0];
893 lim
->scaler_dis_w
= args
[1];
894 lim
->out_rot_en_w
= args
[2];
895 lim
->out_rot_dis_w
= args
[3];
898 ret
= of_property_read_u32_array(node
, "samsung,min-pix-sizes",
900 v
->min_inp_pixsize
= ret
? FIMC_DEF_MIN_SIZE
: args
[0];
901 v
->min_out_pixsize
= ret
? FIMC_DEF_MIN_SIZE
: args
[1];
902 ret
= of_property_read_u32_array(node
, "samsung,min-pix-alignment",
904 v
->min_vsize_align
= ret
? FIMC_DEF_HEIGHT_ALIGN
: args
[0];
905 v
->hor_offs_align
= ret
? FIMC_DEF_HOR_OFFS_ALIGN
: args
[1];
907 ret
= of_property_read_u32(node
, "samsung,rotators", &args
[1]);
908 v
->has_inp_rot
= ret
? 1 : args
[1] & 0x01;
909 v
->has_out_rot
= ret
? 1 : args
[1] & 0x10;
910 v
->has_mainscaler_ext
= of_property_read_bool(node
,
911 "samsung,mainscaler-ext");
913 v
->has_isp_wb
= of_property_read_bool(node
, "samsung,isp-wb");
914 v
->has_cam_if
= of_property_read_bool(node
, "samsung,cam-if");
915 of_property_read_u32(node
, "clock-frequency", clk_freq
);
916 fimc
->id
= of_alias_get_id(node
, "fimc");
922 static int fimc_probe(struct platform_device
*pdev
)
924 struct device
*dev
= &pdev
->dev
;
926 struct fimc_dev
*fimc
;
927 struct resource
*res
;
930 fimc
= devm_kzalloc(dev
, sizeof(*fimc
), GFP_KERNEL
);
937 ret
= fimc_parse_dt(fimc
, &lclk_freq
);
941 fimc
->drv_data
= fimc_get_drvdata(pdev
);
944 if (!fimc
->drv_data
|| fimc
->id
>= fimc
->drv_data
->num_entities
||
946 dev_err(dev
, "Invalid driver data or device id (%d)\n",
951 fimc
->variant
= fimc
->drv_data
->variant
[fimc
->id
];
953 init_waitqueue_head(&fimc
->irq_queue
);
954 spin_lock_init(&fimc
->slock
);
955 mutex_init(&fimc
->lock
);
957 if (fimc
->variant
->has_isp_wb
) {
958 fimc
->sysreg
= fimc_get_sysreg_regmap(dev
->of_node
);
959 if (IS_ERR(fimc
->sysreg
))
960 return PTR_ERR(fimc
->sysreg
);
963 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
964 fimc
->regs
= devm_ioremap_resource(dev
, res
);
965 if (IS_ERR(fimc
->regs
))
966 return PTR_ERR(fimc
->regs
);
968 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
970 dev_err(dev
, "Failed to get IRQ resource\n");
974 ret
= fimc_clk_get(fimc
);
979 lclk_freq
= fimc
->drv_data
->lclk_frequency
;
981 ret
= clk_set_rate(fimc
->clock
[CLK_BUS
], lclk_freq
);
985 ret
= clk_enable(fimc
->clock
[CLK_BUS
]);
989 ret
= devm_request_irq(dev
, res
->start
, fimc_irq_handler
,
990 0, dev_name(dev
), fimc
);
992 dev_err(dev
, "failed to install irq (%d)\n", ret
);
996 ret
= fimc_initialize_capture_subdev(fimc
);
1000 platform_set_drvdata(pdev
, fimc
);
1001 pm_runtime_enable(dev
);
1003 if (!pm_runtime_enabled(dev
)) {
1004 ret
= clk_enable(fimc
->clock
[CLK_GATE
]);
1009 vb2_dma_contig_set_max_seg_size(dev
, DMA_BIT_MASK(32));
1011 dev_dbg(dev
, "FIMC.%d registered successfully\n", fimc
->id
);
1015 fimc_unregister_capture_subdev(fimc
);
1017 clk_disable(fimc
->clock
[CLK_BUS
]);
1023 static int fimc_runtime_resume(struct device
*dev
)
1025 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1027 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1029 /* Enable clocks and perform basic initialization */
1030 clk_enable(fimc
->clock
[CLK_GATE
]);
1031 fimc_hw_reset(fimc
);
1033 /* Resume the capture or mem-to-mem device */
1034 if (fimc_capture_busy(fimc
))
1035 return fimc_capture_resume(fimc
);
1037 return fimc_m2m_resume(fimc
);
1040 static int fimc_runtime_suspend(struct device
*dev
)
1042 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1045 if (fimc_capture_busy(fimc
))
1046 ret
= fimc_capture_suspend(fimc
);
1048 ret
= fimc_m2m_suspend(fimc
);
1050 clk_disable(fimc
->clock
[CLK_GATE
]);
1052 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1057 #ifdef CONFIG_PM_SLEEP
1058 static int fimc_resume(struct device
*dev
)
1060 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1061 unsigned long flags
;
1063 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1065 /* Do not resume if the device was idle before system suspend */
1066 spin_lock_irqsave(&fimc
->slock
, flags
);
1067 if (!test_and_clear_bit(ST_LPM
, &fimc
->state
) ||
1068 (!fimc_m2m_active(fimc
) && !fimc_capture_busy(fimc
))) {
1069 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1072 fimc_hw_reset(fimc
);
1073 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1075 if (fimc_capture_busy(fimc
))
1076 return fimc_capture_resume(fimc
);
1078 return fimc_m2m_resume(fimc
);
1081 static int fimc_suspend(struct device
*dev
)
1083 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1085 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1087 if (test_and_set_bit(ST_LPM
, &fimc
->state
))
1089 if (fimc_capture_busy(fimc
))
1090 return fimc_capture_suspend(fimc
);
1092 return fimc_m2m_suspend(fimc
);
1094 #endif /* CONFIG_PM_SLEEP */
1096 static int fimc_remove(struct platform_device
*pdev
)
1098 struct fimc_dev
*fimc
= platform_get_drvdata(pdev
);
1100 pm_runtime_disable(&pdev
->dev
);
1101 if (!pm_runtime_status_suspended(&pdev
->dev
))
1102 clk_disable(fimc
->clock
[CLK_GATE
]);
1103 pm_runtime_set_suspended(&pdev
->dev
);
1105 fimc_unregister_capture_subdev(fimc
);
1106 vb2_dma_contig_clear_max_seg_size(&pdev
->dev
);
1108 clk_disable(fimc
->clock
[CLK_BUS
]);
1111 dev_info(&pdev
->dev
, "driver unloaded\n");
1115 /* S5PV210, S5PC110 */
1116 static const struct fimc_drvdata fimc_drvdata_s5pv210
= {
1118 .lclk_frequency
= 166000000UL,
1123 /* EXYNOS4210, S5PV310, S5PC210 */
1124 static const struct fimc_drvdata fimc_drvdata_exynos4210
= {
1126 .lclk_frequency
= 166000000UL,
1130 .out_buf_count
= 32,
1134 static const struct fimc_drvdata fimc_drvdata_exynos4x12
= {
1136 .lclk_frequency
= 166000000UL,
1140 .out_buf_count
= 32,
1143 static const struct of_device_id fimc_of_match
[] = {
1145 .compatible
= "samsung,s5pv210-fimc",
1146 .data
= &fimc_drvdata_s5pv210
,
1148 .compatible
= "samsung,exynos4210-fimc",
1149 .data
= &fimc_drvdata_exynos4210
,
1151 .compatible
= "samsung,exynos4212-fimc",
1152 .data
= &fimc_drvdata_exynos4x12
,
1157 static const struct dev_pm_ops fimc_pm_ops
= {
1158 SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend
, fimc_resume
)
1159 SET_RUNTIME_PM_OPS(fimc_runtime_suspend
, fimc_runtime_resume
, NULL
)
1162 static struct platform_driver fimc_driver
= {
1163 .probe
= fimc_probe
,
1164 .remove
= fimc_remove
,
1166 .of_match_table
= fimc_of_match
,
1167 .name
= FIMC_DRIVER_NAME
,
1172 int __init
fimc_register_driver(void)
1174 return platform_driver_register(&fimc_driver
);
1177 void __exit
fimc_unregister_driver(void)
1179 platform_driver_unregister(&fimc_driver
);