1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
6 * Samsung EXYNOS5 SoC series G-Scaler driver
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/workqueue.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
18 #include <linux/list.h>
20 #include <linux/slab.h>
21 #include <linux/clk.h>
23 #include <media/v4l2-ioctl.h>
27 static const struct gsc_fmt gsc_formats
[] = {
29 .pixelformat
= V4L2_PIX_FMT_RGB565X
,
35 .pixelformat
= V4L2_PIX_FMT_BGR32
,
41 .pixelformat
= V4L2_PIX_FMT_YUYV
,
48 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
50 .pixelformat
= V4L2_PIX_FMT_UYVY
,
57 .mbus_code
= MEDIA_BUS_FMT_UYVY8_2X8
,
59 .pixelformat
= V4L2_PIX_FMT_VYUY
,
66 .mbus_code
= MEDIA_BUS_FMT_VYUY8_2X8
,
68 .pixelformat
= V4L2_PIX_FMT_YVYU
,
75 .mbus_code
= MEDIA_BUS_FMT_YVYU8_2X8
,
77 .pixelformat
= V4L2_PIX_FMT_YUV32
,
85 .pixelformat
= V4L2_PIX_FMT_YUV422P
,
93 .pixelformat
= V4L2_PIX_FMT_NV16
,
101 .pixelformat
= V4L2_PIX_FMT_NV16M
,
109 .pixelformat
= V4L2_PIX_FMT_NV61
,
117 .pixelformat
= V4L2_PIX_FMT_NV61M
,
125 .pixelformat
= V4L2_PIX_FMT_YUV420
,
133 .pixelformat
= V4L2_PIX_FMT_YVU420
,
142 .pixelformat
= V4L2_PIX_FMT_NV12
,
150 .pixelformat
= V4L2_PIX_FMT_NV21
,
158 .pixelformat
= V4L2_PIX_FMT_NV21M
,
166 .pixelformat
= V4L2_PIX_FMT_NV12M
,
174 .pixelformat
= V4L2_PIX_FMT_YUV420M
,
175 .depth
= { 8, 2, 2 },
182 .pixelformat
= V4L2_PIX_FMT_YVU420M
,
183 .depth
= { 8, 2, 2 },
190 .pixelformat
= V4L2_PIX_FMT_NV12MT_16X16
,
200 const struct gsc_fmt
*get_format(int index
)
202 if (index
>= ARRAY_SIZE(gsc_formats
))
205 return (struct gsc_fmt
*)&gsc_formats
[index
];
208 const struct gsc_fmt
*find_fmt(u32
*pixelformat
, u32
*mbus_code
, u32 index
)
210 const struct gsc_fmt
*fmt
, *def_fmt
= NULL
;
213 if (index
>= ARRAY_SIZE(gsc_formats
))
216 for (i
= 0; i
< ARRAY_SIZE(gsc_formats
); ++i
) {
218 if (pixelformat
&& fmt
->pixelformat
== *pixelformat
)
220 if (mbus_code
&& fmt
->mbus_code
== *mbus_code
)
229 void gsc_set_frame_size(struct gsc_frame
*frame
, int width
, int height
)
231 frame
->f_width
= width
;
232 frame
->f_height
= height
;
233 frame
->crop
.width
= width
;
234 frame
->crop
.height
= height
;
235 frame
->crop
.left
= 0;
239 int gsc_cal_prescaler_ratio(struct gsc_variant
*var
, u32 src
, u32 dst
,
242 if ((dst
> src
) || (dst
>= src
/ var
->poly_sc_down_max
)) {
247 if ((src
/ var
->poly_sc_down_max
/ var
->pre_sc_down_max
) > dst
) {
248 pr_err("Exceeded maximum downscaling ratio (1/16))");
252 *ratio
= (dst
> (src
/ 8)) ? 2 : 4;
257 void gsc_get_prescaler_shfactor(u32 hratio
, u32 vratio
, u32
*sh
)
259 if (hratio
== 4 && vratio
== 4)
261 else if ((hratio
== 4 && vratio
== 2) ||
262 (hratio
== 2 && vratio
== 4))
264 else if ((hratio
== 4 && vratio
== 1) ||
265 (hratio
== 1 && vratio
== 4) ||
266 (hratio
== 2 && vratio
== 2))
268 else if (hratio
== 1 && vratio
== 1)
274 void gsc_check_src_scale_info(struct gsc_variant
*var
,
275 struct gsc_frame
*s_frame
, u32
*wratio
,
276 u32 tx
, u32 ty
, u32
*hratio
)
278 int remainder
= 0, walign
, halign
;
280 if (is_yuv420(s_frame
->fmt
->color
)) {
281 walign
= GSC_SC_ALIGN_4
;
282 halign
= GSC_SC_ALIGN_4
;
283 } else if (is_yuv422(s_frame
->fmt
->color
)) {
284 walign
= GSC_SC_ALIGN_4
;
285 halign
= GSC_SC_ALIGN_2
;
287 walign
= GSC_SC_ALIGN_2
;
288 halign
= GSC_SC_ALIGN_2
;
291 remainder
= s_frame
->crop
.width
% (*wratio
* walign
);
293 s_frame
->crop
.width
-= remainder
;
294 gsc_cal_prescaler_ratio(var
, s_frame
->crop
.width
, tx
, wratio
);
295 pr_info("cropped src width size is recalculated from %d to %d",
296 s_frame
->crop
.width
+ remainder
, s_frame
->crop
.width
);
299 remainder
= s_frame
->crop
.height
% (*hratio
* halign
);
301 s_frame
->crop
.height
-= remainder
;
302 gsc_cal_prescaler_ratio(var
, s_frame
->crop
.height
, ty
, hratio
);
303 pr_info("cropped src height size is recalculated from %d to %d",
304 s_frame
->crop
.height
+ remainder
, s_frame
->crop
.height
);
308 int gsc_enum_fmt(struct v4l2_fmtdesc
*f
)
310 const struct gsc_fmt
*fmt
;
312 fmt
= find_fmt(NULL
, NULL
, f
->index
);
316 f
->pixelformat
= fmt
->pixelformat
;
321 static int get_plane_info(struct gsc_frame
*frm
, u32 addr
, u32
*index
, u32
*ret_addr
)
323 if (frm
->addr
.y
== addr
) {
325 *ret_addr
= frm
->addr
.y
;
326 } else if (frm
->addr
.cb
== addr
) {
328 *ret_addr
= frm
->addr
.cb
;
329 } else if (frm
->addr
.cr
== addr
) {
331 *ret_addr
= frm
->addr
.cr
;
333 pr_err("Plane address is wrong");
339 void gsc_set_prefbuf(struct gsc_dev
*gsc
, struct gsc_frame
*frm
)
341 u32 f_chk_addr
, f_chk_len
, s_chk_addr
= 0, s_chk_len
= 0;
343 f_chk_addr
= frm
->addr
.y
;
344 f_chk_len
= frm
->payload
[0];
345 if (frm
->fmt
->num_planes
== 2) {
346 s_chk_addr
= frm
->addr
.cb
;
347 s_chk_len
= frm
->payload
[1];
348 } else if (frm
->fmt
->num_planes
== 3) {
349 u32 low_addr
, low_plane
, mid_addr
, mid_plane
;
350 u32 high_addr
, high_plane
;
353 t_min
= min3(frm
->addr
.y
, frm
->addr
.cb
, frm
->addr
.cr
);
354 if (get_plane_info(frm
, t_min
, &low_plane
, &low_addr
))
356 t_max
= max3(frm
->addr
.y
, frm
->addr
.cb
, frm
->addr
.cr
);
357 if (get_plane_info(frm
, t_max
, &high_plane
, &high_addr
))
360 mid_plane
= 3 - (low_plane
+ high_plane
);
362 mid_addr
= frm
->addr
.y
;
363 else if (mid_plane
== 1)
364 mid_addr
= frm
->addr
.cb
;
365 else if (mid_plane
== 2)
366 mid_addr
= frm
->addr
.cr
;
370 f_chk_addr
= low_addr
;
371 if (mid_addr
+ frm
->payload
[mid_plane
] - low_addr
>
372 high_addr
+ frm
->payload
[high_plane
] - mid_addr
) {
373 f_chk_len
= frm
->payload
[low_plane
];
374 s_chk_addr
= mid_addr
;
375 s_chk_len
= high_addr
+
376 frm
->payload
[high_plane
] - mid_addr
;
378 f_chk_len
= mid_addr
+
379 frm
->payload
[mid_plane
] - low_addr
;
380 s_chk_addr
= high_addr
;
381 s_chk_len
= frm
->payload
[high_plane
];
384 pr_debug("f_addr = 0x%08x, f_len = %d, s_addr = 0x%08x, s_len = %d\n",
385 f_chk_addr
, f_chk_len
, s_chk_addr
, s_chk_len
);
388 int gsc_try_fmt_mplane(struct gsc_ctx
*ctx
, struct v4l2_format
*f
)
390 struct gsc_dev
*gsc
= ctx
->gsc_dev
;
391 struct gsc_variant
*variant
= gsc
->variant
;
392 struct v4l2_pix_format_mplane
*pix_mp
= &f
->fmt
.pix_mp
;
393 const struct gsc_fmt
*fmt
;
394 u32 max_w
, max_h
, mod_x
, mod_y
;
395 u32 min_w
, min_h
, tmp_w
, tmp_h
;
398 pr_debug("user put w: %d, h: %d", pix_mp
->width
, pix_mp
->height
);
400 fmt
= find_fmt(&pix_mp
->pixelformat
, NULL
, 0);
402 pr_err("pixelformat format (0x%X) invalid\n",
403 pix_mp
->pixelformat
);
407 if (pix_mp
->field
== V4L2_FIELD_ANY
)
408 pix_mp
->field
= V4L2_FIELD_NONE
;
409 else if (pix_mp
->field
!= V4L2_FIELD_NONE
) {
410 pr_debug("Not supported field order(%d)\n", pix_mp
->field
);
414 max_w
= variant
->pix_max
->target_rot_dis_w
;
415 max_h
= variant
->pix_max
->target_rot_dis_h
;
417 mod_x
= ffs(variant
->pix_align
->org_w
) - 1;
418 if (is_yuv420(fmt
->color
))
419 mod_y
= ffs(variant
->pix_align
->org_h
) - 1;
421 mod_y
= ffs(variant
->pix_align
->org_h
) - 2;
423 if (V4L2_TYPE_IS_OUTPUT(f
->type
)) {
424 min_w
= variant
->pix_min
->org_w
;
425 min_h
= variant
->pix_min
->org_h
;
427 min_w
= variant
->pix_min
->target_rot_dis_w
;
428 min_h
= variant
->pix_min
->target_rot_dis_h
;
429 pix_mp
->colorspace
= ctx
->out_colorspace
;
432 pr_debug("mod_x: %d, mod_y: %d, max_w: %d, max_h = %d",
433 mod_x
, mod_y
, max_w
, max_h
);
435 /* To check if image size is modified to adjust parameter against
436 hardware abilities */
437 tmp_w
= pix_mp
->width
;
438 tmp_h
= pix_mp
->height
;
440 v4l_bound_align_image(&pix_mp
->width
, min_w
, max_w
, mod_x
,
441 &pix_mp
->height
, min_h
, max_h
, mod_y
, 0);
442 if (tmp_w
!= pix_mp
->width
|| tmp_h
!= pix_mp
->height
)
443 pr_debug("Image size has been modified from %dx%d to %dx%d\n",
444 tmp_w
, tmp_h
, pix_mp
->width
, pix_mp
->height
);
446 pix_mp
->num_planes
= fmt
->num_planes
;
448 if (V4L2_TYPE_IS_OUTPUT(f
->type
))
449 ctx
->out_colorspace
= pix_mp
->colorspace
;
451 for (i
= 0; i
< pix_mp
->num_planes
; ++i
) {
452 struct v4l2_plane_pix_format
*plane_fmt
= &pix_mp
->plane_fmt
[i
];
453 u32 bpl
= plane_fmt
->bytesperline
;
455 if (fmt
->num_comp
== 1 && /* Packed */
456 (bpl
== 0 || (bpl
* 8 / fmt
->depth
[i
]) < pix_mp
->width
))
457 bpl
= pix_mp
->width
* fmt
->depth
[i
] / 8;
459 if (fmt
->num_comp
> 1 && /* Planar */
460 (bpl
== 0 || bpl
< pix_mp
->width
))
463 if (i
!= 0 && fmt
->num_comp
== 3)
466 plane_fmt
->bytesperline
= bpl
;
467 plane_fmt
->sizeimage
= max(pix_mp
->width
* pix_mp
->height
*
469 plane_fmt
->sizeimage
);
470 pr_debug("[%d]: bpl: %d, sizeimage: %d",
471 i
, bpl
, pix_mp
->plane_fmt
[i
].sizeimage
);
477 int gsc_g_fmt_mplane(struct gsc_ctx
*ctx
, struct v4l2_format
*f
)
479 struct gsc_frame
*frame
;
480 struct v4l2_pix_format_mplane
*pix_mp
;
483 frame
= ctx_get_frame(ctx
, f
->type
);
485 return PTR_ERR(frame
);
487 pix_mp
= &f
->fmt
.pix_mp
;
489 pix_mp
->width
= frame
->f_width
;
490 pix_mp
->height
= frame
->f_height
;
491 pix_mp
->field
= V4L2_FIELD_NONE
;
492 pix_mp
->pixelformat
= frame
->fmt
->pixelformat
;
493 pix_mp
->num_planes
= frame
->fmt
->num_planes
;
494 pix_mp
->colorspace
= ctx
->out_colorspace
;
496 for (i
= 0; i
< pix_mp
->num_planes
; ++i
) {
497 pix_mp
->plane_fmt
[i
].bytesperline
= (frame
->f_width
*
498 frame
->fmt
->depth
[i
]) / 8;
499 pix_mp
->plane_fmt
[i
].sizeimage
=
500 pix_mp
->plane_fmt
[i
].bytesperline
* frame
->f_height
;
506 void gsc_check_crop_change(u32 tmp_w
, u32 tmp_h
, u32
*w
, u32
*h
)
508 if (tmp_w
!= *w
|| tmp_h
!= *h
) {
509 pr_info("Cropped size has been modified from %dx%d to %dx%d",
510 *w
, *h
, tmp_w
, tmp_h
);
516 int gsc_try_selection(struct gsc_ctx
*ctx
, struct v4l2_selection
*s
)
519 struct gsc_dev
*gsc
= ctx
->gsc_dev
;
520 struct gsc_variant
*variant
= gsc
->variant
;
521 u32 mod_x
= 0, mod_y
= 0, tmp_w
, tmp_h
;
522 u32 min_w
, min_h
, max_w
, max_h
;
524 if (s
->r
.top
< 0 || s
->r
.left
< 0) {
525 pr_err("doesn't support negative values for top & left\n");
528 pr_debug("user put w: %d, h: %d", s
->r
.width
, s
->r
.height
);
530 if (s
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
532 else if (s
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
542 if (V4L2_TYPE_IS_OUTPUT(s
->type
)) {
543 if ((is_yuv422(f
->fmt
->color
) && f
->fmt
->num_comp
== 1) ||
544 is_rgb(f
->fmt
->color
))
548 if ((is_yuv422(f
->fmt
->color
) && f
->fmt
->num_comp
== 3) ||
549 is_yuv420(f
->fmt
->color
))
554 if (is_yuv420(f
->fmt
->color
) || is_yuv422(f
->fmt
->color
))
555 mod_x
= ffs(variant
->pix_align
->target_w
) - 1;
556 if (is_yuv420(f
->fmt
->color
))
557 mod_y
= ffs(variant
->pix_align
->target_h
) - 1;
558 if (ctx
->gsc_ctrls
.rotate
->val
== 90 ||
559 ctx
->gsc_ctrls
.rotate
->val
== 270) {
562 min_w
= variant
->pix_min
->target_rot_en_w
;
563 min_h
= variant
->pix_min
->target_rot_en_h
;
567 min_w
= variant
->pix_min
->target_rot_dis_w
;
568 min_h
= variant
->pix_min
->target_rot_dis_h
;
571 pr_debug("mod_x: %d, mod_y: %d, min_w: %d, min_h = %d",
572 mod_x
, mod_y
, min_w
, min_h
);
573 pr_debug("tmp_w : %d, tmp_h : %d", tmp_w
, tmp_h
);
575 v4l_bound_align_image(&tmp_w
, min_w
, max_w
, mod_x
,
576 &tmp_h
, min_h
, max_h
, mod_y
, 0);
578 if (V4L2_TYPE_IS_CAPTURE(s
->type
) &&
579 (ctx
->gsc_ctrls
.rotate
->val
== 90 ||
580 ctx
->gsc_ctrls
.rotate
->val
== 270))
581 gsc_check_crop_change(tmp_h
, tmp_w
,
582 &s
->r
.width
, &s
->r
.height
);
584 gsc_check_crop_change(tmp_w
, tmp_h
,
585 &s
->r
.width
, &s
->r
.height
);
588 /* adjust left/top if cropping rectangle is out of bounds */
589 /* Need to add code to algin left value with 2's multiple */
590 if (s
->r
.left
+ tmp_w
> max_w
)
591 s
->r
.left
= max_w
- tmp_w
;
592 if (s
->r
.top
+ tmp_h
> max_h
)
593 s
->r
.top
= max_h
- tmp_h
;
595 if ((is_yuv420(f
->fmt
->color
) || is_yuv422(f
->fmt
->color
)) &&
599 pr_debug("Aligned l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
600 s
->r
.left
, s
->r
.top
, s
->r
.width
, s
->r
.height
, max_w
, max_h
);
605 int gsc_check_scaler_ratio(struct gsc_variant
*var
, int sw
, int sh
, int dw
,
606 int dh
, int rot
, int out_path
)
608 int tmp_w
, tmp_h
, sc_down_max
;
610 if (out_path
== GSC_DMA
)
611 sc_down_max
= var
->sc_down_max
;
613 sc_down_max
= var
->local_sc_down
;
615 if (rot
== 90 || rot
== 270) {
623 if ((sw
/ tmp_w
) > sc_down_max
||
624 (sh
/ tmp_h
) > sc_down_max
||
625 (tmp_w
/ sw
) > var
->sc_up_max
||
626 (tmp_h
/ sh
) > var
->sc_up_max
)
632 int gsc_set_scaler_info(struct gsc_ctx
*ctx
)
634 struct gsc_scaler
*sc
= &ctx
->scaler
;
635 struct gsc_frame
*s_frame
= &ctx
->s_frame
;
636 struct gsc_frame
*d_frame
= &ctx
->d_frame
;
637 struct gsc_variant
*variant
= ctx
->gsc_dev
->variant
;
638 struct device
*dev
= &ctx
->gsc_dev
->pdev
->dev
;
642 ret
= gsc_check_scaler_ratio(variant
, s_frame
->crop
.width
,
643 s_frame
->crop
.height
, d_frame
->crop
.width
, d_frame
->crop
.height
,
644 ctx
->gsc_ctrls
.rotate
->val
, ctx
->out_path
);
646 pr_err("out of scaler range");
650 if (ctx
->gsc_ctrls
.rotate
->val
== 90 ||
651 ctx
->gsc_ctrls
.rotate
->val
== 270) {
652 ty
= d_frame
->crop
.width
;
653 tx
= d_frame
->crop
.height
;
655 tx
= d_frame
->crop
.width
;
656 ty
= d_frame
->crop
.height
;
659 if (tx
<= 0 || ty
<= 0) {
660 dev_err(dev
, "Invalid target size: %dx%d", tx
, ty
);
664 ret
= gsc_cal_prescaler_ratio(variant
, s_frame
->crop
.width
,
665 tx
, &sc
->pre_hratio
);
667 pr_err("Horizontal scale ratio is out of range");
671 ret
= gsc_cal_prescaler_ratio(variant
, s_frame
->crop
.height
,
672 ty
, &sc
->pre_vratio
);
674 pr_err("Vertical scale ratio is out of range");
678 gsc_check_src_scale_info(variant
, s_frame
, &sc
->pre_hratio
,
679 tx
, ty
, &sc
->pre_vratio
);
681 gsc_get_prescaler_shfactor(sc
->pre_hratio
, sc
->pre_vratio
,
684 sc
->main_hratio
= (s_frame
->crop
.width
<< 16) / tx
;
685 sc
->main_vratio
= (s_frame
->crop
.height
<< 16) / ty
;
687 pr_debug("scaler input/output size : sx = %d, sy = %d, tx = %d, ty = %d",
688 s_frame
->crop
.width
, s_frame
->crop
.height
, tx
, ty
);
689 pr_debug("scaler ratio info : pre_shfactor : %d, pre_h : %d",
690 sc
->pre_shfactor
, sc
->pre_hratio
);
691 pr_debug("pre_v :%d, main_h : %d, main_v : %d",
692 sc
->pre_vratio
, sc
->main_hratio
, sc
->main_vratio
);
697 static int __gsc_s_ctrl(struct gsc_ctx
*ctx
, struct v4l2_ctrl
*ctrl
)
699 struct gsc_dev
*gsc
= ctx
->gsc_dev
;
700 struct gsc_variant
*variant
= gsc
->variant
;
701 unsigned int flags
= GSC_DST_FMT
| GSC_SRC_FMT
;
704 if (ctrl
->flags
& V4L2_CTRL_FLAG_INACTIVE
)
709 ctx
->hflip
= ctrl
->val
;
713 ctx
->vflip
= ctrl
->val
;
716 case V4L2_CID_ROTATE
:
717 if ((ctx
->state
& flags
) == flags
) {
718 ret
= gsc_check_scaler_ratio(variant
,
719 ctx
->s_frame
.crop
.width
,
720 ctx
->s_frame
.crop
.height
,
721 ctx
->d_frame
.crop
.width
,
722 ctx
->d_frame
.crop
.height
,
723 ctx
->gsc_ctrls
.rotate
->val
,
730 ctx
->rotation
= ctrl
->val
;
733 case V4L2_CID_ALPHA_COMPONENT
:
734 ctx
->d_frame
.alpha
= ctrl
->val
;
738 ctx
->state
|= GSC_PARAMS
;
742 static int gsc_s_ctrl(struct v4l2_ctrl
*ctrl
)
744 struct gsc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
748 spin_lock_irqsave(&ctx
->gsc_dev
->slock
, flags
);
749 ret
= __gsc_s_ctrl(ctx
, ctrl
);
750 spin_unlock_irqrestore(&ctx
->gsc_dev
->slock
, flags
);
755 static const struct v4l2_ctrl_ops gsc_ctrl_ops
= {
756 .s_ctrl
= gsc_s_ctrl
,
759 int gsc_ctrls_create(struct gsc_ctx
*ctx
)
761 if (ctx
->ctrls_rdy
) {
762 pr_err("Control handler of this context was created already");
766 v4l2_ctrl_handler_init(&ctx
->ctrl_handler
, GSC_MAX_CTRL_NUM
);
768 ctx
->gsc_ctrls
.rotate
= v4l2_ctrl_new_std(&ctx
->ctrl_handler
,
769 &gsc_ctrl_ops
, V4L2_CID_ROTATE
, 0, 270, 90, 0);
770 ctx
->gsc_ctrls
.hflip
= v4l2_ctrl_new_std(&ctx
->ctrl_handler
,
771 &gsc_ctrl_ops
, V4L2_CID_HFLIP
, 0, 1, 1, 0);
772 ctx
->gsc_ctrls
.vflip
= v4l2_ctrl_new_std(&ctx
->ctrl_handler
,
773 &gsc_ctrl_ops
, V4L2_CID_VFLIP
, 0, 1, 1, 0);
774 ctx
->gsc_ctrls
.global_alpha
= v4l2_ctrl_new_std(&ctx
->ctrl_handler
,
775 &gsc_ctrl_ops
, V4L2_CID_ALPHA_COMPONENT
, 0, 255, 1, 0);
777 ctx
->ctrls_rdy
= ctx
->ctrl_handler
.error
== 0;
779 if (ctx
->ctrl_handler
.error
) {
780 int err
= ctx
->ctrl_handler
.error
;
781 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
782 pr_err("Failed to create G-Scaler control handlers");
789 void gsc_ctrls_delete(struct gsc_ctx
*ctx
)
791 if (ctx
->ctrls_rdy
) {
792 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
793 ctx
->ctrls_rdy
= false;
797 /* The color format (num_comp, num_planes) must be already configured. */
798 int gsc_prepare_addr(struct gsc_ctx
*ctx
, struct vb2_buffer
*vb
,
799 struct gsc_frame
*frame
, struct gsc_addr
*addr
)
804 if ((vb
== NULL
) || (frame
== NULL
))
807 pix_size
= frame
->f_width
* frame
->f_height
;
809 pr_debug("num_planes= %d, num_comp= %d, pix_size= %d",
810 frame
->fmt
->num_planes
, frame
->fmt
->num_comp
, pix_size
);
812 addr
->y
= vb2_dma_contig_plane_dma_addr(vb
, 0);
814 if (frame
->fmt
->num_planes
== 1) {
815 switch (frame
->fmt
->num_comp
) {
821 /* decompose Y into Y/Cb */
822 addr
->cb
= (dma_addr_t
)(addr
->y
+ pix_size
);
826 /* decompose Y into Y/Cb/Cr */
827 addr
->cb
= (dma_addr_t
)(addr
->y
+ pix_size
);
828 if (GSC_YUV420
== frame
->fmt
->color
)
829 addr
->cr
= (dma_addr_t
)(addr
->cb
832 addr
->cr
= (dma_addr_t
)(addr
->cb
836 pr_err("Invalid the number of color planes");
840 if (frame
->fmt
->num_planes
>= 2)
841 addr
->cb
= vb2_dma_contig_plane_dma_addr(vb
, 1);
843 if (frame
->fmt
->num_planes
== 3)
844 addr
->cr
= vb2_dma_contig_plane_dma_addr(vb
, 2);
847 if ((frame
->fmt
->pixelformat
== V4L2_PIX_FMT_VYUY
) ||
848 (frame
->fmt
->pixelformat
== V4L2_PIX_FMT_YVYU
) ||
849 (frame
->fmt
->pixelformat
== V4L2_PIX_FMT_YVU420
) ||
850 (frame
->fmt
->pixelformat
== V4L2_PIX_FMT_YVU420M
))
851 swap(addr
->cb
, addr
->cr
);
853 pr_debug("ADDR: y= %pad cb= %pad cr= %pad ret= %d",
854 &addr
->y
, &addr
->cb
, &addr
->cr
, ret
);
859 static irqreturn_t
gsc_irq_handler(int irq
, void *priv
)
861 struct gsc_dev
*gsc
= priv
;
865 gsc_irq
= gsc_hw_get_irq_status(gsc
);
866 gsc_hw_clear_irq(gsc
, gsc_irq
);
868 if (gsc_irq
== GSC_IRQ_OVERRUN
) {
869 pr_err("Local path input over-run interrupt has occurred!\n");
873 spin_lock(&gsc
->slock
);
875 if (test_and_clear_bit(ST_M2M_PEND
, &gsc
->state
)) {
877 gsc_hw_enable_control(gsc
, false);
879 if (test_and_clear_bit(ST_M2M_SUSPENDING
, &gsc
->state
)) {
880 set_bit(ST_M2M_SUSPENDED
, &gsc
->state
);
881 wake_up(&gsc
->irq_queue
);
884 ctx
= v4l2_m2m_get_curr_priv(gsc
->m2m
.m2m_dev
);
886 if (!ctx
|| !ctx
->m2m_ctx
)
889 spin_unlock(&gsc
->slock
);
890 gsc_m2m_job_finish(ctx
, VB2_BUF_STATE_DONE
);
892 /* wake_up job_abort, stop_streaming */
893 if (ctx
->state
& GSC_CTX_STOP_REQ
) {
894 ctx
->state
&= ~GSC_CTX_STOP_REQ
;
895 wake_up(&gsc
->irq_queue
);
901 spin_unlock(&gsc
->slock
);
905 static struct gsc_pix_max gsc_v_100_max
= {
906 .org_scaler_bypass_w
= 8192,
907 .org_scaler_bypass_h
= 8192,
908 .org_scaler_input_w
= 4800,
909 .org_scaler_input_h
= 3344,
910 .real_rot_dis_w
= 4800,
911 .real_rot_dis_h
= 3344,
912 .real_rot_en_w
= 2047,
913 .real_rot_en_h
= 2047,
914 .target_rot_dis_w
= 4800,
915 .target_rot_dis_h
= 3344,
916 .target_rot_en_w
= 2016,
917 .target_rot_en_h
= 2016,
920 static struct gsc_pix_max gsc_v_5250_max
= {
921 .org_scaler_bypass_w
= 8192,
922 .org_scaler_bypass_h
= 8192,
923 .org_scaler_input_w
= 4800,
924 .org_scaler_input_h
= 3344,
925 .real_rot_dis_w
= 4800,
926 .real_rot_dis_h
= 3344,
927 .real_rot_en_w
= 2016,
928 .real_rot_en_h
= 2016,
929 .target_rot_dis_w
= 4800,
930 .target_rot_dis_h
= 3344,
931 .target_rot_en_w
= 2016,
932 .target_rot_en_h
= 2016,
935 static struct gsc_pix_max gsc_v_5420_max
= {
936 .org_scaler_bypass_w
= 8192,
937 .org_scaler_bypass_h
= 8192,
938 .org_scaler_input_w
= 4800,
939 .org_scaler_input_h
= 3344,
940 .real_rot_dis_w
= 4800,
941 .real_rot_dis_h
= 3344,
942 .real_rot_en_w
= 2048,
943 .real_rot_en_h
= 2048,
944 .target_rot_dis_w
= 4800,
945 .target_rot_dis_h
= 3344,
946 .target_rot_en_w
= 2016,
947 .target_rot_en_h
= 2016,
950 static struct gsc_pix_max gsc_v_5433_max
= {
951 .org_scaler_bypass_w
= 8192,
952 .org_scaler_bypass_h
= 8192,
953 .org_scaler_input_w
= 4800,
954 .org_scaler_input_h
= 3344,
955 .real_rot_dis_w
= 4800,
956 .real_rot_dis_h
= 3344,
957 .real_rot_en_w
= 2047,
958 .real_rot_en_h
= 2047,
959 .target_rot_dis_w
= 4800,
960 .target_rot_dis_h
= 3344,
961 .target_rot_en_w
= 2016,
962 .target_rot_en_h
= 2016,
965 static struct gsc_pix_min gsc_v_100_min
= {
970 .target_rot_dis_w
= 64,
971 .target_rot_dis_h
= 32,
972 .target_rot_en_w
= 32,
973 .target_rot_en_h
= 16,
976 static struct gsc_pix_align gsc_v_100_align
= {
978 .org_w
= 16, /* yuv420 : 16, others : 8 */
979 .offset_h
= 2, /* yuv420/422 : 2, others : 1 */
980 .real_w
= 16, /* yuv420/422 : 4~16, others : 2~8 */
981 .real_h
= 16, /* yuv420 : 4~16, others : 1 */
982 .target_w
= 2, /* yuv420/422 : 2, others : 1 */
983 .target_h
= 2, /* yuv420 : 2, others : 1 */
986 static struct gsc_variant gsc_v_100_variant
= {
987 .pix_max
= &gsc_v_100_max
,
988 .pix_min
= &gsc_v_100_min
,
989 .pix_align
= &gsc_v_100_align
,
994 .poly_sc_down_max
= 4,
995 .pre_sc_down_max
= 4,
999 static struct gsc_variant gsc_v_5250_variant
= {
1000 .pix_max
= &gsc_v_5250_max
,
1001 .pix_min
= &gsc_v_100_min
,
1002 .pix_align
= &gsc_v_100_align
,
1007 .poly_sc_down_max
= 4,
1008 .pre_sc_down_max
= 4,
1012 static struct gsc_variant gsc_v_5420_variant
= {
1013 .pix_max
= &gsc_v_5420_max
,
1014 .pix_min
= &gsc_v_100_min
,
1015 .pix_align
= &gsc_v_100_align
,
1020 .poly_sc_down_max
= 4,
1021 .pre_sc_down_max
= 4,
1025 static struct gsc_variant gsc_v_5433_variant
= {
1026 .pix_max
= &gsc_v_5433_max
,
1027 .pix_min
= &gsc_v_100_min
,
1028 .pix_align
= &gsc_v_100_align
,
1033 .poly_sc_down_max
= 4,
1034 .pre_sc_down_max
= 4,
1038 static struct gsc_driverdata gsc_v_100_drvdata
= {
1040 [0] = &gsc_v_100_variant
,
1041 [1] = &gsc_v_100_variant
,
1042 [2] = &gsc_v_100_variant
,
1043 [3] = &gsc_v_100_variant
,
1046 .clk_names
= { "gscl" },
1050 static struct gsc_driverdata gsc_v_5250_drvdata
= {
1052 [0] = &gsc_v_5250_variant
,
1053 [1] = &gsc_v_5250_variant
,
1054 [2] = &gsc_v_5250_variant
,
1055 [3] = &gsc_v_5250_variant
,
1058 .clk_names
= { "gscl" },
1062 static struct gsc_driverdata gsc_v_5420_drvdata
= {
1064 [0] = &gsc_v_5420_variant
,
1065 [1] = &gsc_v_5420_variant
,
1068 .clk_names
= { "gscl" },
1072 static struct gsc_driverdata gsc_5433_drvdata
= {
1074 [0] = &gsc_v_5433_variant
,
1075 [1] = &gsc_v_5433_variant
,
1076 [2] = &gsc_v_5433_variant
,
1079 .clk_names
= { "pclk", "aclk", "aclk_xiu", "aclk_gsclbend" },
1083 static const struct of_device_id exynos_gsc_match
[] = {
1085 .compatible
= "samsung,exynos5250-gsc",
1086 .data
= &gsc_v_5250_drvdata
,
1089 .compatible
= "samsung,exynos5420-gsc",
1090 .data
= &gsc_v_5420_drvdata
,
1093 .compatible
= "samsung,exynos5433-gsc",
1094 .data
= &gsc_5433_drvdata
,
1097 .compatible
= "samsung,exynos5-gsc",
1098 .data
= &gsc_v_100_drvdata
,
1102 MODULE_DEVICE_TABLE(of
, exynos_gsc_match
);
1104 static int gsc_probe(struct platform_device
*pdev
)
1106 struct gsc_dev
*gsc
;
1107 struct device
*dev
= &pdev
->dev
;
1108 const struct gsc_driverdata
*drv_data
= of_device_get_match_data(dev
);
1113 gsc
= devm_kzalloc(dev
, sizeof(struct gsc_dev
), GFP_KERNEL
);
1117 ret
= of_alias_get_id(pdev
->dev
.of_node
, "gsc");
1121 if (drv_data
== &gsc_v_100_drvdata
)
1122 dev_info(dev
, "compatible 'exynos5-gsc' is deprecated\n");
1125 if (gsc
->id
>= drv_data
->num_entities
) {
1126 dev_err(dev
, "Invalid platform device id: %d\n", gsc
->id
);
1130 gsc
->num_clocks
= drv_data
->num_clocks
;
1131 gsc
->variant
= drv_data
->variant
[gsc
->id
];
1134 init_waitqueue_head(&gsc
->irq_queue
);
1135 spin_lock_init(&gsc
->slock
);
1136 mutex_init(&gsc
->lock
);
1138 gsc
->regs
= devm_platform_ioremap_resource(pdev
, 0);
1139 if (IS_ERR(gsc
->regs
))
1140 return PTR_ERR(gsc
->regs
);
1142 irq
= platform_get_irq(pdev
, 0);
1146 for (i
= 0; i
< gsc
->num_clocks
; i
++) {
1147 gsc
->clock
[i
] = devm_clk_get(dev
, drv_data
->clk_names
[i
]);
1148 if (IS_ERR(gsc
->clock
[i
])) {
1149 dev_err(dev
, "failed to get clock: %s\n",
1150 drv_data
->clk_names
[i
]);
1151 return PTR_ERR(gsc
->clock
[i
]);
1155 for (i
= 0; i
< gsc
->num_clocks
; i
++) {
1156 ret
= clk_prepare_enable(gsc
->clock
[i
]);
1158 dev_err(dev
, "clock prepare failed for clock: %s\n",
1159 drv_data
->clk_names
[i
]);
1161 clk_disable_unprepare(gsc
->clock
[i
]);
1166 ret
= devm_request_irq(dev
, irq
, gsc_irq_handler
,
1167 0, pdev
->name
, gsc
);
1169 dev_err(dev
, "failed to install irq (%d)\n", ret
);
1173 ret
= v4l2_device_register(dev
, &gsc
->v4l2_dev
);
1177 ret
= gsc_register_m2m_device(gsc
);
1181 platform_set_drvdata(pdev
, gsc
);
1183 gsc_hw_set_sw_reset(gsc
);
1184 gsc_wait_reset(gsc
);
1186 vb2_dma_contig_set_max_seg_size(dev
, DMA_BIT_MASK(32));
1188 dev_dbg(dev
, "gsc-%d registered successfully\n", gsc
->id
);
1190 pm_runtime_set_active(dev
);
1191 pm_runtime_enable(dev
);
1196 v4l2_device_unregister(&gsc
->v4l2_dev
);
1198 for (i
= gsc
->num_clocks
- 1; i
>= 0; i
--)
1199 clk_disable_unprepare(gsc
->clock
[i
]);
1203 static void gsc_remove(struct platform_device
*pdev
)
1205 struct gsc_dev
*gsc
= platform_get_drvdata(pdev
);
1208 gsc_unregister_m2m_device(gsc
);
1209 v4l2_device_unregister(&gsc
->v4l2_dev
);
1211 vb2_dma_contig_clear_max_seg_size(&pdev
->dev
);
1213 pm_runtime_disable(&pdev
->dev
);
1215 if (!pm_runtime_status_suspended(&pdev
->dev
))
1216 for (i
= 0; i
< gsc
->num_clocks
; i
++)
1217 clk_disable_unprepare(gsc
->clock
[i
]);
1219 pm_runtime_set_suspended(&pdev
->dev
);
1221 dev_dbg(&pdev
->dev
, "%s driver unloaded\n", pdev
->name
);
1225 static int gsc_m2m_suspend(struct gsc_dev
*gsc
)
1227 unsigned long flags
;
1230 spin_lock_irqsave(&gsc
->slock
, flags
);
1231 if (!gsc_m2m_pending(gsc
)) {
1232 spin_unlock_irqrestore(&gsc
->slock
, flags
);
1235 clear_bit(ST_M2M_SUSPENDED
, &gsc
->state
);
1236 set_bit(ST_M2M_SUSPENDING
, &gsc
->state
);
1237 spin_unlock_irqrestore(&gsc
->slock
, flags
);
1239 time_left
= wait_event_timeout(gsc
->irq_queue
,
1240 test_bit(ST_M2M_SUSPENDED
, &gsc
->state
),
1241 GSC_SHUTDOWN_TIMEOUT
);
1243 clear_bit(ST_M2M_SUSPENDING
, &gsc
->state
);
1244 return time_left
== 0 ? -EAGAIN
: 0;
1247 static void gsc_m2m_resume(struct gsc_dev
*gsc
)
1249 struct gsc_ctx
*ctx
;
1250 unsigned long flags
;
1252 spin_lock_irqsave(&gsc
->slock
, flags
);
1253 /* Clear for full H/W setup in first run after resume */
1255 gsc
->m2m
.ctx
= NULL
;
1256 spin_unlock_irqrestore(&gsc
->slock
, flags
);
1258 if (test_and_clear_bit(ST_M2M_SUSPENDED
, &gsc
->state
))
1259 gsc_m2m_job_finish(ctx
, VB2_BUF_STATE_ERROR
);
1262 static int gsc_runtime_resume(struct device
*dev
)
1264 struct gsc_dev
*gsc
= dev_get_drvdata(dev
);
1268 pr_debug("gsc%d: state: 0x%lx\n", gsc
->id
, gsc
->state
);
1270 for (i
= 0; i
< gsc
->num_clocks
; i
++) {
1271 ret
= clk_prepare_enable(gsc
->clock
[i
]);
1274 clk_disable_unprepare(gsc
->clock
[i
]);
1279 gsc_hw_set_sw_reset(gsc
);
1280 gsc_wait_reset(gsc
);
1281 gsc_m2m_resume(gsc
);
1286 static int gsc_runtime_suspend(struct device
*dev
)
1288 struct gsc_dev
*gsc
= dev_get_drvdata(dev
);
1292 ret
= gsc_m2m_suspend(gsc
);
1296 for (i
= gsc
->num_clocks
- 1; i
>= 0; i
--)
1297 clk_disable_unprepare(gsc
->clock
[i
]);
1299 pr_debug("gsc%d: state: 0x%lx\n", gsc
->id
, gsc
->state
);
1304 static const struct dev_pm_ops gsc_pm_ops
= {
1305 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1306 pm_runtime_force_resume
)
1307 SET_RUNTIME_PM_OPS(gsc_runtime_suspend
, gsc_runtime_resume
, NULL
)
1310 static struct platform_driver gsc_driver
= {
1312 .remove
= gsc_remove
,
1314 .name
= GSC_MODULE_NAME
,
1316 .of_match_table
= exynos_gsc_match
,
1320 module_platform_driver(gsc_driver
);
1322 MODULE_AUTHOR("Hyunwong Kim <khw0178.kim@samsung.com>");
1323 MODULE_DESCRIPTION("Samsung EXYNOS5 Soc series G-Scaler driver");
1324 MODULE_LICENSE("GPL");