1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3  * Copyright (c) 2020 Rockchip Electronics Co., Ltd.
4  * Author: Andy Yan <andy.yan@rock-chips.com>
5  */
6 #include <linux/bitfield.h>
7 #include <linux/clk.h>
8 #include <linux/component.h>
9 #include <linux/delay.h>
10 #include <linux/iopoll.h>
11 #include <linux/kernel.h>
12 #include <linux/media-bus-format.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/of_graph.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regmap.h>
21 #include <linux/swab.h>
22 
23 #include <drm/drm.h>
24 #include <drm/drm_atomic.h>
25 #include <drm/drm_atomic_uapi.h>
26 #include <drm/drm_blend.h>
27 #include <drm/drm_crtc.h>
28 #include <drm/drm_crtc_helper.h>
29 #include <drm/drm_debugfs.h>
30 #include <drm/drm_flip_work.h>
31 #include <drm/drm_framebuffer.h>
32 #include <drm/drm_plane_helper.h>
33 #include <drm/drm_probe_helper.h>
34 #include <drm/drm_vblank.h>
35 
36 #include <uapi/linux/videodev2.h>
37 #include <dt-bindings/soc/rockchip,vop2.h>
38 
39 #include "rockchip_drm_drv.h"
40 #include "rockchip_drm_gem.h"
41 #include "rockchip_drm_fb.h"
42 #include "rockchip_drm_vop2.h"
43 
44 /*
45  * VOP2 architecture
46  *
47  +----------+   +-------------+                                                        +-----------+
48  |  Cluster |   | Sel 1 from 6|                                                        | 1 from 3  |
49  |  window0 |   |    Layer0   |                                                        |    RGB    |
50  +----------+   +-------------+              +---------------+    +-------------+      +-----------+
51  +----------+   +-------------+              |N from 6 layers|    |             |
52  |  Cluster |   | Sel 1 from 6|              |   Overlay0    +--->| Video Port0 |      +-----------+
53  |  window1 |   |    Layer1   |              |               |    |             |      | 1 from 3  |
54  +----------+   +-------------+              +---------------+    +-------------+      |   LVDS    |
55  +----------+   +-------------+                                                        +-----------+
56  |  Esmart  |   | Sel 1 from 6|
57  |  window0 |   |   Layer2    |              +---------------+    +-------------+      +-----------+
58  +----------+   +-------------+              |N from 6 Layers|    |             | +--> | 1 from 3  |
59  +----------+   +-------------+   -------->  |   Overlay1    +--->| Video Port1 |      |   MIPI    |
60  |  Esmart  |   | Sel 1 from 6|   -------->  |               |    |             |      +-----------+
61  |  Window1 |   |   Layer3    |              +---------------+    +-------------+
62  +----------+   +-------------+                                                        +-----------+
63  +----------+   +-------------+                                                        | 1 from 3  |
64  |  Smart   |   | Sel 1 from 6|              +---------------+    +-------------+      |   HDMI    |
65  |  Window0 |   |    Layer4   |              |N from 6 Layers|    |             |      +-----------+
66  +----------+   +-------------+              |   Overlay2    +--->| Video Port2 |
67  +----------+   +-------------+              |               |    |             |      +-----------+
68  |  Smart   |   | Sel 1 from 6|              +---------------+    +-------------+      |  1 from 3 |
69  |  Window1 |   |    Layer5   |                                                        |    eDP    |
70  +----------+   +-------------+                                                        +-----------+
71  *
72  */
73 
74 enum vop2_data_format {
75 	VOP2_FMT_ARGB8888 = 0,
76 	VOP2_FMT_RGB888,
77 	VOP2_FMT_RGB565,
78 	VOP2_FMT_XRGB101010,
79 	VOP2_FMT_YUV420SP,
80 	VOP2_FMT_YUV422SP,
81 	VOP2_FMT_YUV444SP,
82 	VOP2_FMT_YUYV422 = 8,
83 	VOP2_FMT_YUYV420,
84 	VOP2_FMT_VYUY422,
85 	VOP2_FMT_VYUY420,
86 	VOP2_FMT_YUV420SP_TILE_8x4 = 0x10,
87 	VOP2_FMT_YUV420SP_TILE_16x2,
88 	VOP2_FMT_YUV422SP_TILE_8x4,
89 	VOP2_FMT_YUV422SP_TILE_16x2,
90 	VOP2_FMT_YUV420SP_10,
91 	VOP2_FMT_YUV422SP_10,
92 	VOP2_FMT_YUV444SP_10,
93 };
94 
95 enum vop2_afbc_format {
96 	VOP2_AFBC_FMT_RGB565,
97 	VOP2_AFBC_FMT_ARGB2101010 = 2,
98 	VOP2_AFBC_FMT_YUV420_10BIT,
99 	VOP2_AFBC_FMT_RGB888,
100 	VOP2_AFBC_FMT_ARGB8888,
101 	VOP2_AFBC_FMT_YUV420 = 9,
102 	VOP2_AFBC_FMT_YUV422 = 0xb,
103 	VOP2_AFBC_FMT_YUV422_10BIT = 0xe,
104 	VOP2_AFBC_FMT_INVALID = -1,
105 };
106 
107 union vop2_alpha_ctrl {
108 	u32 val;
109 	struct {
110 		/* [0:1] */
111 		u32 color_mode:1;
112 		u32 alpha_mode:1;
113 		/* [2:3] */
114 		u32 blend_mode:2;
115 		u32 alpha_cal_mode:1;
116 		/* [5:7] */
117 		u32 factor_mode:3;
118 		/* [8:9] */
119 		u32 alpha_en:1;
120 		u32 src_dst_swap:1;
121 		u32 reserved:6;
122 		/* [16:23] */
123 		u32 glb_alpha:8;
124 	} bits;
125 };
126 
127 struct vop2_alpha {
128 	union vop2_alpha_ctrl src_color_ctrl;
129 	union vop2_alpha_ctrl dst_color_ctrl;
130 	union vop2_alpha_ctrl src_alpha_ctrl;
131 	union vop2_alpha_ctrl dst_alpha_ctrl;
132 };
133 
134 struct vop2_alpha_config {
135 	bool src_premulti_en;
136 	bool dst_premulti_en;
137 	bool src_pixel_alpha_en;
138 	bool dst_pixel_alpha_en;
139 	u16 src_glb_alpha_value;
140 	u16 dst_glb_alpha_value;
141 };
142 
143 struct vop2_win {
144 	struct vop2 *vop2;
145 	struct drm_plane base;
146 	const struct vop2_win_data *data;
147 	struct regmap_field *reg[VOP2_WIN_MAX_REG];
148 
149 	/**
150 	 * @win_id: graphic window id, a cluster may be split into two
151 	 * graphics windows.
152 	 */
153 	u8 win_id;
154 	u8 delay;
155 	u32 offset;
156 
157 	enum drm_plane_type type;
158 };
159 
160 struct vop2_video_port {
161 	struct drm_crtc crtc;
162 	struct vop2 *vop2;
163 	struct clk *dclk;
164 	unsigned int id;
165 	const struct vop2_video_port_regs *regs;
166 	const struct vop2_video_port_data *data;
167 
168 	struct completion dsp_hold_completion;
169 
170 	/**
171 	 * @win_mask: Bitmask of windows attached to the video port;
172 	 */
173 	u32 win_mask;
174 
175 	struct vop2_win *primary_plane;
176 	struct drm_pending_vblank_event *event;
177 
178 	unsigned int nlayers;
179 };
180 
181 struct vop2 {
182 	struct device *dev;
183 	struct drm_device *drm;
184 	struct vop2_video_port vps[ROCKCHIP_MAX_CRTC];
185 
186 	const struct vop2_data *data;
187 	/*
188 	 * Number of windows that are registered as plane, may be less than the
189 	 * total number of hardware windows.
190 	 */
191 	u32 registered_num_wins;
192 
193 	void __iomem *regs;
194 	struct regmap *map;
195 
196 	struct regmap *grf;
197 
198 	/* physical map length of vop2 register */
199 	u32 len;
200 
201 	void __iomem *lut_regs;
202 
203 	/* protects crtc enable/disable */
204 	struct mutex vop2_lock;
205 
206 	int irq;
207 
208 	/*
209 	 * Some global resources are shared between all video ports(crtcs), so
210 	 * we need a ref counter here.
211 	 */
212 	unsigned int enable_count;
213 	struct clk *hclk;
214 	struct clk *aclk;
215 
216 	/* must be put at the end of the struct */
217 	struct vop2_win win[];
218 };
219 
220 static struct vop2_video_port *to_vop2_video_port(struct drm_crtc *crtc)
221 {
222 	return container_of(crtc, struct vop2_video_port, crtc);
223 }
224 
225 static struct vop2_win *to_vop2_win(struct drm_plane *p)
226 {
227 	return container_of(p, struct vop2_win, base);
228 }
229 
230 static void vop2_lock(struct vop2 *vop2)
231 {
232 	mutex_lock(&vop2->vop2_lock);
233 }
234 
235 static void vop2_unlock(struct vop2 *vop2)
236 {
237 	mutex_unlock(&vop2->vop2_lock);
238 }
239 
240 static void vop2_writel(struct vop2 *vop2, u32 offset, u32 v)
241 {
242 	regmap_write(vop2->map, offset, v);
243 }
244 
245 static void vop2_vp_write(struct vop2_video_port *vp, u32 offset, u32 v)
246 {
247 	regmap_write(vp->vop2->map, vp->data->offset + offset, v);
248 }
249 
250 static u32 vop2_readl(struct vop2 *vop2, u32 offset)
251 {
252 	u32 val;
253 
254 	regmap_read(vop2->map, offset, &val);
255 
256 	return val;
257 }
258 
259 static void vop2_win_write(const struct vop2_win *win, unsigned int reg, u32 v)
260 {
261 	regmap_field_write(win->reg[reg], v);
262 }
263 
264 static bool vop2_cluster_window(const struct vop2_win *win)
265 {
266 	return win->data->feature & WIN_FEATURE_CLUSTER;
267 }
268 
269 static void vop2_cfg_done(struct vop2_video_port *vp)
270 {
271 	struct vop2 *vop2 = vp->vop2;
272 
273 	regmap_set_bits(vop2->map, RK3568_REG_CFG_DONE,
274 			BIT(vp->id) | RK3568_REG_CFG_DONE__GLB_CFG_DONE_EN);
275 }
276 
277 static void vop2_win_disable(struct vop2_win *win)
278 {
279 	vop2_win_write(win, VOP2_WIN_ENABLE, 0);
280 
281 	if (vop2_cluster_window(win))
282 		vop2_win_write(win, VOP2_WIN_CLUSTER_ENABLE, 0);
283 }
284 
285 static enum vop2_data_format vop2_convert_format(u32 format)
286 {
287 	switch (format) {
288 	case DRM_FORMAT_XRGB8888:
289 	case DRM_FORMAT_ARGB8888:
290 	case DRM_FORMAT_XBGR8888:
291 	case DRM_FORMAT_ABGR8888:
292 		return VOP2_FMT_ARGB8888;
293 	case DRM_FORMAT_RGB888:
294 	case DRM_FORMAT_BGR888:
295 		return VOP2_FMT_RGB888;
296 	case DRM_FORMAT_RGB565:
297 	case DRM_FORMAT_BGR565:
298 		return VOP2_FMT_RGB565;
299 	case DRM_FORMAT_NV12:
300 		return VOP2_FMT_YUV420SP;
301 	case DRM_FORMAT_NV16:
302 		return VOP2_FMT_YUV422SP;
303 	case DRM_FORMAT_NV24:
304 		return VOP2_FMT_YUV444SP;
305 	case DRM_FORMAT_YUYV:
306 	case DRM_FORMAT_YVYU:
307 		return VOP2_FMT_VYUY422;
308 	case DRM_FORMAT_VYUY:
309 	case DRM_FORMAT_UYVY:
310 		return VOP2_FMT_YUYV422;
311 	default:
312 		DRM_ERROR("unsupported format[%08x]\n", format);
313 		return -EINVAL;
314 	}
315 }
316 
317 static enum vop2_afbc_format vop2_convert_afbc_format(u32 format)
318 {
319 	switch (format) {
320 	case DRM_FORMAT_XRGB8888:
321 	case DRM_FORMAT_ARGB8888:
322 	case DRM_FORMAT_XBGR8888:
323 	case DRM_FORMAT_ABGR8888:
324 		return VOP2_AFBC_FMT_ARGB8888;
325 	case DRM_FORMAT_RGB888:
326 	case DRM_FORMAT_BGR888:
327 		return VOP2_AFBC_FMT_RGB888;
328 	case DRM_FORMAT_RGB565:
329 	case DRM_FORMAT_BGR565:
330 		return VOP2_AFBC_FMT_RGB565;
331 	case DRM_FORMAT_NV12:
332 		return VOP2_AFBC_FMT_YUV420;
333 	case DRM_FORMAT_NV16:
334 		return VOP2_AFBC_FMT_YUV422;
335 	default:
336 		return VOP2_AFBC_FMT_INVALID;
337 	}
338 
339 	return VOP2_AFBC_FMT_INVALID;
340 }
341 
342 static bool vop2_win_rb_swap(u32 format)
343 {
344 	switch (format) {
345 	case DRM_FORMAT_XBGR8888:
346 	case DRM_FORMAT_ABGR8888:
347 	case DRM_FORMAT_BGR888:
348 	case DRM_FORMAT_BGR565:
349 		return true;
350 	default:
351 		return false;
352 	}
353 }
354 
355 static bool vop2_afbc_rb_swap(u32 format)
356 {
357 	switch (format) {
358 	case DRM_FORMAT_NV24:
359 		return true;
360 	default:
361 		return false;
362 	}
363 }
364 
365 static bool vop2_afbc_uv_swap(u32 format)
366 {
367 	switch (format) {
368 	case DRM_FORMAT_NV12:
369 	case DRM_FORMAT_NV16:
370 		return true;
371 	default:
372 		return false;
373 	}
374 }
375 
376 static bool vop2_win_uv_swap(u32 format)
377 {
378 	switch (format) {
379 	case DRM_FORMAT_NV12:
380 	case DRM_FORMAT_NV16:
381 	case DRM_FORMAT_NV24:
382 		return true;
383 	default:
384 		return false;
385 	}
386 }
387 
388 static bool vop2_win_dither_up(u32 format)
389 {
390 	switch (format) {
391 	case DRM_FORMAT_BGR565:
392 	case DRM_FORMAT_RGB565:
393 		return true;
394 	default:
395 		return false;
396 	}
397 }
398 
399 static bool vop2_output_uv_swap(u32 bus_format, u32 output_mode)
400 {
401 	/*
402 	 * FIXME:
403 	 *
404 	 * There is no media type for YUV444 output,
405 	 * so when out_mode is AAAA or P888, assume output is YUV444 on
406 	 * yuv format.
407 	 *
408 	 * From H/W testing, YUV444 mode need a rb swap.
409 	 */
410 	if (bus_format == MEDIA_BUS_FMT_YVYU8_1X16 ||
411 	    bus_format == MEDIA_BUS_FMT_VYUY8_1X16 ||
412 	    bus_format == MEDIA_BUS_FMT_YVYU8_2X8 ||
413 	    bus_format == MEDIA_BUS_FMT_VYUY8_2X8 ||
414 	    ((bus_format == MEDIA_BUS_FMT_YUV8_1X24 ||
415 	      bus_format == MEDIA_BUS_FMT_YUV10_1X30) &&
416 	     (output_mode == ROCKCHIP_OUT_MODE_AAAA ||
417 	      output_mode == ROCKCHIP_OUT_MODE_P888)))
418 		return true;
419 	else
420 		return false;
421 }
422 
423 static bool is_yuv_output(u32 bus_format)
424 {
425 	switch (bus_format) {
426 	case MEDIA_BUS_FMT_YUV8_1X24:
427 	case MEDIA_BUS_FMT_YUV10_1X30:
428 	case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
429 	case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
430 	case MEDIA_BUS_FMT_YUYV8_2X8:
431 	case MEDIA_BUS_FMT_YVYU8_2X8:
432 	case MEDIA_BUS_FMT_UYVY8_2X8:
433 	case MEDIA_BUS_FMT_VYUY8_2X8:
434 	case MEDIA_BUS_FMT_YUYV8_1X16:
435 	case MEDIA_BUS_FMT_YVYU8_1X16:
436 	case MEDIA_BUS_FMT_UYVY8_1X16:
437 	case MEDIA_BUS_FMT_VYUY8_1X16:
438 		return true;
439 	default:
440 		return false;
441 	}
442 }
443 
444 static bool rockchip_afbc(struct drm_plane *plane, u64 modifier)
445 {
446 	int i;
447 
448 	if (modifier == DRM_FORMAT_MOD_LINEAR)
449 		return false;
450 
451 	for (i = 0 ; i < plane->modifier_count; i++)
452 		if (plane->modifiers[i] == modifier)
453 			return true;
454 
455 	return false;
456 }
457 
458 static bool rockchip_vop2_mod_supported(struct drm_plane *plane, u32 format,
459 					u64 modifier)
460 {
461 	struct vop2_win *win = to_vop2_win(plane);
462 	struct vop2 *vop2 = win->vop2;
463 
464 	if (modifier == DRM_FORMAT_MOD_INVALID)
465 		return false;
466 
467 	if (modifier == DRM_FORMAT_MOD_LINEAR)
468 		return true;
469 
470 	if (!rockchip_afbc(plane, modifier)) {
471 		drm_err(vop2->drm, "Unsupported format modifier 0x%llx\n",
472 			modifier);
473 
474 		return false;
475 	}
476 
477 	return vop2_convert_afbc_format(format) >= 0;
478 }
479 
480 static u32 vop2_afbc_transform_offset(struct drm_plane_state *pstate,
481 				      bool afbc_half_block_en)
482 {
483 	struct drm_rect *src = &pstate->src;
484 	struct drm_framebuffer *fb = pstate->fb;
485 	u32 bpp = fb->format->cpp[0] * 8;
486 	u32 vir_width = (fb->pitches[0] << 3) / bpp;
487 	u32 width = drm_rect_width(src) >> 16;
488 	u32 height = drm_rect_height(src) >> 16;
489 	u32 act_xoffset = src->x1 >> 16;
490 	u32 act_yoffset = src->y1 >> 16;
491 	u32 align16_crop = 0;
492 	u32 align64_crop = 0;
493 	u32 height_tmp;
494 	u8 tx, ty;
495 	u8 bottom_crop_line_num = 0;
496 
497 	/* 16 pixel align */
498 	if (height & 0xf)
499 		align16_crop = 16 - (height & 0xf);
500 
501 	height_tmp = height + align16_crop;
502 
503 	/* 64 pixel align */
504 	if (height_tmp & 0x3f)
505 		align64_crop = 64 - (height_tmp & 0x3f);
506 
507 	bottom_crop_line_num = align16_crop + align64_crop;
508 
509 	switch (pstate->rotation &
510 		(DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y |
511 		 DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_270)) {
512 	case DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y:
513 		tx = 16 - ((act_xoffset + width) & 0xf);
514 		ty = bottom_crop_line_num - act_yoffset;
515 		break;
516 	case DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_90:
517 		tx = bottom_crop_line_num - act_yoffset;
518 		ty = vir_width - width - act_xoffset;
519 		break;
520 	case DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_270:
521 		tx = act_yoffset;
522 		ty = act_xoffset;
523 		break;
524 	case DRM_MODE_REFLECT_X:
525 		tx = 16 - ((act_xoffset + width) & 0xf);
526 		ty = act_yoffset;
527 		break;
528 	case DRM_MODE_REFLECT_Y:
529 		tx = act_xoffset;
530 		ty = bottom_crop_line_num - act_yoffset;
531 		break;
532 	case DRM_MODE_ROTATE_90:
533 		tx = bottom_crop_line_num - act_yoffset;
534 		ty = act_xoffset;
535 		break;
536 	case DRM_MODE_ROTATE_270:
537 		tx = act_yoffset;
538 		ty = vir_width - width - act_xoffset;
539 		break;
540 	case 0:
541 		tx = act_xoffset;
542 		ty = act_yoffset;
543 		break;
544 	}
545 
546 	if (afbc_half_block_en)
547 		ty &= 0x7f;
548 
549 #define TRANSFORM_XOFFSET GENMASK(7, 0)
550 #define TRANSFORM_YOFFSET GENMASK(23, 16)
551 	return FIELD_PREP(TRANSFORM_XOFFSET, tx) |
552 		FIELD_PREP(TRANSFORM_YOFFSET, ty);
553 }
554 
555 /*
556  * A Cluster window has 2048 x 16 line buffer, which can
557  * works at 2048 x 16(Full) or 4096 x 8 (Half) mode.
558  * for Cluster_lb_mode register:
559  * 0: half mode, for plane input width range 2048 ~ 4096
560  * 1: half mode, for cluster work at 2 * 2048 plane mode
561  * 2: half mode, for rotate_90/270 mode
562  *
563  */
564 static int vop2_get_cluster_lb_mode(struct vop2_win *win,
565 				    struct drm_plane_state *pstate)
566 {
567 	if ((pstate->rotation & DRM_MODE_ROTATE_270) ||
568 	    (pstate->rotation & DRM_MODE_ROTATE_90))
569 		return 2;
570 	else
571 		return 0;
572 }
573 
574 static u16 vop2_scale_factor(u32 src, u32 dst)
575 {
576 	u32 fac;
577 	int shift;
578 
579 	if (src == dst)
580 		return 0;
581 
582 	if (dst < 2)
583 		return U16_MAX;
584 
585 	if (src < 2)
586 		return 0;
587 
588 	if (src > dst)
589 		shift = 12;
590 	else
591 		shift = 16;
592 
593 	src--;
594 	dst--;
595 
596 	fac = DIV_ROUND_UP(src << shift, dst) - 1;
597 
598 	if (fac > U16_MAX)
599 		return U16_MAX;
600 
601 	return fac;
602 }
603 
604 static void vop2_setup_scale(struct vop2 *vop2, const struct vop2_win *win,
605 			     u32 src_w, u32 src_h, u32 dst_w,
606 			     u32 dst_h, u32 pixel_format)
607 {
608 	const struct drm_format_info *info;
609 	u16 hor_scl_mode, ver_scl_mode;
610 	u16 hscl_filter_mode, vscl_filter_mode;
611 	u8 gt2 = 0;
612 	u8 gt4 = 0;
613 	u32 val;
614 
615 	info = drm_format_info(pixel_format);
616 
617 	if (src_h >= (4 * dst_h)) {
618 		gt4 = 1;
619 		src_h >>= 2;
620 	} else if (src_h >= (2 * dst_h)) {
621 		gt2 = 1;
622 		src_h >>= 1;
623 	}
624 
625 	hor_scl_mode = scl_get_scl_mode(src_w, dst_w);
626 	ver_scl_mode = scl_get_scl_mode(src_h, dst_h);
627 
628 	if (hor_scl_mode == SCALE_UP)
629 		hscl_filter_mode = VOP2_SCALE_UP_BIC;
630 	else
631 		hscl_filter_mode = VOP2_SCALE_DOWN_BIL;
632 
633 	if (ver_scl_mode == SCALE_UP)
634 		vscl_filter_mode = VOP2_SCALE_UP_BIL;
635 	else
636 		vscl_filter_mode = VOP2_SCALE_DOWN_BIL;
637 
638 	/*
639 	 * RK3568 VOP Esmart/Smart dsp_w should be even pixel
640 	 * at scale down mode
641 	 */
642 	if (!(win->data->feature & WIN_FEATURE_AFBDC)) {
643 		if ((hor_scl_mode == SCALE_DOWN) && (dst_w & 0x1)) {
644 			drm_dbg(vop2->drm, "%s dst_w[%d] should align as 2 pixel\n",
645 				win->data->name, dst_w);
646 			dst_w++;
647 		}
648 	}
649 
650 	val = vop2_scale_factor(src_w, dst_w);
651 	vop2_win_write(win, VOP2_WIN_SCALE_YRGB_X, val);
652 	val = vop2_scale_factor(src_h, dst_h);
653 	vop2_win_write(win, VOP2_WIN_SCALE_YRGB_Y, val);
654 
655 	vop2_win_write(win, VOP2_WIN_VSD_YRGB_GT4, gt4);
656 	vop2_win_write(win, VOP2_WIN_VSD_YRGB_GT2, gt2);
657 
658 	vop2_win_write(win, VOP2_WIN_YRGB_HOR_SCL_MODE, hor_scl_mode);
659 	vop2_win_write(win, VOP2_WIN_YRGB_VER_SCL_MODE, ver_scl_mode);
660 
661 	if (vop2_cluster_window(win))
662 		return;
663 
664 	vop2_win_write(win, VOP2_WIN_YRGB_HSCL_FILTER_MODE, hscl_filter_mode);
665 	vop2_win_write(win, VOP2_WIN_YRGB_VSCL_FILTER_MODE, vscl_filter_mode);
666 
667 	if (info->is_yuv) {
668 		src_w /= info->hsub;
669 		src_h /= info->vsub;
670 
671 		gt4 = 0;
672 		gt2 = 0;
673 
674 		if (src_h >= (4 * dst_h)) {
675 			gt4 = 1;
676 			src_h >>= 2;
677 		} else if (src_h >= (2 * dst_h)) {
678 			gt2 = 1;
679 			src_h >>= 1;
680 		}
681 
682 		hor_scl_mode = scl_get_scl_mode(src_w, dst_w);
683 		ver_scl_mode = scl_get_scl_mode(src_h, dst_h);
684 
685 		val = vop2_scale_factor(src_w, dst_w);
686 		vop2_win_write(win, VOP2_WIN_SCALE_CBCR_X, val);
687 
688 		val = vop2_scale_factor(src_h, dst_h);
689 		vop2_win_write(win, VOP2_WIN_SCALE_CBCR_Y, val);
690 
691 		vop2_win_write(win, VOP2_WIN_VSD_CBCR_GT4, gt4);
692 		vop2_win_write(win, VOP2_WIN_VSD_CBCR_GT2, gt2);
693 		vop2_win_write(win, VOP2_WIN_CBCR_HOR_SCL_MODE, hor_scl_mode);
694 		vop2_win_write(win, VOP2_WIN_CBCR_VER_SCL_MODE, ver_scl_mode);
695 		vop2_win_write(win, VOP2_WIN_CBCR_HSCL_FILTER_MODE, hscl_filter_mode);
696 		vop2_win_write(win, VOP2_WIN_CBCR_VSCL_FILTER_MODE, vscl_filter_mode);
697 	}
698 }
699 
700 static int vop2_convert_csc_mode(int csc_mode)
701 {
702 	switch (csc_mode) {
703 	case V4L2_COLORSPACE_SMPTE170M:
704 	case V4L2_COLORSPACE_470_SYSTEM_M:
705 	case V4L2_COLORSPACE_470_SYSTEM_BG:
706 		return CSC_BT601L;
707 	case V4L2_COLORSPACE_REC709:
708 	case V4L2_COLORSPACE_SMPTE240M:
709 	case V4L2_COLORSPACE_DEFAULT:
710 		return CSC_BT709L;
711 	case V4L2_COLORSPACE_JPEG:
712 		return CSC_BT601F;
713 	case V4L2_COLORSPACE_BT2020:
714 		return CSC_BT2020;
715 	default:
716 		return CSC_BT709L;
717 	}
718 }
719 
720 /*
721  * colorspace path:
722  *      Input        Win csc                     Output
723  * 1. YUV(2020)  --> Y2R->2020To709->R2Y   --> YUV_OUTPUT(601/709)
724  *    RGB        --> R2Y                  __/
725  *
726  * 2. YUV(2020)  --> bypasss               --> YUV_OUTPUT(2020)
727  *    RGB        --> 709To2020->R2Y       __/
728  *
729  * 3. YUV(2020)  --> Y2R->2020To709        --> RGB_OUTPUT(709)
730  *    RGB        --> R2Y                  __/
731  *
732  * 4. YUV(601/709)-> Y2R->709To2020->R2Y   --> YUV_OUTPUT(2020)
733  *    RGB        --> 709To2020->R2Y       __/
734  *
735  * 5. YUV(601/709)-> bypass                --> YUV_OUTPUT(709)
736  *    RGB        --> R2Y                  __/
737  *
738  * 6. YUV(601/709)-> bypass                --> YUV_OUTPUT(601)
739  *    RGB        --> R2Y(601)             __/
740  *
741  * 7. YUV        --> Y2R(709)              --> RGB_OUTPUT(709)
742  *    RGB        --> bypass               __/
743  *
744  * 8. RGB        --> 709To2020->R2Y        --> YUV_OUTPUT(2020)
745  *
746  * 9. RGB        --> R2Y(709)              --> YUV_OUTPUT(709)
747  *
748  * 10. RGB       --> R2Y(601)              --> YUV_OUTPUT(601)
749  *
750  * 11. RGB       --> bypass                --> RGB_OUTPUT(709)
751  */
752 
753 static void vop2_setup_csc_mode(struct vop2_video_port *vp,
754 				struct vop2_win *win,
755 				struct drm_plane_state *pstate)
756 {
757 	struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(vp->crtc.state);
758 	int is_input_yuv = pstate->fb->format->is_yuv;
759 	int is_output_yuv = is_yuv_output(vcstate->bus_format);
760 	int input_csc = V4L2_COLORSPACE_DEFAULT;
761 	int output_csc = vcstate->color_space;
762 	bool r2y_en, y2r_en;
763 	int csc_mode;
764 
765 	if (is_input_yuv && !is_output_yuv) {
766 		y2r_en = true;
767 		r2y_en = false;
768 		csc_mode = vop2_convert_csc_mode(input_csc);
769 	} else if (!is_input_yuv && is_output_yuv) {
770 		y2r_en = false;
771 		r2y_en = true;
772 		csc_mode = vop2_convert_csc_mode(output_csc);
773 	} else {
774 		y2r_en = false;
775 		r2y_en = false;
776 		csc_mode = false;
777 	}
778 
779 	vop2_win_write(win, VOP2_WIN_Y2R_EN, y2r_en);
780 	vop2_win_write(win, VOP2_WIN_R2Y_EN, r2y_en);
781 	vop2_win_write(win, VOP2_WIN_CSC_MODE, csc_mode);
782 }
783 
784 static void vop2_crtc_enable_irq(struct vop2_video_port *vp, u32 irq)
785 {
786 	struct vop2 *vop2 = vp->vop2;
787 
788 	vop2_writel(vop2, RK3568_VP_INT_CLR(vp->id), irq << 16 | irq);
789 	vop2_writel(vop2, RK3568_VP_INT_EN(vp->id), irq << 16 | irq);
790 }
791 
792 static void vop2_crtc_disable_irq(struct vop2_video_port *vp, u32 irq)
793 {
794 	struct vop2 *vop2 = vp->vop2;
795 
796 	vop2_writel(vop2, RK3568_VP_INT_EN(vp->id), irq << 16);
797 }
798 
799 static int vop2_core_clks_prepare_enable(struct vop2 *vop2)
800 {
801 	int ret;
802 
803 	ret = clk_prepare_enable(vop2->hclk);
804 	if (ret < 0) {
805 		drm_err(vop2->drm, "failed to enable hclk - %d\n", ret);
806 		return ret;
807 	}
808 
809 	ret = clk_prepare_enable(vop2->aclk);
810 	if (ret < 0) {
811 		drm_err(vop2->drm, "failed to enable aclk - %d\n", ret);
812 		goto err;
813 	}
814 
815 	return 0;
816 err:
817 	clk_disable_unprepare(vop2->hclk);
818 
819 	return ret;
820 }
821 
822 static void vop2_enable(struct vop2 *vop2)
823 {
824 	int ret;
825 
826 	ret = pm_runtime_get_sync(vop2->dev);
827 	if (ret < 0) {
828 		drm_err(vop2->drm, "failed to get pm runtime: %d\n", ret);
829 		return;
830 	}
831 
832 	ret = vop2_core_clks_prepare_enable(vop2);
833 	if (ret) {
834 		pm_runtime_put_sync(vop2->dev);
835 		return;
836 	}
837 
838 	ret = rockchip_drm_dma_attach_device(vop2->drm, vop2->dev);
839 	if (ret) {
840 		drm_err(vop2->drm, "failed to attach dma mapping, %d\n", ret);
841 		return;
842 	}
843 
844 	if (vop2->data->soc_id == 3566)
845 		vop2_writel(vop2, RK3568_OTP_WIN_EN, 1);
846 
847 	vop2_writel(vop2, RK3568_REG_CFG_DONE, RK3568_REG_CFG_DONE__GLB_CFG_DONE_EN);
848 
849 	/*
850 	 * Disable auto gating, this is a workaround to
851 	 * avoid display image shift when a window enabled.
852 	 */
853 	regmap_clear_bits(vop2->map, RK3568_SYS_AUTO_GATING_CTRL,
854 			  RK3568_SYS_AUTO_GATING_CTRL__AUTO_GATING_EN);
855 
856 	vop2_writel(vop2, RK3568_SYS0_INT_CLR,
857 		    VOP2_INT_BUS_ERRPR << 16 | VOP2_INT_BUS_ERRPR);
858 	vop2_writel(vop2, RK3568_SYS0_INT_EN,
859 		    VOP2_INT_BUS_ERRPR << 16 | VOP2_INT_BUS_ERRPR);
860 	vop2_writel(vop2, RK3568_SYS1_INT_CLR,
861 		    VOP2_INT_BUS_ERRPR << 16 | VOP2_INT_BUS_ERRPR);
862 	vop2_writel(vop2, RK3568_SYS1_INT_EN,
863 		    VOP2_INT_BUS_ERRPR << 16 | VOP2_INT_BUS_ERRPR);
864 }
865 
866 static void vop2_disable(struct vop2 *vop2)
867 {
868 	rockchip_drm_dma_detach_device(vop2->drm, vop2->dev);
869 
870 	pm_runtime_put_sync(vop2->dev);
871 
872 	clk_disable_unprepare(vop2->aclk);
873 	clk_disable_unprepare(vop2->hclk);
874 }
875 
876 static void vop2_crtc_atomic_disable(struct drm_crtc *crtc,
877 				     struct drm_atomic_state *state)
878 {
879 	struct vop2_video_port *vp = to_vop2_video_port(crtc);
880 	struct vop2 *vop2 = vp->vop2;
881 	int ret;
882 
883 	vop2_lock(vop2);
884 
885 	drm_crtc_vblank_off(crtc);
886 
887 	/*
888 	 * Vop standby will take effect at end of current frame,
889 	 * if dsp hold valid irq happen, it means standby complete.
890 	 *
891 	 * we must wait standby complete when we want to disable aclk,
892 	 * if not, memory bus maybe dead.
893 	 */
894 	reinit_completion(&vp->dsp_hold_completion);
895 
896 	vop2_crtc_enable_irq(vp, VP_INT_DSP_HOLD_VALID);
897 
898 	vop2_vp_write(vp, RK3568_VP_DSP_CTRL, RK3568_VP_DSP_CTRL__STANDBY);
899 
900 	ret = wait_for_completion_timeout(&vp->dsp_hold_completion,
901 					  msecs_to_jiffies(50));
902 	if (!ret)
903 		drm_info(vop2->drm, "wait for vp%d dsp_hold timeout\n", vp->id);
904 
905 	vop2_crtc_disable_irq(vp, VP_INT_DSP_HOLD_VALID);
906 
907 	clk_disable_unprepare(vp->dclk);
908 
909 	vop2->enable_count--;
910 
911 	if (!vop2->enable_count)
912 		vop2_disable(vop2);
913 
914 	vop2_unlock(vop2);
915 
916 	if (crtc->state->event && !crtc->state->active) {
917 		spin_lock_irq(&crtc->dev->event_lock);
918 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
919 		spin_unlock_irq(&crtc->dev->event_lock);
920 
921 		crtc->state->event = NULL;
922 	}
923 }
924 
925 static int vop2_plane_atomic_check(struct drm_plane *plane,
926 				   struct drm_atomic_state *astate)
927 {
928 	struct drm_plane_state *pstate = drm_atomic_get_new_plane_state(astate, plane);
929 	struct drm_framebuffer *fb = pstate->fb;
930 	struct drm_crtc *crtc = pstate->crtc;
931 	struct drm_crtc_state *cstate;
932 	struct vop2_video_port *vp;
933 	struct vop2 *vop2;
934 	const struct vop2_data *vop2_data;
935 	struct drm_rect *dest = &pstate->dst;
936 	struct drm_rect *src = &pstate->src;
937 	int min_scale = FRAC_16_16(1, 8);
938 	int max_scale = FRAC_16_16(8, 1);
939 	int format;
940 	int ret;
941 
942 	if (!crtc)
943 		return 0;
944 
945 	vp = to_vop2_video_port(crtc);
946 	vop2 = vp->vop2;
947 	vop2_data = vop2->data;
948 
949 	cstate = drm_atomic_get_existing_crtc_state(pstate->state, crtc);
950 	if (WARN_ON(!cstate))
951 		return -EINVAL;
952 
953 	ret = drm_atomic_helper_check_plane_state(pstate, cstate,
954 						  min_scale, max_scale,
955 						  true, true);
956 	if (ret)
957 		return ret;
958 
959 	if (!pstate->visible)
960 		return 0;
961 
962 	format = vop2_convert_format(fb->format->format);
963 	if (format < 0)
964 		return format;
965 
966 	if (drm_rect_width(src) >> 16 < 4 || drm_rect_height(src) >> 16 < 4 ||
967 	    drm_rect_width(dest) < 4 || drm_rect_width(dest) < 4) {
968 		drm_err(vop2->drm, "Invalid size: %dx%d->%dx%d, min size is 4x4\n",
969 			drm_rect_width(src) >> 16, drm_rect_height(src) >> 16,
970 			drm_rect_width(dest), drm_rect_height(dest));
971 		pstate->visible = false;
972 		return 0;
973 	}
974 
975 	if (drm_rect_width(src) >> 16 > vop2_data->max_input.width ||
976 	    drm_rect_height(src) >> 16 > vop2_data->max_input.height) {
977 		drm_err(vop2->drm, "Invalid source: %dx%d. max input: %dx%d\n",
978 			drm_rect_width(src) >> 16,
979 			drm_rect_height(src) >> 16,
980 			vop2_data->max_input.width,
981 			vop2_data->max_input.height);
982 		return -EINVAL;
983 	}
984 
985 	/*
986 	 * Src.x1 can be odd when do clip, but yuv plane start point
987 	 * need align with 2 pixel.
988 	 */
989 	if (fb->format->is_yuv && ((pstate->src.x1 >> 16) % 2)) {
990 		drm_err(vop2->drm, "Invalid Source: Yuv format not support odd xpos\n");
991 		return -EINVAL;
992 	}
993 
994 	return 0;
995 }
996 
997 static void vop2_plane_atomic_disable(struct drm_plane *plane,
998 				      struct drm_atomic_state *state)
999 {
1000 	struct drm_plane_state *old_pstate = drm_atomic_get_old_plane_state(state, plane);
1001 	struct vop2_win *win = to_vop2_win(plane);
1002 	struct vop2 *vop2 = win->vop2;
1003 
1004 	drm_dbg(vop2->drm, "%s disable\n", win->data->name);
1005 
1006 	if (!old_pstate->crtc)
1007 		return;
1008 
1009 	vop2_win_disable(win);
1010 	vop2_win_write(win, VOP2_WIN_YUV_CLIP, 0);
1011 }
1012 
1013 /*
1014  * The color key is 10 bit, so all format should
1015  * convert to 10 bit here.
1016  */
1017 static void vop2_plane_setup_color_key(struct drm_plane *plane, u32 color_key)
1018 {
1019 	struct drm_plane_state *pstate = plane->state;
1020 	struct drm_framebuffer *fb = pstate->fb;
1021 	struct vop2_win *win = to_vop2_win(plane);
1022 	u32 color_key_en = 0;
1023 	u32 r = 0;
1024 	u32 g = 0;
1025 	u32 b = 0;
1026 
1027 	if (!(color_key & VOP2_COLOR_KEY_MASK) || fb->format->is_yuv) {
1028 		vop2_win_write(win, VOP2_WIN_COLOR_KEY_EN, 0);
1029 		return;
1030 	}
1031 
1032 	switch (fb->format->format) {
1033 	case DRM_FORMAT_RGB565:
1034 	case DRM_FORMAT_BGR565:
1035 		r = (color_key & 0xf800) >> 11;
1036 		g = (color_key & 0x7e0) >> 5;
1037 		b = (color_key & 0x1f);
1038 		r <<= 5;
1039 		g <<= 4;
1040 		b <<= 5;
1041 		color_key_en = 1;
1042 		break;
1043 	case DRM_FORMAT_XRGB8888:
1044 	case DRM_FORMAT_ARGB8888:
1045 	case DRM_FORMAT_XBGR8888:
1046 	case DRM_FORMAT_ABGR8888:
1047 	case DRM_FORMAT_RGB888:
1048 	case DRM_FORMAT_BGR888:
1049 		r = (color_key & 0xff0000) >> 16;
1050 		g = (color_key & 0xff00) >> 8;
1051 		b = (color_key & 0xff);
1052 		r <<= 2;
1053 		g <<= 2;
1054 		b <<= 2;
1055 		color_key_en = 1;
1056 		break;
1057 	}
1058 
1059 	vop2_win_write(win, VOP2_WIN_COLOR_KEY_EN, color_key_en);
1060 	vop2_win_write(win, VOP2_WIN_COLOR_KEY, (r << 20) | (g << 10) | b);
1061 }
1062 
1063 static void vop2_plane_atomic_update(struct drm_plane *plane,
1064 				     struct drm_atomic_state *state)
1065 {
1066 	struct drm_plane_state *pstate = plane->state;
1067 	struct drm_crtc *crtc = pstate->crtc;
1068 	struct vop2_win *win = to_vop2_win(plane);
1069 	struct vop2_video_port *vp = to_vop2_video_port(crtc);
1070 	struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
1071 	struct vop2 *vop2 = win->vop2;
1072 	struct drm_framebuffer *fb = pstate->fb;
1073 	u32 bpp = fb->format->cpp[0] * 8;
1074 	u32 actual_w, actual_h, dsp_w, dsp_h;
1075 	u32 act_info, dsp_info;
1076 	u32 format;
1077 	u32 afbc_format;
1078 	u32 rb_swap;
1079 	u32 uv_swap;
1080 	struct drm_rect *src = &pstate->src;
1081 	struct drm_rect *dest = &pstate->dst;
1082 	u32 afbc_tile_num;
1083 	u32 transform_offset;
1084 	bool dither_up;
1085 	bool xmirror = pstate->rotation & DRM_MODE_REFLECT_X ? true : false;
1086 	bool ymirror = pstate->rotation & DRM_MODE_REFLECT_Y ? true : false;
1087 	bool rotate_270 = pstate->rotation & DRM_MODE_ROTATE_270;
1088 	bool rotate_90 = pstate->rotation & DRM_MODE_ROTATE_90;
1089 	struct rockchip_gem_object *rk_obj;
1090 	unsigned long offset;
1091 	bool afbc_en;
1092 	dma_addr_t yrgb_mst;
1093 	dma_addr_t uv_mst;
1094 
1095 	/*
1096 	 * can't update plane when vop2 is disabled.
1097 	 */
1098 	if (WARN_ON(!crtc))
1099 		return;
1100 
1101 	if (!pstate->visible) {
1102 		vop2_plane_atomic_disable(plane, state);
1103 		return;
1104 	}
1105 
1106 	afbc_en = rockchip_afbc(plane, fb->modifier);
1107 
1108 	offset = (src->x1 >> 16) * fb->format->cpp[0];
1109 
1110 	/*
1111 	 * AFBC HDR_PTR must set to the zero offset of the framebuffer.
1112 	 */
1113 	if (afbc_en)
1114 		offset = 0;
1115 	else if (pstate->rotation & DRM_MODE_REFLECT_Y)
1116 		offset += ((src->y2 >> 16) - 1) * fb->pitches[0];
1117 	else
1118 		offset += (src->y1 >> 16) * fb->pitches[0];
1119 
1120 	rk_obj = to_rockchip_obj(fb->obj[0]);
1121 
1122 	yrgb_mst = rk_obj->dma_addr + offset + fb->offsets[0];
1123 	if (fb->format->is_yuv) {
1124 		int hsub = fb->format->hsub;
1125 		int vsub = fb->format->vsub;
1126 
1127 		offset = (src->x1 >> 16) * fb->format->cpp[1] / hsub;
1128 		offset += (src->y1 >> 16) * fb->pitches[1] / vsub;
1129 
1130 		if ((pstate->rotation & DRM_MODE_REFLECT_Y) && !afbc_en)
1131 			offset += fb->pitches[1] * ((pstate->src_h >> 16) - 2) / vsub;
1132 
1133 		rk_obj = to_rockchip_obj(fb->obj[0]);
1134 		uv_mst = rk_obj->dma_addr + offset + fb->offsets[1];
1135 	}
1136 
1137 	actual_w = drm_rect_width(src) >> 16;
1138 	actual_h = drm_rect_height(src) >> 16;
1139 	dsp_w = drm_rect_width(dest);
1140 
1141 	if (dest->x1 + dsp_w > adjusted_mode->hdisplay) {
1142 		drm_err(vop2->drm, "vp%d %s dest->x1[%d] + dsp_w[%d] exceed mode hdisplay[%d]\n",
1143 			vp->id, win->data->name, dest->x1, dsp_w, adjusted_mode->hdisplay);
1144 		dsp_w = adjusted_mode->hdisplay - dest->x1;
1145 		if (dsp_w < 4)
1146 			dsp_w = 4;
1147 		actual_w = dsp_w * actual_w / drm_rect_width(dest);
1148 	}
1149 
1150 	dsp_h = drm_rect_height(dest);
1151 
1152 	if (dest->y1 + dsp_h > adjusted_mode->vdisplay) {
1153 		drm_err(vop2->drm, "vp%d %s dest->y1[%d] + dsp_h[%d] exceed mode vdisplay[%d]\n",
1154 			vp->id, win->data->name, dest->y1, dsp_h, adjusted_mode->vdisplay);
1155 		dsp_h = adjusted_mode->vdisplay - dest->y1;
1156 		if (dsp_h < 4)
1157 			dsp_h = 4;
1158 		actual_h = dsp_h * actual_h / drm_rect_height(dest);
1159 	}
1160 
1161 	/*
1162 	 * This is workaround solution for IC design:
1163 	 * esmart can't support scale down when actual_w % 16 == 1.
1164 	 */
1165 	if (!(win->data->feature & WIN_FEATURE_AFBDC)) {
1166 		if (actual_w > dsp_w && (actual_w & 0xf) == 1) {
1167 			drm_err(vop2->drm, "vp%d %s act_w[%d] MODE 16 == 1\n",
1168 				vp->id, win->data->name, actual_w);
1169 			actual_w -= 1;
1170 		}
1171 	}
1172 
1173 	if (afbc_en && actual_w % 4) {
1174 		drm_err(vop2->drm, "vp%d %s actual_w[%d] not 4 pixel aligned\n",
1175 			vp->id, win->data->name, actual_w);
1176 		actual_w = ALIGN_DOWN(actual_w, 4);
1177 	}
1178 
1179 	act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff);
1180 	dsp_info = (dsp_h - 1) << 16 | ((dsp_w - 1) & 0xffff);
1181 
1182 	format = vop2_convert_format(fb->format->format);
1183 
1184 	drm_dbg(vop2->drm, "vp%d update %s[%dx%d->%dx%d@%dx%d] fmt[%p4cc_%s] addr[%pad]\n",
1185 		vp->id, win->data->name, actual_w, actual_h, dsp_w, dsp_h,
1186 		dest->x1, dest->y1,
1187 		&fb->format->format,
1188 		afbc_en ? "AFBC" : "", &yrgb_mst);
1189 
1190 	if (afbc_en) {
1191 		u32 stride;
1192 
1193 		/* the afbc superblock is 16 x 16 */
1194 		afbc_format = vop2_convert_afbc_format(fb->format->format);
1195 
1196 		/* Enable color transform for YTR */
1197 		if (fb->modifier & AFBC_FORMAT_MOD_YTR)
1198 			afbc_format |= (1 << 4);
1199 
1200 		afbc_tile_num = ALIGN(actual_w, 16) >> 4;
1201 
1202 		/*
1203 		 * AFBC pic_vir_width is count by pixel, this is different
1204 		 * with WIN_VIR_STRIDE.
1205 		 */
1206 		stride = (fb->pitches[0] << 3) / bpp;
1207 		if ((stride & 0x3f) && (xmirror || rotate_90 || rotate_270))
1208 			drm_err(vop2->drm, "vp%d %s stride[%d] not 64 pixel aligned\n",
1209 				vp->id, win->data->name, stride);
1210 
1211 		rb_swap = vop2_afbc_rb_swap(fb->format->format);
1212 		uv_swap = vop2_afbc_uv_swap(fb->format->format);
1213 		/*
1214 		 * This is a workaround for crazy IC design, Cluster
1215 		 * and Esmart/Smart use different format configuration map:
1216 		 * YUV420_10BIT: 0x10 for Cluster, 0x14 for Esmart/Smart.
1217 		 *
1218 		 * This is one thing we can make the convert simple:
1219 		 * AFBCD decode all the YUV data to YUV444. So we just
1220 		 * set all the yuv 10 bit to YUV444_10.
1221 		 */
1222 		if (fb->format->is_yuv && bpp == 10)
1223 			format = VOP2_CLUSTER_YUV444_10;
1224 
1225 		if (vop2_cluster_window(win))
1226 			vop2_win_write(win, VOP2_WIN_AFBC_ENABLE, 1);
1227 		vop2_win_write(win, VOP2_WIN_AFBC_FORMAT, afbc_format);
1228 		vop2_win_write(win, VOP2_WIN_AFBC_RB_SWAP, rb_swap);
1229 		vop2_win_write(win, VOP2_WIN_AFBC_UV_SWAP, uv_swap);
1230 		vop2_win_write(win, VOP2_WIN_AFBC_AUTO_GATING_EN, 0);
1231 		vop2_win_write(win, VOP2_WIN_AFBC_BLOCK_SPLIT_EN, 0);
1232 		if (pstate->rotation & (DRM_MODE_ROTATE_270 | DRM_MODE_ROTATE_90)) {
1233 			vop2_win_write(win, VOP2_WIN_AFBC_HALF_BLOCK_EN, 0);
1234 			transform_offset = vop2_afbc_transform_offset(pstate, false);
1235 		} else {
1236 			vop2_win_write(win, VOP2_WIN_AFBC_HALF_BLOCK_EN, 1);
1237 			transform_offset = vop2_afbc_transform_offset(pstate, true);
1238 		}
1239 		vop2_win_write(win, VOP2_WIN_AFBC_HDR_PTR, yrgb_mst);
1240 		vop2_win_write(win, VOP2_WIN_AFBC_PIC_SIZE, act_info);
1241 		vop2_win_write(win, VOP2_WIN_AFBC_TRANSFORM_OFFSET, transform_offset);
1242 		vop2_win_write(win, VOP2_WIN_AFBC_PIC_OFFSET, ((src->x1 >> 16) | src->y1));
1243 		vop2_win_write(win, VOP2_WIN_AFBC_DSP_OFFSET, (dest->x1 | (dest->y1 << 16)));
1244 		vop2_win_write(win, VOP2_WIN_AFBC_PIC_VIR_WIDTH, stride);
1245 		vop2_win_write(win, VOP2_WIN_AFBC_TILE_NUM, afbc_tile_num);
1246 		vop2_win_write(win, VOP2_WIN_XMIRROR, xmirror);
1247 		vop2_win_write(win, VOP2_WIN_AFBC_ROTATE_270, rotate_270);
1248 		vop2_win_write(win, VOP2_WIN_AFBC_ROTATE_90, rotate_90);
1249 	} else {
1250 		vop2_win_write(win, VOP2_WIN_YRGB_VIR, DIV_ROUND_UP(fb->pitches[0], 4));
1251 	}
1252 
1253 	vop2_win_write(win, VOP2_WIN_YMIRROR, ymirror);
1254 
1255 	if (rotate_90 || rotate_270) {
1256 		act_info = swahw32(act_info);
1257 		actual_w = drm_rect_height(src) >> 16;
1258 		actual_h = drm_rect_width(src) >> 16;
1259 	}
1260 
1261 	vop2_win_write(win, VOP2_WIN_FORMAT, format);
1262 	vop2_win_write(win, VOP2_WIN_YRGB_MST, yrgb_mst);
1263 
1264 	rb_swap = vop2_win_rb_swap(fb->format->format);
1265 	vop2_win_write(win, VOP2_WIN_RB_SWAP, rb_swap);
1266 	if (!vop2_cluster_window(win)) {
1267 		uv_swap = vop2_win_uv_swap(fb->format->format);
1268 		vop2_win_write(win, VOP2_WIN_UV_SWAP, uv_swap);
1269 	}
1270 
1271 	if (fb->format->is_yuv) {
1272 		vop2_win_write(win, VOP2_WIN_UV_VIR, DIV_ROUND_UP(fb->pitches[1], 4));
1273 		vop2_win_write(win, VOP2_WIN_UV_MST, uv_mst);
1274 	}
1275 
1276 	vop2_setup_scale(vop2, win, actual_w, actual_h, dsp_w, dsp_h, fb->format->format);
1277 	if (!vop2_cluster_window(win))
1278 		vop2_plane_setup_color_key(plane, 0);
1279 	vop2_win_write(win, VOP2_WIN_ACT_INFO, act_info);
1280 	vop2_win_write(win, VOP2_WIN_DSP_INFO, dsp_info);
1281 	vop2_win_write(win, VOP2_WIN_DSP_ST, dest->y1 << 16 | (dest->x1 & 0xffff));
1282 
1283 	vop2_setup_csc_mode(vp, win, pstate);
1284 
1285 	dither_up = vop2_win_dither_up(fb->format->format);
1286 	vop2_win_write(win, VOP2_WIN_DITHER_UP, dither_up);
1287 
1288 	vop2_win_write(win, VOP2_WIN_ENABLE, 1);
1289 
1290 	if (vop2_cluster_window(win)) {
1291 		int lb_mode = vop2_get_cluster_lb_mode(win, pstate);
1292 
1293 		vop2_win_write(win, VOP2_WIN_CLUSTER_LB_MODE, lb_mode);
1294 		vop2_win_write(win, VOP2_WIN_CLUSTER_ENABLE, 1);
1295 	}
1296 }
1297 
1298 static const struct drm_plane_helper_funcs vop2_plane_helper_funcs = {
1299 	.atomic_check = vop2_plane_atomic_check,
1300 	.atomic_update = vop2_plane_atomic_update,
1301 	.atomic_disable = vop2_plane_atomic_disable,
1302 };
1303 
1304 static const struct drm_plane_funcs vop2_plane_funcs = {
1305 	.update_plane	= drm_atomic_helper_update_plane,
1306 	.disable_plane	= drm_atomic_helper_disable_plane,
1307 	.destroy = drm_plane_cleanup,
1308 	.reset = drm_atomic_helper_plane_reset,
1309 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
1310 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
1311 	.format_mod_supported = rockchip_vop2_mod_supported,
1312 };
1313 
1314 static int vop2_crtc_enable_vblank(struct drm_crtc *crtc)
1315 {
1316 	struct vop2_video_port *vp = to_vop2_video_port(crtc);
1317 
1318 	vop2_crtc_enable_irq(vp, VP_INT_FS_FIELD);
1319 
1320 	return 0;
1321 }
1322 
1323 static void vop2_crtc_disable_vblank(struct drm_crtc *crtc)
1324 {
1325 	struct vop2_video_port *vp = to_vop2_video_port(crtc);
1326 
1327 	vop2_crtc_disable_irq(vp, VP_INT_FS_FIELD);
1328 }
1329 
1330 static bool vop2_crtc_mode_fixup(struct drm_crtc *crtc,
1331 				 const struct drm_display_mode *mode,
1332 				 struct drm_display_mode *adj_mode)
1333 {
1334 	drm_mode_set_crtcinfo(adj_mode, CRTC_INTERLACE_HALVE_V |
1335 					CRTC_STEREO_DOUBLE);
1336 
1337 	return true;
1338 }
1339 
1340 static void vop2_dither_setup(struct drm_crtc *crtc, u32 *dsp_ctrl)
1341 {
1342 	struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
1343 
1344 	switch (vcstate->bus_format) {
1345 	case MEDIA_BUS_FMT_RGB565_1X16:
1346 		*dsp_ctrl |= RK3568_VP_DSP_CTRL__DITHER_DOWN_EN;
1347 		break;
1348 	case MEDIA_BUS_FMT_RGB666_1X18:
1349 	case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
1350 	case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
1351 		*dsp_ctrl |= RK3568_VP_DSP_CTRL__DITHER_DOWN_EN;
1352 		*dsp_ctrl |= RGB888_TO_RGB666;
1353 		break;
1354 	case MEDIA_BUS_FMT_YUV8_1X24:
1355 	case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
1356 		*dsp_ctrl |= RK3568_VP_DSP_CTRL__PRE_DITHER_DOWN_EN;
1357 		break;
1358 	default:
1359 		break;
1360 	}
1361 
1362 	if (vcstate->output_mode != ROCKCHIP_OUT_MODE_AAAA)
1363 		*dsp_ctrl |= RK3568_VP_DSP_CTRL__PRE_DITHER_DOWN_EN;
1364 
1365 	*dsp_ctrl |= FIELD_PREP(RK3568_VP_DSP_CTRL__DITHER_DOWN_SEL,
1366 				DITHER_DOWN_ALLEGRO);
1367 }
1368 
1369 static void vop2_post_config(struct drm_crtc *crtc)
1370 {
1371 	struct vop2_video_port *vp = to_vop2_video_port(crtc);
1372 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
1373 	u16 vtotal = mode->crtc_vtotal;
1374 	u16 hdisplay = mode->crtc_hdisplay;
1375 	u16 hact_st = mode->crtc_htotal - mode->crtc_hsync_start;
1376 	u16 vdisplay = mode->crtc_vdisplay;
1377 	u16 vact_st = mode->crtc_vtotal - mode->crtc_vsync_start;
1378 	u32 left_margin = 100, right_margin = 100;
1379 	u32 top_margin = 100, bottom_margin = 100;
1380 	u16 hsize = hdisplay * (left_margin + right_margin) / 200;
1381 	u16 vsize = vdisplay * (top_margin + bottom_margin) / 200;
1382 	u16 hact_end, vact_end;
1383 	u32 val;
1384 
1385 	vsize = rounddown(vsize, 2);
1386 	hsize = rounddown(hsize, 2);
1387 	hact_st += hdisplay * (100 - left_margin) / 200;
1388 	hact_end = hact_st + hsize;
1389 	val = hact_st << 16;
1390 	val |= hact_end;
1391 	vop2_vp_write(vp, RK3568_VP_POST_DSP_HACT_INFO, val);
1392 	vact_st += vdisplay * (100 - top_margin) / 200;
1393 	vact_end = vact_st + vsize;
1394 	val = vact_st << 16;
1395 	val |= vact_end;
1396 	vop2_vp_write(vp, RK3568_VP_POST_DSP_VACT_INFO, val);
1397 	val = scl_cal_scale2(vdisplay, vsize) << 16;
1398 	val |= scl_cal_scale2(hdisplay, hsize);
1399 	vop2_vp_write(vp, RK3568_VP_POST_SCL_FACTOR_YRGB, val);
1400 
1401 	val = 0;
1402 	if (hdisplay != hsize)
1403 		val |= RK3568_VP_POST_SCL_CTRL__HSCALEDOWN;
1404 	if (vdisplay != vsize)
1405 		val |= RK3568_VP_POST_SCL_CTRL__VSCALEDOWN;
1406 	vop2_vp_write(vp, RK3568_VP_POST_SCL_CTRL, val);
1407 
1408 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1409 		u16 vact_st_f1 = vtotal + vact_st + 1;
1410 		u16 vact_end_f1 = vact_st_f1 + vsize;
1411 
1412 		val = vact_st_f1 << 16 | vact_end_f1;
1413 		vop2_vp_write(vp, RK3568_VP_POST_DSP_VACT_INFO_F1, val);
1414 	}
1415 
1416 	vop2_vp_write(vp, RK3568_VP_DSP_BG, 0);
1417 }
1418 
1419 static void rk3568_set_intf_mux(struct vop2_video_port *vp, int id,
1420 				u32 polflags)
1421 {
1422 	struct vop2 *vop2 = vp->vop2;
1423 	u32 die, dip;
1424 
1425 	die = vop2_readl(vop2, RK3568_DSP_IF_EN);
1426 	dip = vop2_readl(vop2, RK3568_DSP_IF_POL);
1427 
1428 	switch (id) {
1429 	case ROCKCHIP_VOP2_EP_RGB0:
1430 		die &= ~RK3568_SYS_DSP_INFACE_EN_RGB_MUX;
1431 		die |= RK3568_SYS_DSP_INFACE_EN_RGB |
1432 			   FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_RGB_MUX, vp->id);
1433 		if (polflags & POLFLAG_DCLK_INV)
1434 			regmap_write(vop2->grf, RK3568_GRF_VO_CON1, BIT(3 + 16) | BIT(3));
1435 		else
1436 			regmap_write(vop2->grf, RK3568_GRF_VO_CON1, BIT(3 + 16));
1437 		break;
1438 	case ROCKCHIP_VOP2_EP_HDMI0:
1439 		die &= ~RK3568_SYS_DSP_INFACE_EN_HDMI_MUX;
1440 		die |= RK3568_SYS_DSP_INFACE_EN_HDMI |
1441 			   FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_HDMI_MUX, vp->id);
1442 		break;
1443 	case ROCKCHIP_VOP2_EP_EDP0:
1444 		die &= ~RK3568_SYS_DSP_INFACE_EN_EDP_MUX;
1445 		die |= RK3568_SYS_DSP_INFACE_EN_EDP |
1446 			   FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_EDP_MUX, vp->id);
1447 		break;
1448 	case ROCKCHIP_VOP2_EP_MIPI0:
1449 		die &= ~RK3568_SYS_DSP_INFACE_EN_MIPI0_MUX;
1450 		die |= RK3568_SYS_DSP_INFACE_EN_MIPI0 |
1451 			   FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_MIPI0_MUX, vp->id);
1452 		dip &= ~RK3568_DSP_IF_POL__MIPI_PIN_POL;
1453 		dip |= FIELD_PREP(RK3568_DSP_IF_POL__MIPI_PIN_POL, polflags);
1454 		break;
1455 	case ROCKCHIP_VOP2_EP_MIPI1:
1456 		die &= ~RK3568_SYS_DSP_INFACE_EN_MIPI1_MUX;
1457 		die |= RK3568_SYS_DSP_INFACE_EN_MIPI1 |
1458 			   FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_MIPI1_MUX, vp->id);
1459 		dip &= ~RK3568_DSP_IF_POL__MIPI_PIN_POL;
1460 		dip |= FIELD_PREP(RK3568_DSP_IF_POL__MIPI_PIN_POL, polflags);
1461 		break;
1462 	case ROCKCHIP_VOP2_EP_LVDS0:
1463 		die &= ~RK3568_SYS_DSP_INFACE_EN_LVDS0_MUX;
1464 		die |= RK3568_SYS_DSP_INFACE_EN_LVDS0 |
1465 			   FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_LVDS0_MUX, vp->id);
1466 		dip &= ~RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL;
1467 		dip |= FIELD_PREP(RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL, polflags);
1468 		break;
1469 	case ROCKCHIP_VOP2_EP_LVDS1:
1470 		die &= ~RK3568_SYS_DSP_INFACE_EN_LVDS1_MUX;
1471 		die |= RK3568_SYS_DSP_INFACE_EN_LVDS1 |
1472 			   FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_LVDS1_MUX, vp->id);
1473 		dip &= ~RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL;
1474 		dip |= FIELD_PREP(RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL, polflags);
1475 		break;
1476 	default:
1477 		drm_err(vop2->drm, "Invalid interface id %d on vp%d\n", id, vp->id);
1478 		return;
1479 	}
1480 
1481 	dip |= RK3568_DSP_IF_POL__CFG_DONE_IMD;
1482 
1483 	vop2_writel(vop2, RK3568_DSP_IF_EN, die);
1484 	vop2_writel(vop2, RK3568_DSP_IF_POL, dip);
1485 }
1486 
1487 static int us_to_vertical_line(struct drm_display_mode *mode, int us)
1488 {
1489 	return us * mode->clock / mode->htotal / 1000;
1490 }
1491 
1492 static void vop2_crtc_atomic_enable(struct drm_crtc *crtc,
1493 				    struct drm_atomic_state *state)
1494 {
1495 	struct vop2_video_port *vp = to_vop2_video_port(crtc);
1496 	struct vop2 *vop2 = vp->vop2;
1497 	const struct vop2_data *vop2_data = vop2->data;
1498 	const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
1499 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1500 	struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
1501 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
1502 	unsigned long clock = mode->crtc_clock * 1000;
1503 	u16 hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
1504 	u16 hdisplay = mode->crtc_hdisplay;
1505 	u16 htotal = mode->crtc_htotal;
1506 	u16 hact_st = mode->crtc_htotal - mode->crtc_hsync_start;
1507 	u16 hact_end = hact_st + hdisplay;
1508 	u16 vdisplay = mode->crtc_vdisplay;
1509 	u16 vtotal = mode->crtc_vtotal;
1510 	u16 vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
1511 	u16 vact_st = mode->crtc_vtotal - mode->crtc_vsync_start;
1512 	u16 vact_end = vact_st + vdisplay;
1513 	u8 out_mode;
1514 	u32 dsp_ctrl = 0;
1515 	int act_end;
1516 	u32 val, polflags;
1517 	int ret;
1518 	struct drm_encoder *encoder;
1519 
1520 	drm_dbg(vop2->drm, "Update mode to %dx%d%s%d, type: %d for vp%d\n",
1521 		hdisplay, vdisplay, mode->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "p",
1522 		drm_mode_vrefresh(mode), vcstate->output_type, vp->id);
1523 
1524 	vop2_lock(vop2);
1525 
1526 	ret = clk_prepare_enable(vp->dclk);
1527 	if (ret < 0) {
1528 		drm_err(vop2->drm, "failed to enable dclk for video port%d - %d\n",
1529 			vp->id, ret);
1530 		vop2_unlock(vop2);
1531 		return;
1532 	}
1533 
1534 	if (!vop2->enable_count)
1535 		vop2_enable(vop2);
1536 
1537 	vop2->enable_count++;
1538 
1539 	vop2_crtc_enable_irq(vp, VP_INT_POST_BUF_EMPTY);
1540 
1541 	polflags = 0;
1542 	if (vcstate->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
1543 		polflags |= POLFLAG_DCLK_INV;
1544 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1545 		polflags |= BIT(HSYNC_POSITIVE);
1546 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1547 		polflags |= BIT(VSYNC_POSITIVE);
1548 
1549 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc_state->encoder_mask) {
1550 		struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
1551 
1552 		rk3568_set_intf_mux(vp, rkencoder->crtc_endpoint_id, polflags);
1553 	}
1554 
1555 	if (vcstate->output_mode == ROCKCHIP_OUT_MODE_AAAA &&
1556 	    !(vp_data->feature & VOP_FEATURE_OUTPUT_10BIT))
1557 		out_mode = ROCKCHIP_OUT_MODE_P888;
1558 	else
1559 		out_mode = vcstate->output_mode;
1560 
1561 	dsp_ctrl |= FIELD_PREP(RK3568_VP_DSP_CTRL__OUT_MODE, out_mode);
1562 
1563 	if (vop2_output_uv_swap(vcstate->bus_format, vcstate->output_mode))
1564 		dsp_ctrl |= RK3568_VP_DSP_CTRL__DSP_RB_SWAP;
1565 
1566 	if (is_yuv_output(vcstate->bus_format))
1567 		dsp_ctrl |= RK3568_VP_DSP_CTRL__POST_DSP_OUT_R2Y;
1568 
1569 	vop2_dither_setup(crtc, &dsp_ctrl);
1570 
1571 	vop2_vp_write(vp, RK3568_VP_DSP_HTOTAL_HS_END, (htotal << 16) | hsync_len);
1572 	val = hact_st << 16;
1573 	val |= hact_end;
1574 	vop2_vp_write(vp, RK3568_VP_DSP_HACT_ST_END, val);
1575 
1576 	val = vact_st << 16;
1577 	val |= vact_end;
1578 	vop2_vp_write(vp, RK3568_VP_DSP_VACT_ST_END, val);
1579 
1580 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1581 		u16 vact_st_f1 = vtotal + vact_st + 1;
1582 		u16 vact_end_f1 = vact_st_f1 + vdisplay;
1583 
1584 		val = vact_st_f1 << 16 | vact_end_f1;
1585 		vop2_vp_write(vp, RK3568_VP_DSP_VACT_ST_END_F1, val);
1586 
1587 		val = vtotal << 16 | (vtotal + vsync_len);
1588 		vop2_vp_write(vp, RK3568_VP_DSP_VS_ST_END_F1, val);
1589 		dsp_ctrl |= RK3568_VP_DSP_CTRL__DSP_INTERLACE;
1590 		dsp_ctrl |= RK3568_VP_DSP_CTRL__DSP_FILED_POL;
1591 		dsp_ctrl |= RK3568_VP_DSP_CTRL__P2I_EN;
1592 		vtotal += vtotal + 1;
1593 		act_end = vact_end_f1;
1594 	} else {
1595 		act_end = vact_end;
1596 	}
1597 
1598 	vop2_writel(vop2, RK3568_VP_LINE_FLAG(vp->id),
1599 		    (act_end - us_to_vertical_line(mode, 0)) << 16 | act_end);
1600 
1601 	vop2_vp_write(vp, RK3568_VP_DSP_VTOTAL_VS_END, vtotal << 16 | vsync_len);
1602 
1603 	if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
1604 		dsp_ctrl |= RK3568_VP_DSP_CTRL__CORE_DCLK_DIV;
1605 		clock *= 2;
1606 	}
1607 
1608 	vop2_vp_write(vp, RK3568_VP_MIPI_CTRL, 0);
1609 
1610 	clk_set_rate(vp->dclk, clock);
1611 
1612 	vop2_post_config(crtc);
1613 
1614 	vop2_cfg_done(vp);
1615 
1616 	vop2_vp_write(vp, RK3568_VP_DSP_CTRL, dsp_ctrl);
1617 
1618 	drm_crtc_vblank_on(crtc);
1619 
1620 	vop2_unlock(vop2);
1621 }
1622 
1623 static int vop2_crtc_atomic_check(struct drm_crtc *crtc,
1624 				  struct drm_atomic_state *state)
1625 {
1626 	struct vop2_video_port *vp = to_vop2_video_port(crtc);
1627 	struct drm_plane *plane;
1628 	int nplanes = 0;
1629 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1630 
1631 	drm_atomic_crtc_state_for_each_plane(plane, crtc_state)
1632 		nplanes++;
1633 
1634 	if (nplanes > vp->nlayers)
1635 		return -EINVAL;
1636 
1637 	return 0;
1638 }
1639 
1640 static bool is_opaque(u16 alpha)
1641 {
1642 	return (alpha >> 8) == 0xff;
1643 }
1644 
1645 static void vop2_parse_alpha(struct vop2_alpha_config *alpha_config,
1646 			     struct vop2_alpha *alpha)
1647 {
1648 	int src_glb_alpha_en = is_opaque(alpha_config->src_glb_alpha_value) ? 0 : 1;
1649 	int dst_glb_alpha_en = is_opaque(alpha_config->dst_glb_alpha_value) ? 0 : 1;
1650 	int src_color_mode = alpha_config->src_premulti_en ?
1651 				ALPHA_SRC_PRE_MUL : ALPHA_SRC_NO_PRE_MUL;
1652 	int dst_color_mode = alpha_config->dst_premulti_en ?
1653 				ALPHA_SRC_PRE_MUL : ALPHA_SRC_NO_PRE_MUL;
1654 
1655 	alpha->src_color_ctrl.val = 0;
1656 	alpha->dst_color_ctrl.val = 0;
1657 	alpha->src_alpha_ctrl.val = 0;
1658 	alpha->dst_alpha_ctrl.val = 0;
1659 
1660 	if (!alpha_config->src_pixel_alpha_en)
1661 		alpha->src_color_ctrl.bits.blend_mode = ALPHA_GLOBAL;
1662 	else if (alpha_config->src_pixel_alpha_en && !src_glb_alpha_en)
1663 		alpha->src_color_ctrl.bits.blend_mode = ALPHA_PER_PIX;
1664 	else
1665 		alpha->src_color_ctrl.bits.blend_mode = ALPHA_PER_PIX_GLOBAL;
1666 
1667 	alpha->src_color_ctrl.bits.alpha_en = 1;
1668 
1669 	if (alpha->src_color_ctrl.bits.blend_mode == ALPHA_GLOBAL) {
1670 		alpha->src_color_ctrl.bits.color_mode = src_color_mode;
1671 		alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_SRC_GLOBAL;
1672 	} else if (alpha->src_color_ctrl.bits.blend_mode == ALPHA_PER_PIX) {
1673 		alpha->src_color_ctrl.bits.color_mode = src_color_mode;
1674 		alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_ONE;
1675 	} else {
1676 		alpha->src_color_ctrl.bits.color_mode = ALPHA_SRC_PRE_MUL;
1677 		alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_SRC_GLOBAL;
1678 	}
1679 	alpha->src_color_ctrl.bits.glb_alpha = alpha_config->src_glb_alpha_value >> 8;
1680 	alpha->src_color_ctrl.bits.alpha_mode = ALPHA_STRAIGHT;
1681 	alpha->src_color_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION;
1682 
1683 	alpha->dst_color_ctrl.bits.alpha_mode = ALPHA_STRAIGHT;
1684 	alpha->dst_color_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION;
1685 	alpha->dst_color_ctrl.bits.blend_mode = ALPHA_GLOBAL;
1686 	alpha->dst_color_ctrl.bits.glb_alpha = alpha_config->dst_glb_alpha_value >> 8;
1687 	alpha->dst_color_ctrl.bits.color_mode = dst_color_mode;
1688 	alpha->dst_color_ctrl.bits.factor_mode = ALPHA_SRC_INVERSE;
1689 
1690 	alpha->src_alpha_ctrl.bits.alpha_mode = ALPHA_STRAIGHT;
1691 	alpha->src_alpha_ctrl.bits.blend_mode = alpha->src_color_ctrl.bits.blend_mode;
1692 	alpha->src_alpha_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION;
1693 	alpha->src_alpha_ctrl.bits.factor_mode = ALPHA_ONE;
1694 
1695 	alpha->dst_alpha_ctrl.bits.alpha_mode = ALPHA_STRAIGHT;
1696 	if (alpha_config->dst_pixel_alpha_en && !dst_glb_alpha_en)
1697 		alpha->dst_alpha_ctrl.bits.blend_mode = ALPHA_PER_PIX;
1698 	else
1699 		alpha->dst_alpha_ctrl.bits.blend_mode = ALPHA_PER_PIX_GLOBAL;
1700 	alpha->dst_alpha_ctrl.bits.alpha_cal_mode = ALPHA_NO_SATURATION;
1701 	alpha->dst_alpha_ctrl.bits.factor_mode = ALPHA_SRC_INVERSE;
1702 }
1703 
1704 static int vop2_find_start_mixer_id_for_vp(struct vop2 *vop2, u8 port_id)
1705 {
1706 	struct vop2_video_port *vp;
1707 	int used_layer = 0;
1708 	int i;
1709 
1710 	for (i = 0; i < port_id; i++) {
1711 		vp = &vop2->vps[i];
1712 		used_layer += hweight32(vp->win_mask);
1713 	}
1714 
1715 	return used_layer;
1716 }
1717 
1718 static void vop2_setup_cluster_alpha(struct vop2 *vop2, struct vop2_win *main_win)
1719 {
1720 	u32 offset = (main_win->data->phys_id * 0x10);
1721 	struct vop2_alpha_config alpha_config;
1722 	struct vop2_alpha alpha;
1723 	struct drm_plane_state *bottom_win_pstate;
1724 	bool src_pixel_alpha_en = false;
1725 	u16 src_glb_alpha_val, dst_glb_alpha_val;
1726 	bool premulti_en = false;
1727 	bool swap = false;
1728 
1729 	/* At one win mode, win0 is dst/bottom win, and win1 is a all zero src/top win */
1730 	bottom_win_pstate = main_win->base.state;
1731 	src_glb_alpha_val = 0;
1732 	dst_glb_alpha_val = main_win->base.state->alpha;
1733 
1734 	if (!bottom_win_pstate->fb)
1735 		return;
1736 
1737 	alpha_config.src_premulti_en = premulti_en;
1738 	alpha_config.dst_premulti_en = false;
1739 	alpha_config.src_pixel_alpha_en = src_pixel_alpha_en;
1740 	alpha_config.dst_pixel_alpha_en = true; /* alpha value need transfer to next mix */
1741 	alpha_config.src_glb_alpha_value = src_glb_alpha_val;
1742 	alpha_config.dst_glb_alpha_value = dst_glb_alpha_val;
1743 	vop2_parse_alpha(&alpha_config, &alpha);
1744 
1745 	alpha.src_color_ctrl.bits.src_dst_swap = swap;
1746 	vop2_writel(vop2, RK3568_CLUSTER0_MIX_SRC_COLOR_CTRL + offset,
1747 		    alpha.src_color_ctrl.val);
1748 	vop2_writel(vop2, RK3568_CLUSTER0_MIX_DST_COLOR_CTRL + offset,
1749 		    alpha.dst_color_ctrl.val);
1750 	vop2_writel(vop2, RK3568_CLUSTER0_MIX_SRC_ALPHA_CTRL + offset,
1751 		    alpha.src_alpha_ctrl.val);
1752 	vop2_writel(vop2, RK3568_CLUSTER0_MIX_DST_ALPHA_CTRL + offset,
1753 		    alpha.dst_alpha_ctrl.val);
1754 }
1755 
1756 static void vop2_setup_alpha(struct vop2_video_port *vp)
1757 {
1758 	struct vop2 *vop2 = vp->vop2;
1759 	struct drm_framebuffer *fb;
1760 	struct vop2_alpha_config alpha_config;
1761 	struct vop2_alpha alpha;
1762 	struct drm_plane *plane;
1763 	int pixel_alpha_en;
1764 	int premulti_en, gpremulti_en = 0;
1765 	int mixer_id;
1766 	u32 offset;
1767 	bool bottom_layer_alpha_en = false;
1768 	u32 dst_global_alpha = DRM_BLEND_ALPHA_OPAQUE;
1769 
1770 	mixer_id = vop2_find_start_mixer_id_for_vp(vop2, vp->id);
1771 	alpha_config.dst_pixel_alpha_en = true; /* alpha value need transfer to next mix */
1772 
1773 	drm_atomic_crtc_for_each_plane(plane, &vp->crtc) {
1774 		struct vop2_win *win = to_vop2_win(plane);
1775 
1776 		if (plane->state->normalized_zpos == 0 &&
1777 		    !is_opaque(plane->state->alpha) &&
1778 		    !vop2_cluster_window(win)) {
1779 			/*
1780 			 * If bottom layer have global alpha effect [except cluster layer,
1781 			 * because cluster have deal with bottom layer global alpha value
1782 			 * at cluster mix], bottom layer mix need deal with global alpha.
1783 			 */
1784 			bottom_layer_alpha_en = true;
1785 			dst_global_alpha = plane->state->alpha;
1786 		}
1787 	}
1788 
1789 	drm_atomic_crtc_for_each_plane(plane, &vp->crtc) {
1790 		struct vop2_win *win = to_vop2_win(plane);
1791 		int zpos = plane->state->normalized_zpos;
1792 
1793 		if (plane->state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
1794 			premulti_en = 1;
1795 		else
1796 			premulti_en = 0;
1797 
1798 		plane = &win->base;
1799 		fb = plane->state->fb;
1800 
1801 		pixel_alpha_en = fb->format->has_alpha;
1802 
1803 		alpha_config.src_premulti_en = premulti_en;
1804 
1805 		if (bottom_layer_alpha_en && zpos == 1) {
1806 			gpremulti_en = premulti_en;
1807 			/* Cd = Cs + (1 - As) * Cd * Agd */
1808 			alpha_config.dst_premulti_en = false;
1809 			alpha_config.src_pixel_alpha_en = pixel_alpha_en;
1810 			alpha_config.src_glb_alpha_value = plane->state->alpha;
1811 			alpha_config.dst_glb_alpha_value = dst_global_alpha;
1812 		} else if (vop2_cluster_window(win)) {
1813 			/* Mix output data only have pixel alpha */
1814 			alpha_config.dst_premulti_en = true;
1815 			alpha_config.src_pixel_alpha_en = true;
1816 			alpha_config.src_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE;
1817 			alpha_config.dst_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE;
1818 		} else {
1819 			/* Cd = Cs + (1 - As) * Cd */
1820 			alpha_config.dst_premulti_en = true;
1821 			alpha_config.src_pixel_alpha_en = pixel_alpha_en;
1822 			alpha_config.src_glb_alpha_value = plane->state->alpha;
1823 			alpha_config.dst_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE;
1824 		}
1825 
1826 		vop2_parse_alpha(&alpha_config, &alpha);
1827 
1828 		offset = (mixer_id + zpos - 1) * 0x10;
1829 		vop2_writel(vop2, RK3568_MIX0_SRC_COLOR_CTRL + offset,
1830 			    alpha.src_color_ctrl.val);
1831 		vop2_writel(vop2, RK3568_MIX0_DST_COLOR_CTRL + offset,
1832 			    alpha.dst_color_ctrl.val);
1833 		vop2_writel(vop2, RK3568_MIX0_SRC_ALPHA_CTRL + offset,
1834 			    alpha.src_alpha_ctrl.val);
1835 		vop2_writel(vop2, RK3568_MIX0_DST_ALPHA_CTRL + offset,
1836 			    alpha.dst_alpha_ctrl.val);
1837 	}
1838 
1839 	if (vp->id == 0) {
1840 		if (bottom_layer_alpha_en) {
1841 			/* Transfer pixel alpha to hdr mix */
1842 			alpha_config.src_premulti_en = gpremulti_en;
1843 			alpha_config.dst_premulti_en = true;
1844 			alpha_config.src_pixel_alpha_en = true;
1845 			alpha_config.src_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE;
1846 			alpha_config.dst_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE;
1847 			vop2_parse_alpha(&alpha_config, &alpha);
1848 
1849 			vop2_writel(vop2, RK3568_HDR0_SRC_COLOR_CTRL,
1850 				    alpha.src_color_ctrl.val);
1851 			vop2_writel(vop2, RK3568_HDR0_DST_COLOR_CTRL,
1852 				    alpha.dst_color_ctrl.val);
1853 			vop2_writel(vop2, RK3568_HDR0_SRC_ALPHA_CTRL,
1854 				    alpha.src_alpha_ctrl.val);
1855 			vop2_writel(vop2, RK3568_HDR0_DST_ALPHA_CTRL,
1856 				    alpha.dst_alpha_ctrl.val);
1857 		} else {
1858 			vop2_writel(vop2, RK3568_HDR0_SRC_COLOR_CTRL, 0);
1859 		}
1860 	}
1861 }
1862 
1863 static void vop2_setup_layer_mixer(struct vop2_video_port *vp)
1864 {
1865 	struct vop2 *vop2 = vp->vop2;
1866 	struct drm_plane *plane;
1867 	u32 layer_sel = 0;
1868 	u32 port_sel;
1869 	unsigned int nlayer, ofs;
1870 	struct drm_display_mode *adjusted_mode;
1871 	u16 hsync_len;
1872 	u16 hdisplay;
1873 	u32 bg_dly;
1874 	u32 pre_scan_dly;
1875 	int i;
1876 	struct vop2_video_port *vp0 = &vop2->vps[0];
1877 	struct vop2_video_port *vp1 = &vop2->vps[1];
1878 	struct vop2_video_port *vp2 = &vop2->vps[2];
1879 
1880 	adjusted_mode = &vp->crtc.state->adjusted_mode;
1881 	hsync_len = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
1882 	hdisplay = adjusted_mode->crtc_hdisplay;
1883 
1884 	bg_dly = vp->data->pre_scan_max_dly[3];
1885 	vop2_writel(vop2, RK3568_VP_BG_MIX_CTRL(vp->id),
1886 		    FIELD_PREP(RK3568_VP_BG_MIX_CTRL__BG_DLY, bg_dly));
1887 
1888 	pre_scan_dly = ((bg_dly + (hdisplay >> 1) - 1) << 16) | hsync_len;
1889 	vop2_vp_write(vp, RK3568_VP_PRE_SCAN_HTIMING, pre_scan_dly);
1890 
1891 	vop2_writel(vop2, RK3568_OVL_CTRL, 0);
1892 	port_sel = vop2_readl(vop2, RK3568_OVL_PORT_SEL);
1893 	port_sel &= RK3568_OVL_PORT_SEL__SEL_PORT;
1894 
1895 	if (vp0->nlayers)
1896 		port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT0_MUX,
1897 				     vp0->nlayers - 1);
1898 	else
1899 		port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT0_MUX, 8);
1900 
1901 	if (vp1->nlayers)
1902 		port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT1_MUX,
1903 				     (vp0->nlayers + vp1->nlayers - 1));
1904 	else
1905 		port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT1_MUX, 8);
1906 
1907 	if (vp2->nlayers)
1908 		port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT2_MUX,
1909 			(vp2->nlayers + vp1->nlayers + vp0->nlayers - 1));
1910 	else
1911 		port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT1_MUX, 8);
1912 
1913 	layer_sel = vop2_readl(vop2, RK3568_OVL_LAYER_SEL);
1914 
1915 	ofs = 0;
1916 	for (i = 0; i < vp->id; i++)
1917 		ofs += vop2->vps[i].nlayers;
1918 
1919 	nlayer = 0;
1920 	drm_atomic_crtc_for_each_plane(plane, &vp->crtc) {
1921 		struct vop2_win *win = to_vop2_win(plane);
1922 
1923 		switch (win->data->phys_id) {
1924 		case ROCKCHIP_VOP2_CLUSTER0:
1925 			port_sel &= ~RK3568_OVL_PORT_SEL__CLUSTER0;
1926 			port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__CLUSTER0, vp->id);
1927 			break;
1928 		case ROCKCHIP_VOP2_CLUSTER1:
1929 			port_sel &= ~RK3568_OVL_PORT_SEL__CLUSTER1;
1930 			port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__CLUSTER1, vp->id);
1931 			break;
1932 		case ROCKCHIP_VOP2_ESMART0:
1933 			port_sel &= ~RK3568_OVL_PORT_SEL__ESMART0;
1934 			port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__ESMART0, vp->id);
1935 			break;
1936 		case ROCKCHIP_VOP2_ESMART1:
1937 			port_sel &= ~RK3568_OVL_PORT_SEL__ESMART1;
1938 			port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__ESMART1, vp->id);
1939 			break;
1940 		case ROCKCHIP_VOP2_SMART0:
1941 			port_sel &= ~RK3568_OVL_PORT_SEL__SMART0;
1942 			port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__SMART0, vp->id);
1943 			break;
1944 		case ROCKCHIP_VOP2_SMART1:
1945 			port_sel &= ~RK3568_OVL_PORT_SEL__SMART1;
1946 			port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__SMART1, vp->id);
1947 			break;
1948 		}
1949 
1950 		layer_sel &= ~RK3568_OVL_LAYER_SEL__LAYER(plane->state->normalized_zpos + ofs,
1951 							  0x7);
1952 		layer_sel |= RK3568_OVL_LAYER_SEL__LAYER(plane->state->normalized_zpos + ofs,
1953 							 win->data->layer_sel_id);
1954 		nlayer++;
1955 	}
1956 
1957 	/* configure unused layers to 0x5 (reserved) */
1958 	for (; nlayer < vp->nlayers; nlayer++) {
1959 		layer_sel &= ~RK3568_OVL_LAYER_SEL__LAYER(nlayer + ofs, 0x7);
1960 		layer_sel |= RK3568_OVL_LAYER_SEL__LAYER(nlayer + ofs, 5);
1961 	}
1962 
1963 	vop2_writel(vop2, RK3568_OVL_LAYER_SEL, layer_sel);
1964 	vop2_writel(vop2, RK3568_OVL_PORT_SEL, port_sel);
1965 	vop2_writel(vop2, RK3568_OVL_CTRL, RK3568_OVL_CTRL__LAYERSEL_REGDONE_IMD);
1966 }
1967 
1968 static void vop2_setup_dly_for_windows(struct vop2 *vop2)
1969 {
1970 	struct vop2_win *win;
1971 	int i = 0;
1972 	u32 cdly = 0, sdly = 0;
1973 
1974 	for (i = 0; i < vop2->data->win_size; i++) {
1975 		u32 dly;
1976 
1977 		win = &vop2->win[i];
1978 		dly = win->delay;
1979 
1980 		switch (win->data->phys_id) {
1981 		case ROCKCHIP_VOP2_CLUSTER0:
1982 			cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER0_0, dly);
1983 			cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER0_1, dly);
1984 			break;
1985 		case ROCKCHIP_VOP2_CLUSTER1:
1986 			cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER1_0, dly);
1987 			cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER1_1, dly);
1988 			break;
1989 		case ROCKCHIP_VOP2_ESMART0:
1990 			sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__ESMART0, dly);
1991 			break;
1992 		case ROCKCHIP_VOP2_ESMART1:
1993 			sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__ESMART1, dly);
1994 			break;
1995 		case ROCKCHIP_VOP2_SMART0:
1996 			sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__SMART0, dly);
1997 			break;
1998 		case ROCKCHIP_VOP2_SMART1:
1999 			sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__SMART1, dly);
2000 			break;
2001 		}
2002 	}
2003 
2004 	vop2_writel(vop2, RK3568_CLUSTER_DLY_NUM, cdly);
2005 	vop2_writel(vop2, RK3568_SMART_DLY_NUM, sdly);
2006 }
2007 
2008 static void vop2_crtc_atomic_begin(struct drm_crtc *crtc,
2009 				   struct drm_atomic_state *state)
2010 {
2011 	struct vop2_video_port *vp = to_vop2_video_port(crtc);
2012 	struct vop2 *vop2 = vp->vop2;
2013 	struct drm_plane *plane;
2014 
2015 	vp->win_mask = 0;
2016 
2017 	drm_atomic_crtc_for_each_plane(plane, crtc) {
2018 		struct vop2_win *win = to_vop2_win(plane);
2019 
2020 		win->delay = win->data->dly[VOP2_DLY_MODE_DEFAULT];
2021 
2022 		vp->win_mask |= BIT(win->data->phys_id);
2023 
2024 		if (vop2_cluster_window(win))
2025 			vop2_setup_cluster_alpha(vop2, win);
2026 	}
2027 
2028 	if (!vp->win_mask)
2029 		return;
2030 
2031 	vop2_setup_layer_mixer(vp);
2032 	vop2_setup_alpha(vp);
2033 	vop2_setup_dly_for_windows(vop2);
2034 }
2035 
2036 static void vop2_crtc_atomic_flush(struct drm_crtc *crtc,
2037 				   struct drm_atomic_state *state)
2038 {
2039 	struct vop2_video_port *vp = to_vop2_video_port(crtc);
2040 
2041 	vop2_post_config(crtc);
2042 
2043 	vop2_cfg_done(vp);
2044 
2045 	spin_lock_irq(&crtc->dev->event_lock);
2046 
2047 	if (crtc->state->event) {
2048 		WARN_ON(drm_crtc_vblank_get(crtc));
2049 		vp->event = crtc->state->event;
2050 		crtc->state->event = NULL;
2051 	}
2052 
2053 	spin_unlock_irq(&crtc->dev->event_lock);
2054 }
2055 
2056 static const struct drm_crtc_helper_funcs vop2_crtc_helper_funcs = {
2057 	.mode_fixup = vop2_crtc_mode_fixup,
2058 	.atomic_check = vop2_crtc_atomic_check,
2059 	.atomic_begin = vop2_crtc_atomic_begin,
2060 	.atomic_flush = vop2_crtc_atomic_flush,
2061 	.atomic_enable = vop2_crtc_atomic_enable,
2062 	.atomic_disable = vop2_crtc_atomic_disable,
2063 };
2064 
2065 static void vop2_crtc_reset(struct drm_crtc *crtc)
2066 {
2067 	struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
2068 
2069 	if (crtc->state) {
2070 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
2071 		kfree(vcstate);
2072 	}
2073 
2074 	vcstate = kzalloc(sizeof(*vcstate), GFP_KERNEL);
2075 	if (!vcstate)
2076 		return;
2077 
2078 	crtc->state = &vcstate->base;
2079 	crtc->state->crtc = crtc;
2080 }
2081 
2082 static struct drm_crtc_state *vop2_crtc_duplicate_state(struct drm_crtc *crtc)
2083 {
2084 	struct rockchip_crtc_state *vcstate, *old_vcstate;
2085 
2086 	old_vcstate = to_rockchip_crtc_state(crtc->state);
2087 
2088 	vcstate = kmemdup(old_vcstate, sizeof(*old_vcstate), GFP_KERNEL);
2089 	if (!vcstate)
2090 		return NULL;
2091 
2092 	__drm_atomic_helper_crtc_duplicate_state(crtc, &vcstate->base);
2093 
2094 	return &vcstate->base;
2095 }
2096 
2097 static void vop2_crtc_destroy_state(struct drm_crtc *crtc,
2098 				    struct drm_crtc_state *state)
2099 {
2100 	struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(state);
2101 
2102 	__drm_atomic_helper_crtc_destroy_state(&vcstate->base);
2103 	kfree(vcstate);
2104 }
2105 
2106 static const struct drm_crtc_funcs vop2_crtc_funcs = {
2107 	.set_config = drm_atomic_helper_set_config,
2108 	.page_flip = drm_atomic_helper_page_flip,
2109 	.destroy = drm_crtc_cleanup,
2110 	.reset = vop2_crtc_reset,
2111 	.atomic_duplicate_state = vop2_crtc_duplicate_state,
2112 	.atomic_destroy_state = vop2_crtc_destroy_state,
2113 	.enable_vblank = vop2_crtc_enable_vblank,
2114 	.disable_vblank = vop2_crtc_disable_vblank,
2115 };
2116 
2117 static irqreturn_t vop2_isr(int irq, void *data)
2118 {
2119 	struct vop2 *vop2 = data;
2120 	const struct vop2_data *vop2_data = vop2->data;
2121 	u32 axi_irqs[VOP2_SYS_AXI_BUS_NUM];
2122 	int ret = IRQ_NONE;
2123 	int i;
2124 
2125 	/*
2126 	 * The irq is shared with the iommu. If the runtime-pm state of the
2127 	 * vop2-device is disabled the irq has to be targeted at the iommu.
2128 	 */
2129 	if (!pm_runtime_get_if_in_use(vop2->dev))
2130 		return IRQ_NONE;
2131 
2132 	for (i = 0; i < vop2_data->nr_vps; i++) {
2133 		struct vop2_video_port *vp = &vop2->vps[i];
2134 		struct drm_crtc *crtc = &vp->crtc;
2135 		u32 irqs;
2136 
2137 		irqs = vop2_readl(vop2, RK3568_VP_INT_STATUS(vp->id));
2138 		vop2_writel(vop2, RK3568_VP_INT_CLR(vp->id), irqs << 16 | irqs);
2139 
2140 		if (irqs & VP_INT_DSP_HOLD_VALID) {
2141 			complete(&vp->dsp_hold_completion);
2142 			ret = IRQ_HANDLED;
2143 		}
2144 
2145 		if (irqs & VP_INT_FS_FIELD) {
2146 			drm_crtc_handle_vblank(crtc);
2147 			spin_lock(&crtc->dev->event_lock);
2148 			if (vp->event) {
2149 				u32 val = vop2_readl(vop2, RK3568_REG_CFG_DONE);
2150 
2151 				if (!(val & BIT(vp->id))) {
2152 					drm_crtc_send_vblank_event(crtc, vp->event);
2153 					vp->event = NULL;
2154 					drm_crtc_vblank_put(crtc);
2155 				}
2156 			}
2157 			spin_unlock(&crtc->dev->event_lock);
2158 
2159 			ret = IRQ_HANDLED;
2160 		}
2161 
2162 		if (irqs & VP_INT_POST_BUF_EMPTY) {
2163 			drm_err_ratelimited(vop2->drm,
2164 					    "POST_BUF_EMPTY irq err at vp%d\n",
2165 					    vp->id);
2166 			ret = IRQ_HANDLED;
2167 		}
2168 	}
2169 
2170 	axi_irqs[0] = vop2_readl(vop2, RK3568_SYS0_INT_STATUS);
2171 	vop2_writel(vop2, RK3568_SYS0_INT_CLR, axi_irqs[0] << 16 | axi_irqs[0]);
2172 	axi_irqs[1] = vop2_readl(vop2, RK3568_SYS1_INT_STATUS);
2173 	vop2_writel(vop2, RK3568_SYS1_INT_CLR, axi_irqs[1] << 16 | axi_irqs[1]);
2174 
2175 	for (i = 0; i < ARRAY_SIZE(axi_irqs); i++) {
2176 		if (axi_irqs[i] & VOP2_INT_BUS_ERRPR) {
2177 			drm_err_ratelimited(vop2->drm, "BUS_ERROR irq err\n");
2178 			ret = IRQ_HANDLED;
2179 		}
2180 	}
2181 
2182 	pm_runtime_put(vop2->dev);
2183 
2184 	return ret;
2185 }
2186 
2187 static int vop2_plane_init(struct vop2 *vop2, struct vop2_win *win,
2188 			   unsigned long possible_crtcs)
2189 {
2190 	const struct vop2_win_data *win_data = win->data;
2191 	unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
2192 				  BIT(DRM_MODE_BLEND_PREMULTI) |
2193 				  BIT(DRM_MODE_BLEND_COVERAGE);
2194 	int ret;
2195 
2196 	ret = drm_universal_plane_init(vop2->drm, &win->base, possible_crtcs,
2197 				       &vop2_plane_funcs, win_data->formats,
2198 				       win_data->nformats,
2199 				       win_data->format_modifiers,
2200 				       win->type, win_data->name);
2201 	if (ret) {
2202 		drm_err(vop2->drm, "failed to initialize plane %d\n", ret);
2203 		return ret;
2204 	}
2205 
2206 	drm_plane_helper_add(&win->base, &vop2_plane_helper_funcs);
2207 
2208 	if (win->data->supported_rotations)
2209 		drm_plane_create_rotation_property(&win->base, DRM_MODE_ROTATE_0,
2210 						   DRM_MODE_ROTATE_0 |
2211 						   win->data->supported_rotations);
2212 	drm_plane_create_alpha_property(&win->base);
2213 	drm_plane_create_blend_mode_property(&win->base, blend_caps);
2214 	drm_plane_create_zpos_property(&win->base, win->win_id, 0,
2215 				       vop2->registered_num_wins - 1);
2216 
2217 	return 0;
2218 }
2219 
2220 static struct vop2_video_port *find_vp_without_primary(struct vop2 *vop2)
2221 {
2222 	int i;
2223 
2224 	for (i = 0; i < vop2->data->nr_vps; i++) {
2225 		struct vop2_video_port *vp = &vop2->vps[i];
2226 
2227 		if (!vp->crtc.port)
2228 			continue;
2229 		if (vp->primary_plane)
2230 			continue;
2231 
2232 		return vp;
2233 	}
2234 
2235 	return NULL;
2236 }
2237 
2238 #define NR_LAYERS 6
2239 
2240 static int vop2_create_crtc(struct vop2 *vop2)
2241 {
2242 	const struct vop2_data *vop2_data = vop2->data;
2243 	struct drm_device *drm = vop2->drm;
2244 	struct device *dev = vop2->dev;
2245 	struct drm_plane *plane;
2246 	struct device_node *port;
2247 	struct vop2_video_port *vp;
2248 	int i, nvp, nvps = 0;
2249 	int ret;
2250 
2251 	for (i = 0; i < vop2_data->nr_vps; i++) {
2252 		const struct vop2_video_port_data *vp_data;
2253 		struct device_node *np;
2254 		char dclk_name[9];
2255 
2256 		vp_data = &vop2_data->vp[i];
2257 		vp = &vop2->vps[i];
2258 		vp->vop2 = vop2;
2259 		vp->id = vp_data->id;
2260 		vp->regs = vp_data->regs;
2261 		vp->data = vp_data;
2262 
2263 		snprintf(dclk_name, sizeof(dclk_name), "dclk_vp%d", vp->id);
2264 		vp->dclk = devm_clk_get(vop2->dev, dclk_name);
2265 		if (IS_ERR(vp->dclk)) {
2266 			drm_err(vop2->drm, "failed to get %s\n", dclk_name);
2267 			return PTR_ERR(vp->dclk);
2268 		}
2269 
2270 		np = of_graph_get_remote_node(dev->of_node, i, -1);
2271 		if (!np) {
2272 			drm_dbg(vop2->drm, "%s: No remote for vp%d\n", __func__, i);
2273 			continue;
2274 		}
2275 		of_node_put(np);
2276 
2277 		port = of_graph_get_port_by_id(dev->of_node, i);
2278 		if (!port) {
2279 			drm_err(vop2->drm, "no port node found for video_port%d\n", i);
2280 			return -ENOENT;
2281 		}
2282 
2283 		vp->crtc.port = port;
2284 		nvps++;
2285 	}
2286 
2287 	nvp = 0;
2288 	for (i = 0; i < vop2->registered_num_wins; i++) {
2289 		struct vop2_win *win = &vop2->win[i];
2290 		u32 possible_crtcs;
2291 
2292 		if (vop2->data->soc_id == 3566) {
2293 			/*
2294 			 * On RK3566 these windows don't have an independent
2295 			 * framebuffer. They share the framebuffer with smart0,
2296 			 * esmart0 and cluster0 respectively.
2297 			 */
2298 			switch (win->data->phys_id) {
2299 			case ROCKCHIP_VOP2_SMART1:
2300 			case ROCKCHIP_VOP2_ESMART1:
2301 			case ROCKCHIP_VOP2_CLUSTER1:
2302 				continue;
2303 			}
2304 		}
2305 
2306 		if (win->type == DRM_PLANE_TYPE_PRIMARY) {
2307 			vp = find_vp_without_primary(vop2);
2308 			if (vp) {
2309 				possible_crtcs = BIT(nvp);
2310 				vp->primary_plane = win;
2311 				nvp++;
2312 			} else {
2313 				/* change the unused primary window to overlay window */
2314 				win->type = DRM_PLANE_TYPE_OVERLAY;
2315 			}
2316 		}
2317 
2318 		if (win->type == DRM_PLANE_TYPE_OVERLAY)
2319 			possible_crtcs = (1 << nvps) - 1;
2320 
2321 		ret = vop2_plane_init(vop2, win, possible_crtcs);
2322 		if (ret) {
2323 			drm_err(vop2->drm, "failed to init plane %s: %d\n",
2324 				win->data->name, ret);
2325 			return ret;
2326 		}
2327 	}
2328 
2329 	for (i = 0; i < vop2_data->nr_vps; i++) {
2330 		vp = &vop2->vps[i];
2331 
2332 		if (!vp->crtc.port)
2333 			continue;
2334 
2335 		plane = &vp->primary_plane->base;
2336 
2337 		ret = drm_crtc_init_with_planes(drm, &vp->crtc, plane, NULL,
2338 						&vop2_crtc_funcs,
2339 						"video_port%d", vp->id);
2340 		if (ret) {
2341 			drm_err(vop2->drm, "crtc init for video_port%d failed\n", i);
2342 			return ret;
2343 		}
2344 
2345 		drm_crtc_helper_add(&vp->crtc, &vop2_crtc_helper_funcs);
2346 
2347 		init_completion(&vp->dsp_hold_completion);
2348 	}
2349 
2350 	/*
2351 	 * On the VOP2 it's very hard to change the number of layers on a VP
2352 	 * during runtime, so we distribute the layers equally over the used
2353 	 * VPs
2354 	 */
2355 	for (i = 0; i < vop2->data->nr_vps; i++) {
2356 		struct vop2_video_port *vp = &vop2->vps[i];
2357 
2358 		if (vp->crtc.port)
2359 			vp->nlayers = NR_LAYERS / nvps;
2360 	}
2361 
2362 	return 0;
2363 }
2364 
2365 static void vop2_destroy_crtc(struct drm_crtc *crtc)
2366 {
2367 	of_node_put(crtc->port);
2368 
2369 	/*
2370 	 * Destroy CRTC after vop2_plane_destroy() since vop2_disable_plane()
2371 	 * references the CRTC.
2372 	 */
2373 	drm_crtc_cleanup(crtc);
2374 }
2375 
2376 static struct reg_field vop2_cluster_regs[VOP2_WIN_MAX_REG] = {
2377 	[VOP2_WIN_ENABLE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 0, 0),
2378 	[VOP2_WIN_FORMAT] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 1, 5),
2379 	[VOP2_WIN_RB_SWAP] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 14, 14),
2380 	[VOP2_WIN_DITHER_UP] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 18, 18),
2381 	[VOP2_WIN_ACT_INFO] = REG_FIELD(RK3568_CLUSTER_WIN_ACT_INFO, 0, 31),
2382 	[VOP2_WIN_DSP_INFO] = REG_FIELD(RK3568_CLUSTER_WIN_DSP_INFO, 0, 31),
2383 	[VOP2_WIN_DSP_ST] = REG_FIELD(RK3568_CLUSTER_WIN_DSP_ST, 0, 31),
2384 	[VOP2_WIN_YRGB_MST] = REG_FIELD(RK3568_CLUSTER_WIN_YRGB_MST, 0, 31),
2385 	[VOP2_WIN_UV_MST] = REG_FIELD(RK3568_CLUSTER_WIN_CBR_MST, 0, 31),
2386 	[VOP2_WIN_YUV_CLIP] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 19, 19),
2387 	[VOP2_WIN_YRGB_VIR] = REG_FIELD(RK3568_CLUSTER_WIN_VIR, 0, 15),
2388 	[VOP2_WIN_UV_VIR] = REG_FIELD(RK3568_CLUSTER_WIN_VIR, 16, 31),
2389 	[VOP2_WIN_Y2R_EN] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 8, 8),
2390 	[VOP2_WIN_R2Y_EN] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 9, 9),
2391 	[VOP2_WIN_CSC_MODE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 10, 11),
2392 
2393 	/* Scale */
2394 	[VOP2_WIN_SCALE_YRGB_X] = REG_FIELD(RK3568_CLUSTER_WIN_SCL_FACTOR_YRGB, 0, 15),
2395 	[VOP2_WIN_SCALE_YRGB_Y] = REG_FIELD(RK3568_CLUSTER_WIN_SCL_FACTOR_YRGB, 16, 31),
2396 	[VOP2_WIN_YRGB_VER_SCL_MODE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 14, 15),
2397 	[VOP2_WIN_YRGB_HOR_SCL_MODE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 12, 13),
2398 	[VOP2_WIN_BIC_COE_SEL] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 2, 3),
2399 	[VOP2_WIN_VSD_YRGB_GT2] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 28, 28),
2400 	[VOP2_WIN_VSD_YRGB_GT4] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 29, 29),
2401 
2402 	/* cluster regs */
2403 	[VOP2_WIN_AFBC_ENABLE] = REG_FIELD(RK3568_CLUSTER_CTRL, 1, 1),
2404 	[VOP2_WIN_CLUSTER_ENABLE] = REG_FIELD(RK3568_CLUSTER_CTRL, 0, 0),
2405 	[VOP2_WIN_CLUSTER_LB_MODE] = REG_FIELD(RK3568_CLUSTER_CTRL, 4, 7),
2406 
2407 	/* afbc regs */
2408 	[VOP2_WIN_AFBC_FORMAT] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 2, 6),
2409 	[VOP2_WIN_AFBC_RB_SWAP] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 9, 9),
2410 	[VOP2_WIN_AFBC_UV_SWAP] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 10, 10),
2411 	[VOP2_WIN_AFBC_AUTO_GATING_EN] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_OUTPUT_CTRL, 4, 4),
2412 	[VOP2_WIN_AFBC_HALF_BLOCK_EN] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 7, 7),
2413 	[VOP2_WIN_AFBC_BLOCK_SPLIT_EN] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 8, 8),
2414 	[VOP2_WIN_AFBC_HDR_PTR] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_HDR_PTR, 0, 31),
2415 	[VOP2_WIN_AFBC_PIC_SIZE] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_PIC_SIZE, 0, 31),
2416 	[VOP2_WIN_AFBC_PIC_VIR_WIDTH] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_VIR_WIDTH, 0, 15),
2417 	[VOP2_WIN_AFBC_TILE_NUM] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_VIR_WIDTH, 16, 31),
2418 	[VOP2_WIN_AFBC_PIC_OFFSET] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_PIC_OFFSET, 0, 31),
2419 	[VOP2_WIN_AFBC_DSP_OFFSET] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_DSP_OFFSET, 0, 31),
2420 	[VOP2_WIN_AFBC_TRANSFORM_OFFSET] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_TRANSFORM_OFFSET, 0, 31),
2421 	[VOP2_WIN_AFBC_ROTATE_90] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 0, 0),
2422 	[VOP2_WIN_AFBC_ROTATE_270] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 1, 1),
2423 	[VOP2_WIN_XMIRROR] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 2, 2),
2424 	[VOP2_WIN_YMIRROR] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 3, 3),
2425 	[VOP2_WIN_UV_SWAP] = { .reg = 0xffffffff },
2426 	[VOP2_WIN_COLOR_KEY] = { .reg = 0xffffffff },
2427 	[VOP2_WIN_COLOR_KEY_EN] = { .reg = 0xffffffff },
2428 	[VOP2_WIN_SCALE_CBCR_X] = { .reg = 0xffffffff },
2429 	[VOP2_WIN_SCALE_CBCR_Y] = { .reg = 0xffffffff },
2430 	[VOP2_WIN_YRGB_HSCL_FILTER_MODE] = { .reg = 0xffffffff },
2431 	[VOP2_WIN_YRGB_VSCL_FILTER_MODE] = { .reg = 0xffffffff },
2432 	[VOP2_WIN_CBCR_VER_SCL_MODE] = { .reg = 0xffffffff },
2433 	[VOP2_WIN_CBCR_HSCL_FILTER_MODE] = { .reg = 0xffffffff },
2434 	[VOP2_WIN_CBCR_HOR_SCL_MODE] = { .reg = 0xffffffff },
2435 	[VOP2_WIN_CBCR_VSCL_FILTER_MODE] = { .reg = 0xffffffff },
2436 	[VOP2_WIN_VSD_CBCR_GT2] = { .reg = 0xffffffff },
2437 	[VOP2_WIN_VSD_CBCR_GT4] = { .reg = 0xffffffff },
2438 };
2439 
2440 static int vop2_cluster_init(struct vop2_win *win)
2441 {
2442 	struct vop2 *vop2 = win->vop2;
2443 	struct reg_field *cluster_regs;
2444 	int ret, i;
2445 
2446 	cluster_regs = kmemdup(vop2_cluster_regs, sizeof(vop2_cluster_regs),
2447 			       GFP_KERNEL);
2448 	if (!cluster_regs)
2449 		return -ENOMEM;
2450 
2451 	for (i = 0; i < ARRAY_SIZE(vop2_cluster_regs); i++)
2452 		if (cluster_regs[i].reg != 0xffffffff)
2453 			cluster_regs[i].reg += win->offset;
2454 
2455 	ret = devm_regmap_field_bulk_alloc(vop2->dev, vop2->map, win->reg,
2456 					   cluster_regs,
2457 					   ARRAY_SIZE(vop2_cluster_regs));
2458 
2459 	kfree(cluster_regs);
2460 
2461 	return ret;
2462 };
2463 
2464 static struct reg_field vop2_esmart_regs[VOP2_WIN_MAX_REG] = {
2465 	[VOP2_WIN_ENABLE] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 0, 0),
2466 	[VOP2_WIN_FORMAT] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 1, 5),
2467 	[VOP2_WIN_DITHER_UP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 12, 12),
2468 	[VOP2_WIN_RB_SWAP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 14, 14),
2469 	[VOP2_WIN_UV_SWAP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 16, 16),
2470 	[VOP2_WIN_ACT_INFO] = REG_FIELD(RK3568_SMART_REGION0_ACT_INFO, 0, 31),
2471 	[VOP2_WIN_DSP_INFO] = REG_FIELD(RK3568_SMART_REGION0_DSP_INFO, 0, 31),
2472 	[VOP2_WIN_DSP_ST] = REG_FIELD(RK3568_SMART_REGION0_DSP_ST, 0, 28),
2473 	[VOP2_WIN_YRGB_MST] = REG_FIELD(RK3568_SMART_REGION0_YRGB_MST, 0, 31),
2474 	[VOP2_WIN_UV_MST] = REG_FIELD(RK3568_SMART_REGION0_CBR_MST, 0, 31),
2475 	[VOP2_WIN_YUV_CLIP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 17, 17),
2476 	[VOP2_WIN_YRGB_VIR] = REG_FIELD(RK3568_SMART_REGION0_VIR, 0, 15),
2477 	[VOP2_WIN_UV_VIR] = REG_FIELD(RK3568_SMART_REGION0_VIR, 16, 31),
2478 	[VOP2_WIN_Y2R_EN] = REG_FIELD(RK3568_SMART_CTRL0, 0, 0),
2479 	[VOP2_WIN_R2Y_EN] = REG_FIELD(RK3568_SMART_CTRL0, 1, 1),
2480 	[VOP2_WIN_CSC_MODE] = REG_FIELD(RK3568_SMART_CTRL0, 2, 3),
2481 	[VOP2_WIN_YMIRROR] = REG_FIELD(RK3568_SMART_CTRL1, 31, 31),
2482 	[VOP2_WIN_COLOR_KEY] = REG_FIELD(RK3568_SMART_COLOR_KEY_CTRL, 0, 29),
2483 	[VOP2_WIN_COLOR_KEY_EN] = REG_FIELD(RK3568_SMART_COLOR_KEY_CTRL, 31, 31),
2484 
2485 	/* Scale */
2486 	[VOP2_WIN_SCALE_YRGB_X] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_YRGB, 0, 15),
2487 	[VOP2_WIN_SCALE_YRGB_Y] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_YRGB, 16, 31),
2488 	[VOP2_WIN_SCALE_CBCR_X] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_CBR, 0, 15),
2489 	[VOP2_WIN_SCALE_CBCR_Y] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_CBR, 16, 31),
2490 	[VOP2_WIN_YRGB_HOR_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 0, 1),
2491 	[VOP2_WIN_YRGB_HSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 2, 3),
2492 	[VOP2_WIN_YRGB_VER_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 4, 5),
2493 	[VOP2_WIN_YRGB_VSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 6, 7),
2494 	[VOP2_WIN_CBCR_HOR_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 8, 9),
2495 	[VOP2_WIN_CBCR_HSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 10, 11),
2496 	[VOP2_WIN_CBCR_VER_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 12, 13),
2497 	[VOP2_WIN_CBCR_VSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 14, 15),
2498 	[VOP2_WIN_BIC_COE_SEL] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 16, 17),
2499 	[VOP2_WIN_VSD_YRGB_GT2] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 8, 8),
2500 	[VOP2_WIN_VSD_YRGB_GT4] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 9, 9),
2501 	[VOP2_WIN_VSD_CBCR_GT2] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 10, 10),
2502 	[VOP2_WIN_VSD_CBCR_GT4] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 11, 11),
2503 	[VOP2_WIN_XMIRROR] = { .reg = 0xffffffff },
2504 	[VOP2_WIN_CLUSTER_ENABLE] = { .reg = 0xffffffff },
2505 	[VOP2_WIN_AFBC_ENABLE] = { .reg = 0xffffffff },
2506 	[VOP2_WIN_CLUSTER_LB_MODE] = { .reg = 0xffffffff },
2507 	[VOP2_WIN_AFBC_FORMAT] = { .reg = 0xffffffff },
2508 	[VOP2_WIN_AFBC_RB_SWAP] = { .reg = 0xffffffff },
2509 	[VOP2_WIN_AFBC_UV_SWAP] = { .reg = 0xffffffff },
2510 	[VOP2_WIN_AFBC_AUTO_GATING_EN] = { .reg = 0xffffffff },
2511 	[VOP2_WIN_AFBC_BLOCK_SPLIT_EN] = { .reg = 0xffffffff },
2512 	[VOP2_WIN_AFBC_PIC_VIR_WIDTH] = { .reg = 0xffffffff },
2513 	[VOP2_WIN_AFBC_TILE_NUM] = { .reg = 0xffffffff },
2514 	[VOP2_WIN_AFBC_PIC_OFFSET] = { .reg = 0xffffffff },
2515 	[VOP2_WIN_AFBC_PIC_SIZE] = { .reg = 0xffffffff },
2516 	[VOP2_WIN_AFBC_DSP_OFFSET] = { .reg = 0xffffffff },
2517 	[VOP2_WIN_AFBC_TRANSFORM_OFFSET] = { .reg = 0xffffffff },
2518 	[VOP2_WIN_AFBC_HDR_PTR] = { .reg = 0xffffffff },
2519 	[VOP2_WIN_AFBC_HALF_BLOCK_EN] = { .reg = 0xffffffff },
2520 	[VOP2_WIN_AFBC_ROTATE_270] = { .reg = 0xffffffff },
2521 	[VOP2_WIN_AFBC_ROTATE_90] = { .reg = 0xffffffff },
2522 };
2523 
2524 static int vop2_esmart_init(struct vop2_win *win)
2525 {
2526 	struct vop2 *vop2 = win->vop2;
2527 	struct reg_field *esmart_regs;
2528 	int ret, i;
2529 
2530 	esmart_regs = kmemdup(vop2_esmart_regs, sizeof(vop2_esmart_regs),
2531 			      GFP_KERNEL);
2532 	if (!esmart_regs)
2533 		return -ENOMEM;
2534 
2535 	for (i = 0; i < ARRAY_SIZE(vop2_esmart_regs); i++)
2536 		if (esmart_regs[i].reg != 0xffffffff)
2537 			esmart_regs[i].reg += win->offset;
2538 
2539 	ret = devm_regmap_field_bulk_alloc(vop2->dev, vop2->map, win->reg,
2540 					   esmart_regs,
2541 					   ARRAY_SIZE(vop2_esmart_regs));
2542 
2543 	kfree(esmart_regs);
2544 
2545 	return ret;
2546 };
2547 
2548 static int vop2_win_init(struct vop2 *vop2)
2549 {
2550 	const struct vop2_data *vop2_data = vop2->data;
2551 	struct vop2_win *win;
2552 	int i, ret;
2553 
2554 	for (i = 0; i < vop2_data->win_size; i++) {
2555 		const struct vop2_win_data *win_data = &vop2_data->win[i];
2556 
2557 		win = &vop2->win[i];
2558 		win->data = win_data;
2559 		win->type = win_data->type;
2560 		win->offset = win_data->base;
2561 		win->win_id = i;
2562 		win->vop2 = vop2;
2563 		if (vop2_cluster_window(win))
2564 			ret = vop2_cluster_init(win);
2565 		else
2566 			ret = vop2_esmart_init(win);
2567 		if (ret)
2568 			return ret;
2569 	}
2570 
2571 	vop2->registered_num_wins = vop2_data->win_size;
2572 
2573 	return 0;
2574 }
2575 
2576 /*
2577  * The window registers are only updated when config done is written.
2578  * Until that they read back the old value. As we read-modify-write
2579  * these registers mark them as non-volatile. This makes sure we read
2580  * the new values from the regmap register cache.
2581  */
2582 static const struct regmap_range vop2_nonvolatile_range[] = {
2583 	regmap_reg_range(0x1000, 0x23ff),
2584 };
2585 
2586 static const struct regmap_access_table vop2_volatile_table = {
2587 	.no_ranges = vop2_nonvolatile_range,
2588 	.n_no_ranges = ARRAY_SIZE(vop2_nonvolatile_range),
2589 };
2590 
2591 static const struct regmap_config vop2_regmap_config = {
2592 	.reg_bits	= 32,
2593 	.val_bits	= 32,
2594 	.reg_stride	= 4,
2595 	.max_register	= 0x3000,
2596 	.name		= "vop2",
2597 	.volatile_table	= &vop2_volatile_table,
2598 	.cache_type	= REGCACHE_RBTREE,
2599 };
2600 
2601 static int vop2_bind(struct device *dev, struct device *master, void *data)
2602 {
2603 	struct platform_device *pdev = to_platform_device(dev);
2604 	const struct vop2_data *vop2_data;
2605 	struct drm_device *drm = data;
2606 	struct vop2 *vop2;
2607 	struct resource *res;
2608 	size_t alloc_size;
2609 	int ret;
2610 
2611 	vop2_data = of_device_get_match_data(dev);
2612 	if (!vop2_data)
2613 		return -ENODEV;
2614 
2615 	/* Allocate vop2 struct and its vop2_win array */
2616 	alloc_size = sizeof(*vop2) + sizeof(*vop2->win) * vop2_data->win_size;
2617 	vop2 = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
2618 	if (!vop2)
2619 		return -ENOMEM;
2620 
2621 	vop2->dev = dev;
2622 	vop2->data = vop2_data;
2623 	vop2->drm = drm;
2624 
2625 	dev_set_drvdata(dev, vop2);
2626 
2627 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vop");
2628 	if (!res) {
2629 		drm_err(vop2->drm, "failed to get vop2 register byname\n");
2630 		return -EINVAL;
2631 	}
2632 
2633 	vop2->regs = devm_ioremap_resource(dev, res);
2634 	if (IS_ERR(vop2->regs))
2635 		return PTR_ERR(vop2->regs);
2636 	vop2->len = resource_size(res);
2637 
2638 	vop2->map = devm_regmap_init_mmio(dev, vop2->regs, &vop2_regmap_config);
2639 
2640 	ret = vop2_win_init(vop2);
2641 	if (ret)
2642 		return ret;
2643 
2644 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gamma-lut");
2645 	if (res) {
2646 		vop2->lut_regs = devm_ioremap_resource(dev, res);
2647 		if (IS_ERR(vop2->lut_regs))
2648 			return PTR_ERR(vop2->lut_regs);
2649 	}
2650 
2651 	vop2->grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
2652 
2653 	vop2->hclk = devm_clk_get(vop2->dev, "hclk");
2654 	if (IS_ERR(vop2->hclk)) {
2655 		drm_err(vop2->drm, "failed to get hclk source\n");
2656 		return PTR_ERR(vop2->hclk);
2657 	}
2658 
2659 	vop2->aclk = devm_clk_get(vop2->dev, "aclk");
2660 	if (IS_ERR(vop2->aclk)) {
2661 		drm_err(vop2->drm, "failed to get aclk source\n");
2662 		return PTR_ERR(vop2->aclk);
2663 	}
2664 
2665 	vop2->irq = platform_get_irq(pdev, 0);
2666 	if (vop2->irq < 0) {
2667 		drm_err(vop2->drm, "cannot find irq for vop2\n");
2668 		return vop2->irq;
2669 	}
2670 
2671 	mutex_init(&vop2->vop2_lock);
2672 
2673 	ret = devm_request_irq(dev, vop2->irq, vop2_isr, IRQF_SHARED, dev_name(dev), vop2);
2674 	if (ret)
2675 		return ret;
2676 
2677 	ret = vop2_create_crtc(vop2);
2678 	if (ret)
2679 		return ret;
2680 
2681 	rockchip_drm_dma_init_device(vop2->drm, vop2->dev);
2682 
2683 	pm_runtime_enable(&pdev->dev);
2684 
2685 	return 0;
2686 }
2687 
2688 static void vop2_unbind(struct device *dev, struct device *master, void *data)
2689 {
2690 	struct vop2 *vop2 = dev_get_drvdata(dev);
2691 	struct drm_device *drm = vop2->drm;
2692 	struct list_head *plane_list = &drm->mode_config.plane_list;
2693 	struct list_head *crtc_list = &drm->mode_config.crtc_list;
2694 	struct drm_crtc *crtc, *tmpc;
2695 	struct drm_plane *plane, *tmpp;
2696 
2697 	pm_runtime_disable(dev);
2698 
2699 	list_for_each_entry_safe(plane, tmpp, plane_list, head)
2700 		drm_plane_cleanup(plane);
2701 
2702 	list_for_each_entry_safe(crtc, tmpc, crtc_list, head)
2703 		vop2_destroy_crtc(crtc);
2704 }
2705 
2706 const struct component_ops vop2_component_ops = {
2707 	.bind = vop2_bind,
2708 	.unbind = vop2_unbind,
2709 };
2710 EXPORT_SYMBOL_GPL(vop2_component_ops);
2711