xref: /linux/drivers/gpu/drm/tidss/tidss_dispc.c (revision face6a3615a649456eb4549f6d474221d877d604)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2016-2018 Texas Instruments Incorporated - https://www.ti.com/
4  * Author: Jyri Sarha <jsarha@ti.com>
5  */
6 
7 #include <linux/bitfield.h>
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/kernel.h>
15 #include <linux/media-bus-format.h>
16 #include <linux/module.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/of.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <linux/sys_soc.h>
23 
24 #include <drm/drm_blend.h>
25 #include <drm/drm_fourcc.h>
26 #include <drm/drm_fb_dma_helper.h>
27 #include <drm/drm_framebuffer.h>
28 #include <drm/drm_gem_dma_helper.h>
29 #include <drm/drm_panel.h>
30 
31 #include "tidss_crtc.h"
32 #include "tidss_dispc.h"
33 #include "tidss_drv.h"
34 #include "tidss_irq.h"
35 #include "tidss_plane.h"
36 
37 #include "tidss_dispc_regs.h"
38 #include "tidss_scale_coefs.h"
39 
40 static const u16 tidss_k2g_common_regs[DISPC_COMMON_REG_TABLE_LEN] = {
41 	[DSS_REVISION_OFF] =                    0x00,
42 	[DSS_SYSCONFIG_OFF] =                   0x04,
43 	[DSS_SYSSTATUS_OFF] =                   0x08,
44 	[DISPC_IRQ_EOI_OFF] =                   0x20,
45 	[DISPC_IRQSTATUS_RAW_OFF] =             0x24,
46 	[DISPC_IRQSTATUS_OFF] =                 0x28,
47 	[DISPC_IRQENABLE_SET_OFF] =             0x2c,
48 	[DISPC_IRQENABLE_CLR_OFF] =             0x30,
49 
50 	[DISPC_GLOBAL_MFLAG_ATTRIBUTE_OFF] =    0x40,
51 	[DISPC_GLOBAL_BUFFER_OFF] =             0x44,
52 
53 	[DISPC_DBG_CONTROL_OFF] =               0x4c,
54 	[DISPC_DBG_STATUS_OFF] =                0x50,
55 
56 	[DISPC_CLKGATING_DISABLE_OFF] =         0x54,
57 };
58 
59 const struct dispc_features dispc_k2g_feats = {
60 	.min_pclk_khz = 4375,
61 
62 	.max_pclk_khz = {
63 		[DISPC_VP_DPI] = 150000,
64 	},
65 
66 	/*
67 	 * XXX According TRM the RGB input buffer width up to 2560 should
68 	 *     work on 3 taps, but in practice it only works up to 1280.
69 	 */
70 	.scaling = {
71 		.in_width_max_5tap_rgb = 1280,
72 		.in_width_max_3tap_rgb = 1280,
73 		.in_width_max_5tap_yuv = 2560,
74 		.in_width_max_3tap_yuv = 2560,
75 		.upscale_limit = 16,
76 		.downscale_limit_5tap = 4,
77 		.downscale_limit_3tap = 2,
78 		/*
79 		 * The max supported pixel inc value is 255. The value
80 		 * of pixel inc is calculated like this: 1+(xinc-1)*bpp.
81 		 * The maximum bpp of all formats supported by the HW
82 		 * is 8. So the maximum supported xinc value is 32,
83 		 * because 1+(32-1)*8 < 255 < 1+(33-1)*4.
84 		 */
85 		.xinc_max = 32,
86 	},
87 
88 	.subrev = DISPC_K2G,
89 
90 	.common = "common",
91 
92 	.common_regs = tidss_k2g_common_regs,
93 
94 	.num_vps = 1,
95 	.vp_name = { "vp1" },
96 	.ovr_name = { "ovr1" },
97 	.vpclk_name =  { "vp1" },
98 	.vp_bus_type = { DISPC_VP_DPI },
99 
100 	.vp_feat = { .color = {
101 			.has_ctm = true,
102 			.gamma_size = 256,
103 			.gamma_type = TIDSS_GAMMA_8BIT,
104 		},
105 	},
106 
107 	.num_vids = 1,
108 
109 	.vid_info = {
110 		{
111 			.name = "vid1",
112 			.is_lite = false,
113 			.hw_id = 0,
114 		},
115 	},
116 
117 	.vid_order = { 0 },
118 };
119 
120 static const u16 tidss_am65x_common_regs[DISPC_COMMON_REG_TABLE_LEN] = {
121 	[DSS_REVISION_OFF] =			0x4,
122 	[DSS_SYSCONFIG_OFF] =			0x8,
123 	[DSS_SYSSTATUS_OFF] =			0x20,
124 	[DISPC_IRQ_EOI_OFF] =			0x24,
125 	[DISPC_IRQSTATUS_RAW_OFF] =		0x28,
126 	[DISPC_IRQSTATUS_OFF] =			0x2c,
127 	[DISPC_IRQENABLE_SET_OFF] =		0x30,
128 	[DISPC_IRQENABLE_CLR_OFF] =		0x40,
129 	[DISPC_VID_IRQENABLE_OFF] =		0x44,
130 	[DISPC_VID_IRQSTATUS_OFF] =		0x58,
131 	[DISPC_VP_IRQENABLE_OFF] =		0x70,
132 	[DISPC_VP_IRQSTATUS_OFF] =		0x7c,
133 
134 	[WB_IRQENABLE_OFF] =			0x88,
135 	[WB_IRQSTATUS_OFF] =			0x8c,
136 
137 	[DISPC_GLOBAL_MFLAG_ATTRIBUTE_OFF] =	0x90,
138 	[DISPC_GLOBAL_OUTPUT_ENABLE_OFF] =	0x94,
139 	[DISPC_GLOBAL_BUFFER_OFF] =		0x98,
140 	[DSS_CBA_CFG_OFF] =			0x9c,
141 	[DISPC_DBG_CONTROL_OFF] =		0xa0,
142 	[DISPC_DBG_STATUS_OFF] =		0xa4,
143 	[DISPC_CLKGATING_DISABLE_OFF] =		0xa8,
144 	[DISPC_SECURE_DISABLE_OFF] =		0xac,
145 };
146 
147 const struct dispc_features dispc_am65x_feats = {
148 	.max_pclk_khz = {
149 		[DISPC_VP_DPI] = 165000,
150 		[DISPC_VP_OLDI_AM65X] = 165000,
151 	},
152 
153 	.scaling = {
154 		.in_width_max_5tap_rgb = 1280,
155 		.in_width_max_3tap_rgb = 2560,
156 		.in_width_max_5tap_yuv = 2560,
157 		.in_width_max_3tap_yuv = 4096,
158 		.upscale_limit = 16,
159 		.downscale_limit_5tap = 4,
160 		.downscale_limit_3tap = 2,
161 		/*
162 		 * The max supported pixel inc value is 255. The value
163 		 * of pixel inc is calculated like this: 1+(xinc-1)*bpp.
164 		 * The maximum bpp of all formats supported by the HW
165 		 * is 8. So the maximum supported xinc value is 32,
166 		 * because 1+(32-1)*8 < 255 < 1+(33-1)*4.
167 		 */
168 		.xinc_max = 32,
169 	},
170 
171 	.subrev = DISPC_AM65X,
172 
173 	.common = "common",
174 	.common_regs = tidss_am65x_common_regs,
175 
176 	.num_vps = 2,
177 	.vp_name = { "vp1", "vp2" },
178 	.ovr_name = { "ovr1", "ovr2" },
179 	.vpclk_name =  { "vp1", "vp2" },
180 	.vp_bus_type = { DISPC_VP_OLDI_AM65X, DISPC_VP_DPI },
181 
182 	.vp_feat = { .color = {
183 			.has_ctm = true,
184 			.gamma_size = 256,
185 			.gamma_type = TIDSS_GAMMA_8BIT,
186 		},
187 	},
188 
189 	.num_vids = 2,
190 	/* note: vid is plane_id 0 and vidl1 is plane_id 1 */
191 	.vid_info = {
192 		{
193 			.name = "vid",
194 			.is_lite = false,
195 			.hw_id = 0,
196 		},
197 		{
198 			.name = "vidl1",
199 			.is_lite = true,
200 			.hw_id = 1,
201 		},
202 	},
203 
204 	.vid_order = {1, 0},
205 };
206 
207 static const u16 tidss_j721e_common_regs[DISPC_COMMON_REG_TABLE_LEN] = {
208 	[DSS_REVISION_OFF] =			0x4,
209 	[DSS_SYSCONFIG_OFF] =			0x8,
210 	[DSS_SYSSTATUS_OFF] =			0x20,
211 	[DISPC_IRQ_EOI_OFF] =			0x80,
212 	[DISPC_IRQSTATUS_RAW_OFF] =		0x28,
213 	[DISPC_IRQSTATUS_OFF] =			0x2c,
214 	[DISPC_IRQENABLE_SET_OFF] =		0x30,
215 	[DISPC_IRQENABLE_CLR_OFF] =		0x34,
216 	[DISPC_VID_IRQENABLE_OFF] =		0x38,
217 	[DISPC_VID_IRQSTATUS_OFF] =		0x48,
218 	[DISPC_VP_IRQENABLE_OFF] =		0x58,
219 	[DISPC_VP_IRQSTATUS_OFF] =		0x68,
220 
221 	[WB_IRQENABLE_OFF] =			0x78,
222 	[WB_IRQSTATUS_OFF] =			0x7c,
223 
224 	[DISPC_GLOBAL_MFLAG_ATTRIBUTE_OFF] =	0x98,
225 	[DISPC_GLOBAL_OUTPUT_ENABLE_OFF] =	0x9c,
226 	[DISPC_GLOBAL_BUFFER_OFF] =		0xa0,
227 	[DSS_CBA_CFG_OFF] =			0xa4,
228 	[DISPC_DBG_CONTROL_OFF] =		0xa8,
229 	[DISPC_DBG_STATUS_OFF] =		0xac,
230 	[DISPC_CLKGATING_DISABLE_OFF] =		0xb0,
231 	[DISPC_SECURE_DISABLE_OFF] =		0x90,
232 
233 	[FBDC_REVISION_1_OFF] =			0xb8,
234 	[FBDC_REVISION_2_OFF] =			0xbc,
235 	[FBDC_REVISION_3_OFF] =			0xc0,
236 	[FBDC_REVISION_4_OFF] =			0xc4,
237 	[FBDC_REVISION_5_OFF] =			0xc8,
238 	[FBDC_REVISION_6_OFF] =			0xcc,
239 	[FBDC_COMMON_CONTROL_OFF] =		0xd0,
240 	[FBDC_CONSTANT_COLOR_0_OFF] =		0xd4,
241 	[FBDC_CONSTANT_COLOR_1_OFF] =		0xd8,
242 	[DISPC_CONNECTIONS_OFF] =		0xe4,
243 	[DISPC_MSS_VP1_OFF] =			0xe8,
244 	[DISPC_MSS_VP3_OFF] =			0xec,
245 };
246 
247 const struct dispc_features dispc_j721e_feats = {
248 	.max_pclk_khz = {
249 		[DISPC_VP_DPI] = 170000,
250 		[DISPC_VP_INTERNAL] = 600000,
251 	},
252 
253 	.scaling = {
254 		.in_width_max_5tap_rgb = 2048,
255 		.in_width_max_3tap_rgb = 4096,
256 		.in_width_max_5tap_yuv = 4096,
257 		.in_width_max_3tap_yuv = 4096,
258 		.upscale_limit = 16,
259 		.downscale_limit_5tap = 4,
260 		.downscale_limit_3tap = 2,
261 		/*
262 		 * The max supported pixel inc value is 255. The value
263 		 * of pixel inc is calculated like this: 1+(xinc-1)*bpp.
264 		 * The maximum bpp of all formats supported by the HW
265 		 * is 8. So the maximum supported xinc value is 32,
266 		 * because 1+(32-1)*8 < 255 < 1+(33-1)*4.
267 		 */
268 		.xinc_max = 32,
269 	},
270 
271 	.subrev = DISPC_J721E,
272 
273 	.common = "common_m",
274 	.common_regs = tidss_j721e_common_regs,
275 
276 	.num_vps = 4,
277 	.vp_name = { "vp1", "vp2", "vp3", "vp4" },
278 	.ovr_name = { "ovr1", "ovr2", "ovr3", "ovr4" },
279 	.vpclk_name = { "vp1", "vp2", "vp3", "vp4" },
280 	/* Currently hard coded VP routing (see dispc_initial_config()) */
281 	.vp_bus_type =	{ DISPC_VP_INTERNAL, DISPC_VP_DPI,
282 			  DISPC_VP_INTERNAL, DISPC_VP_DPI, },
283 	.vp_feat = { .color = {
284 			.has_ctm = true,
285 			.gamma_size = 1024,
286 			.gamma_type = TIDSS_GAMMA_10BIT,
287 		},
288 	},
289 
290 	.num_vids = 4,
291 
292 	.vid_info = {
293 		{
294 			.name = "vid1",
295 			.is_lite = false,
296 			.hw_id = 0,
297 		},
298 		{
299 			.name = "vidl1",
300 			.is_lite = true,
301 			.hw_id = 1,
302 		},
303 		{
304 			.name = "vid2",
305 			.is_lite = false,
306 			.hw_id = 2,
307 		},
308 		{
309 			.name = "vidl2",
310 			.is_lite = true,
311 			.hw_id = 3,
312 		},
313 	},
314 
315 	.vid_order = { 1, 3, 0, 2 },
316 };
317 
318 const struct dispc_features dispc_am625_feats = {
319 	.max_pclk_khz = {
320 		[DISPC_VP_DPI] = 165000,
321 		[DISPC_VP_INTERNAL] = 170000,
322 	},
323 
324 	.scaling = {
325 		.in_width_max_5tap_rgb = 1280,
326 		.in_width_max_3tap_rgb = 2560,
327 		.in_width_max_5tap_yuv = 2560,
328 		.in_width_max_3tap_yuv = 4096,
329 		.upscale_limit = 16,
330 		.downscale_limit_5tap = 4,
331 		.downscale_limit_3tap = 2,
332 		/*
333 		 * The max supported pixel inc value is 255. The value
334 		 * of pixel inc is calculated like this: 1+(xinc-1)*bpp.
335 		 * The maximum bpp of all formats supported by the HW
336 		 * is 8. So the maximum supported xinc value is 32,
337 		 * because 1+(32-1)*8 < 255 < 1+(33-1)*4.
338 		 */
339 		.xinc_max = 32,
340 	},
341 
342 	.subrev = DISPC_AM625,
343 
344 	.common = "common",
345 	.common_regs = tidss_am65x_common_regs,
346 
347 	.num_vps = 2,
348 	.vp_name = { "vp1", "vp2" },
349 	.ovr_name = { "ovr1", "ovr2" },
350 	.vpclk_name =  { "vp1", "vp2" },
351 	.vp_bus_type = { DISPC_VP_INTERNAL, DISPC_VP_DPI },
352 
353 	.vp_feat = { .color = {
354 			.has_ctm = true,
355 			.gamma_size = 256,
356 			.gamma_type = TIDSS_GAMMA_8BIT,
357 		},
358 	},
359 
360 	.num_vids = 2,
361 
362 	/* note: vid is plane_id 0 and vidl1 is plane_id 1 */
363 	.vid_info = {
364 		{
365 			.name = "vid",
366 			.is_lite = false,
367 			.hw_id = 0,
368 		},
369 		{
370 			.name = "vidl1",
371 			.is_lite = true,
372 			.hw_id = 1,
373 		}
374 	},
375 
376 	.vid_order = {1, 0},
377 };
378 
379 const struct dispc_features dispc_am62a7_feats = {
380 	/*
381 	 * if the code reaches dispc_mode_valid with VP1,
382 	 * it should return MODE_BAD.
383 	 */
384 	.max_pclk_khz = {
385 		[DISPC_VP_TIED_OFF] = 0,
386 		[DISPC_VP_DPI] = 165000,
387 	},
388 
389 	.scaling = {
390 		.in_width_max_5tap_rgb = 1280,
391 		.in_width_max_3tap_rgb = 2560,
392 		.in_width_max_5tap_yuv = 2560,
393 		.in_width_max_3tap_yuv = 4096,
394 		.upscale_limit = 16,
395 		.downscale_limit_5tap = 4,
396 		.downscale_limit_3tap = 2,
397 		/*
398 		 * The max supported pixel inc value is 255. The value
399 		 * of pixel inc is calculated like this: 1+(xinc-1)*bpp.
400 		 * The maximum bpp of all formats supported by the HW
401 		 * is 8. So the maximum supported xinc value is 32,
402 		 * because 1+(32-1)*8 < 255 < 1+(33-1)*4.
403 		 */
404 		.xinc_max = 32,
405 	},
406 
407 	.subrev = DISPC_AM62A7,
408 
409 	.common = "common",
410 	.common_regs = tidss_am65x_common_regs,
411 
412 	.num_vps = 2,
413 	.vp_name = { "vp1", "vp2" },
414 	.ovr_name = { "ovr1", "ovr2" },
415 	.vpclk_name =  { "vp1", "vp2" },
416 	/* VP1 of the DSS in AM62A7 SoC is tied off internally */
417 	.vp_bus_type = { DISPC_VP_TIED_OFF, DISPC_VP_DPI },
418 
419 	.vp_feat = { .color = {
420 			.has_ctm = true,
421 			.gamma_size = 256,
422 			.gamma_type = TIDSS_GAMMA_8BIT,
423 		},
424 	},
425 
426 	.num_vids = 2,
427 
428 	.vid_info = {
429 		{
430 			.name = "vid",
431 			.is_lite = false,
432 			.hw_id = 0,
433 		},
434 		{
435 			.name = "vidl1",
436 			.is_lite = true,
437 			.hw_id = 1,
438 		}
439 	},
440 
441 	.vid_order = {1, 0},
442 };
443 
444 const struct dispc_features dispc_am62l_feats = {
445 	.max_pclk_khz = {
446 		[DISPC_VP_DPI] = 165000,
447 	},
448 
449 	.subrev = DISPC_AM62L,
450 
451 	.common = "common",
452 	.common_regs = tidss_am65x_common_regs,
453 
454 	.num_vps = 1,
455 	.vp_name = { "vp1" },
456 	.ovr_name = { "ovr1" },
457 	.vpclk_name =  { "vp1" },
458 	.vp_bus_type = { DISPC_VP_DPI },
459 
460 	.vp_feat = { .color = {
461 			.has_ctm = true,
462 			.gamma_size = 256,
463 			.gamma_type = TIDSS_GAMMA_8BIT,
464 		},
465 	},
466 
467 	.num_vids = 1,
468 
469 	.vid_info = {
470 		{
471 			.name = "vidl1",
472 			.is_lite = true,
473 			.hw_id = 1,
474 		}
475 	},
476 
477 	.vid_order = {0},
478 };
479 
480 static const u16 *dispc_common_regmap;
481 
482 struct dss_vp_data {
483 	u32 *gamma_table;
484 };
485 
486 struct dispc_device {
487 	struct tidss_device *tidss;
488 	struct device *dev;
489 
490 	void __iomem *base_common;
491 	void __iomem *base_vid[TIDSS_MAX_PLANES];
492 	void __iomem *base_ovr[TIDSS_MAX_PORTS];
493 	void __iomem *base_vp[TIDSS_MAX_PORTS];
494 
495 	struct regmap *am65x_oldi_io_ctrl;
496 
497 	struct clk *vp_clk[TIDSS_MAX_PORTS];
498 
499 	const struct dispc_features *feat;
500 
501 	struct clk *fclk;
502 
503 	bool is_enabled;
504 
505 	struct dss_vp_data vp_data[TIDSS_MAX_PORTS];
506 
507 	u32 *fourccs;
508 	u32 num_fourccs;
509 
510 	u32 memory_bandwidth_limit;
511 
512 	struct dispc_errata errata;
513 };
514 
515 static void dispc_write(struct dispc_device *dispc, u16 reg, u32 val)
516 {
517 	iowrite32(val, dispc->base_common + reg);
518 }
519 
520 static u32 dispc_read(struct dispc_device *dispc, u16 reg)
521 {
522 	return ioread32(dispc->base_common + reg);
523 }
524 
525 static
526 void dispc_vid_write(struct dispc_device *dispc, u32 hw_plane, u16 reg, u32 val)
527 {
528 	void __iomem *base = dispc->base_vid[hw_plane];
529 
530 	iowrite32(val, base + reg);
531 }
532 
533 static u32 dispc_vid_read(struct dispc_device *dispc, u32 hw_plane, u16 reg)
534 {
535 	void __iomem *base = dispc->base_vid[hw_plane];
536 
537 	return ioread32(base + reg);
538 }
539 
540 static void dispc_ovr_write(struct dispc_device *dispc, u32 hw_videoport,
541 			    u16 reg, u32 val)
542 {
543 	void __iomem *base = dispc->base_ovr[hw_videoport];
544 
545 	iowrite32(val, base + reg);
546 }
547 
548 static u32 dispc_ovr_read(struct dispc_device *dispc, u32 hw_videoport, u16 reg)
549 {
550 	void __iomem *base = dispc->base_ovr[hw_videoport];
551 
552 	return ioread32(base + reg);
553 }
554 
555 static void dispc_vp_write(struct dispc_device *dispc, u32 hw_videoport,
556 			   u16 reg, u32 val)
557 {
558 	void __iomem *base = dispc->base_vp[hw_videoport];
559 
560 	iowrite32(val, base + reg);
561 }
562 
563 static u32 dispc_vp_read(struct dispc_device *dispc, u32 hw_videoport, u16 reg)
564 {
565 	void __iomem *base = dispc->base_vp[hw_videoport];
566 
567 	return ioread32(base + reg);
568 }
569 
570 int tidss_configure_oldi(struct tidss_device *tidss, u32 hw_videoport,
571 			 u32 oldi_cfg)
572 {
573 	u32 count = 0;
574 	u32 oldi_reset_bit = BIT(5 + hw_videoport);
575 
576 	dispc_vp_write(tidss->dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG, oldi_cfg);
577 
578 	while (!(oldi_reset_bit & dispc_read(tidss->dispc, DSS_SYSSTATUS)) &&
579 	       count < 10000)
580 		count++;
581 
582 	if (!(oldi_reset_bit & dispc_read(tidss->dispc, DSS_SYSSTATUS)))
583 		return -ETIMEDOUT;
584 
585 	return 0;
586 }
587 
588 void tidss_disable_oldi(struct tidss_device *tidss, u32 hw_videoport)
589 {
590 	dispc_vp_write(tidss->dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG, 0);
591 }
592 
593 /*
594  * TRM gives bitfields as start:end, where start is the higher bit
595  * number. For example 7:0
596  */
597 
598 #define REG_GET(dispc, idx, mask)					\
599 	((u32)FIELD_GET((mask), dispc_read((dispc), (idx))))
600 
601 #define REG_FLD_MOD(dispc, idx, val, mask)				\
602 	({								\
603 		struct dispc_device *_dispc = (dispc);			\
604 		u32 _idx = (idx);					\
605 		u32 _reg = dispc_read(_dispc, _idx);			\
606 		FIELD_MODIFY((mask), &_reg, (val));			\
607 		dispc_write(_dispc, _idx, _reg);			\
608 	})
609 
610 #define VID_REG_GET(dispc, hw_plane, idx, mask)				\
611 	((u32)FIELD_GET((mask), dispc_vid_read((dispc), (hw_plane), (idx))))
612 
613 #define VID_REG_FLD_MOD(dispc, hw_plane, idx, val, mask)		\
614 	({								\
615 		struct dispc_device *_dispc = (dispc);			\
616 		u32 _hw_plane = (hw_plane);				\
617 		u32 _idx = (idx);					\
618 		u32 _reg = dispc_vid_read(_dispc, _hw_plane, _idx);	\
619 		FIELD_MODIFY((mask), &_reg, (val));			\
620 		dispc_vid_write(_dispc, _hw_plane, _idx, _reg);		\
621 	})
622 
623 #define VP_REG_GET(dispc, vp, idx, mask)				\
624 	((u32)FIELD_GET((mask), dispc_vp_read((dispc), (vp), (idx))))
625 
626 #define VP_REG_FLD_MOD(dispc, vp, idx, val, mask)			\
627 	({								\
628 		struct dispc_device *_dispc = (dispc);			\
629 		u32 _vp = (vp);						\
630 		u32 _idx = (idx);					\
631 		u32 _reg = dispc_vp_read(_dispc, _vp, _idx);		\
632 		FIELD_MODIFY((mask), &_reg, (val));			\
633 		dispc_vp_write(_dispc, _vp, _idx, _reg);		\
634 	})
635 
636 #define OVR_REG_FLD_MOD(dispc, ovr, idx, val, mask)			\
637 	({								\
638 		struct dispc_device *_dispc = (dispc);			\
639 		u32 _ovr = (ovr);					\
640 		u32 _idx = (idx);					\
641 		u32 _reg = dispc_ovr_read(_dispc, _ovr, _idx);		\
642 		FIELD_MODIFY((mask), &_reg, (val));			\
643 		dispc_ovr_write(_dispc, _ovr, _idx, _reg);		\
644 	})
645 
646 static dispc_irq_t dispc_vp_irq_from_raw(u32 stat, u32 hw_videoport)
647 {
648 	dispc_irq_t vp_stat = 0;
649 
650 	if (stat & BIT(0))
651 		vp_stat |= DSS_IRQ_VP_FRAME_DONE(hw_videoport);
652 	if (stat & BIT(1))
653 		vp_stat |= DSS_IRQ_VP_VSYNC_EVEN(hw_videoport);
654 	if (stat & BIT(2))
655 		vp_stat |= DSS_IRQ_VP_VSYNC_ODD(hw_videoport);
656 	if (stat & BIT(4))
657 		vp_stat |= DSS_IRQ_VP_SYNC_LOST(hw_videoport);
658 
659 	return vp_stat;
660 }
661 
662 static u32 dispc_vp_irq_to_raw(dispc_irq_t vpstat, u32 hw_videoport)
663 {
664 	u32 stat = 0;
665 
666 	if (vpstat & DSS_IRQ_VP_FRAME_DONE(hw_videoport))
667 		stat |= BIT(0);
668 	if (vpstat & DSS_IRQ_VP_VSYNC_EVEN(hw_videoport))
669 		stat |= BIT(1);
670 	if (vpstat & DSS_IRQ_VP_VSYNC_ODD(hw_videoport))
671 		stat |= BIT(2);
672 	if (vpstat & DSS_IRQ_VP_SYNC_LOST(hw_videoport))
673 		stat |= BIT(4);
674 
675 	return stat;
676 }
677 
678 static dispc_irq_t dispc_vid_irq_from_raw(u32 stat, u32 hw_plane)
679 {
680 	dispc_irq_t vid_stat = 0;
681 
682 	if (stat & BIT(0))
683 		vid_stat |= DSS_IRQ_PLANE_FIFO_UNDERFLOW(hw_plane);
684 
685 	return vid_stat;
686 }
687 
688 static u32 dispc_vid_irq_to_raw(dispc_irq_t vidstat, u32 hw_plane)
689 {
690 	u32 stat = 0;
691 
692 	if (vidstat & DSS_IRQ_PLANE_FIFO_UNDERFLOW(hw_plane))
693 		stat |= BIT(0);
694 
695 	return stat;
696 }
697 
698 static dispc_irq_t dispc_k2g_vp_read_irqstatus(struct dispc_device *dispc,
699 					       u32 hw_videoport)
700 {
701 	u32 stat = dispc_vp_read(dispc, hw_videoport, DISPC_VP_K2G_IRQSTATUS);
702 
703 	return dispc_vp_irq_from_raw(stat, hw_videoport);
704 }
705 
706 static void dispc_k2g_vp_write_irqstatus(struct dispc_device *dispc,
707 					 u32 hw_videoport, dispc_irq_t vpstat)
708 {
709 	u32 stat = dispc_vp_irq_to_raw(vpstat, hw_videoport);
710 
711 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_K2G_IRQSTATUS, stat);
712 }
713 
714 static dispc_irq_t dispc_k2g_vid_read_irqstatus(struct dispc_device *dispc,
715 						u32 hw_plane)
716 {
717 	u32 stat = dispc_vid_read(dispc, hw_plane, DISPC_VID_K2G_IRQSTATUS);
718 
719 	return dispc_vid_irq_from_raw(stat, hw_plane);
720 }
721 
722 static void dispc_k2g_vid_write_irqstatus(struct dispc_device *dispc,
723 					  u32 hw_plane, dispc_irq_t vidstat)
724 {
725 	u32 stat = dispc_vid_irq_to_raw(vidstat, hw_plane);
726 
727 	dispc_vid_write(dispc, hw_plane, DISPC_VID_K2G_IRQSTATUS, stat);
728 }
729 
730 static dispc_irq_t dispc_k2g_vp_read_irqenable(struct dispc_device *dispc,
731 					       u32 hw_videoport)
732 {
733 	u32 stat = dispc_vp_read(dispc, hw_videoport, DISPC_VP_K2G_IRQENABLE);
734 
735 	return dispc_vp_irq_from_raw(stat, hw_videoport);
736 }
737 
738 static void dispc_k2g_vp_set_irqenable(struct dispc_device *dispc,
739 				       u32 hw_videoport, dispc_irq_t vpstat)
740 {
741 	u32 stat = dispc_vp_irq_to_raw(vpstat, hw_videoport);
742 
743 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_K2G_IRQENABLE, stat);
744 }
745 
746 static dispc_irq_t dispc_k2g_vid_read_irqenable(struct dispc_device *dispc,
747 						u32 hw_plane)
748 {
749 	u32 stat = dispc_vid_read(dispc, hw_plane, DISPC_VID_K2G_IRQENABLE);
750 
751 	return dispc_vid_irq_from_raw(stat, hw_plane);
752 }
753 
754 static void dispc_k2g_vid_set_irqenable(struct dispc_device *dispc,
755 					u32 hw_plane, dispc_irq_t vidstat)
756 {
757 	u32 stat = dispc_vid_irq_to_raw(vidstat, hw_plane);
758 
759 	dispc_vid_write(dispc, hw_plane, DISPC_VID_K2G_IRQENABLE, stat);
760 }
761 
762 static void dispc_k2g_clear_irqstatus(struct dispc_device *dispc,
763 				      dispc_irq_t mask)
764 {
765 	dispc_k2g_vp_write_irqstatus(dispc, 0, mask);
766 	dispc_k2g_vid_write_irqstatus(dispc, 0, mask);
767 }
768 
769 static
770 dispc_irq_t dispc_k2g_read_and_clear_irqstatus(struct dispc_device *dispc)
771 {
772 	dispc_irq_t stat = 0;
773 
774 	/* always clear the top level irqstatus */
775 	dispc_write(dispc, DISPC_IRQSTATUS,
776 		    dispc_read(dispc, DISPC_IRQSTATUS));
777 
778 	stat |= dispc_k2g_vp_read_irqstatus(dispc, 0);
779 	stat |= dispc_k2g_vid_read_irqstatus(dispc, 0);
780 
781 	dispc_k2g_clear_irqstatus(dispc, stat);
782 
783 	return stat;
784 }
785 
786 static dispc_irq_t dispc_k2g_read_irqenable(struct dispc_device *dispc)
787 {
788 	dispc_irq_t stat = 0;
789 
790 	stat |= dispc_k2g_vp_read_irqenable(dispc, 0);
791 	stat |= dispc_k2g_vid_read_irqenable(dispc, 0);
792 
793 	return stat;
794 }
795 
796 static
797 void dispc_k2g_set_irqenable(struct dispc_device *dispc, dispc_irq_t mask)
798 {
799 	dispc_irq_t old_mask = dispc_k2g_read_irqenable(dispc);
800 
801 	/* clear the irqstatus for irqs that will be enabled */
802 	dispc_k2g_clear_irqstatus(dispc, (mask ^ old_mask) & mask);
803 
804 	dispc_k2g_vp_set_irqenable(dispc, 0, mask);
805 	dispc_k2g_vid_set_irqenable(dispc, 0, mask);
806 
807 	dispc_write(dispc, DISPC_IRQENABLE_SET, (1 << 0) | (1 << 7));
808 
809 	/* clear the irqstatus for irqs that were disabled */
810 	dispc_k2g_clear_irqstatus(dispc, (mask ^ old_mask) & old_mask);
811 
812 	/* flush posted write */
813 	dispc_k2g_read_irqenable(dispc);
814 }
815 
816 static dispc_irq_t dispc_k3_vp_read_irqstatus(struct dispc_device *dispc,
817 					      u32 hw_videoport)
818 {
819 	u32 stat = dispc_read(dispc, DISPC_VP_IRQSTATUS(hw_videoport));
820 
821 	return dispc_vp_irq_from_raw(stat, hw_videoport);
822 }
823 
824 static void dispc_k3_vp_write_irqstatus(struct dispc_device *dispc,
825 					u32 hw_videoport, dispc_irq_t vpstat)
826 {
827 	u32 stat = dispc_vp_irq_to_raw(vpstat, hw_videoport);
828 
829 	dispc_write(dispc, DISPC_VP_IRQSTATUS(hw_videoport), stat);
830 }
831 
832 static dispc_irq_t dispc_k3_vid_read_irqstatus(struct dispc_device *dispc,
833 					       u32 hw_plane)
834 {
835 	u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
836 	u32 stat = dispc_read(dispc, DISPC_VID_IRQSTATUS(hw_id));
837 
838 	return dispc_vid_irq_from_raw(stat, hw_plane);
839 }
840 
841 static void dispc_k3_vid_write_irqstatus(struct dispc_device *dispc,
842 					 u32 hw_plane, dispc_irq_t vidstat)
843 {
844 	u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
845 	u32 stat = dispc_vid_irq_to_raw(vidstat, hw_plane);
846 
847 	dispc_write(dispc, DISPC_VID_IRQSTATUS(hw_id), stat);
848 }
849 
850 static dispc_irq_t dispc_k3_vp_read_irqenable(struct dispc_device *dispc,
851 					      u32 hw_videoport)
852 {
853 	u32 stat = dispc_read(dispc, DISPC_VP_IRQENABLE(hw_videoport));
854 
855 	return dispc_vp_irq_from_raw(stat, hw_videoport);
856 }
857 
858 static void dispc_k3_vp_set_irqenable(struct dispc_device *dispc,
859 				      u32 hw_videoport, dispc_irq_t vpstat)
860 {
861 	u32 stat = dispc_vp_irq_to_raw(vpstat, hw_videoport);
862 
863 	dispc_write(dispc, DISPC_VP_IRQENABLE(hw_videoport), stat);
864 }
865 
866 static dispc_irq_t dispc_k3_vid_read_irqenable(struct dispc_device *dispc,
867 					       u32 hw_plane)
868 {
869 	u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
870 	u32 stat = dispc_read(dispc, DISPC_VID_IRQENABLE(hw_id));
871 
872 	return dispc_vid_irq_from_raw(stat, hw_plane);
873 }
874 
875 static void dispc_k3_vid_set_irqenable(struct dispc_device *dispc,
876 				       u32 hw_plane, dispc_irq_t vidstat)
877 {
878 	u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
879 	u32 stat = dispc_vid_irq_to_raw(vidstat, hw_plane);
880 
881 	dispc_write(dispc, DISPC_VID_IRQENABLE(hw_id), stat);
882 }
883 
884 static
885 void dispc_k3_clear_irqstatus(struct dispc_device *dispc, dispc_irq_t clearmask)
886 {
887 	unsigned int i;
888 
889 	for (i = 0; i < dispc->feat->num_vps; ++i) {
890 		if (clearmask & DSS_IRQ_VP_MASK(i))
891 			dispc_k3_vp_write_irqstatus(dispc, i, clearmask);
892 	}
893 
894 	for (i = 0; i < dispc->feat->num_vids; ++i) {
895 		if (clearmask & DSS_IRQ_PLANE_MASK(i))
896 			dispc_k3_vid_write_irqstatus(dispc, i, clearmask);
897 	}
898 
899 	/* always clear the top level irqstatus */
900 	dispc_write(dispc, DISPC_IRQSTATUS, dispc_read(dispc, DISPC_IRQSTATUS));
901 
902 	/* Flush posted writes */
903 	dispc_read(dispc, DISPC_IRQSTATUS);
904 }
905 
906 static
907 dispc_irq_t dispc_k3_read_and_clear_irqstatus(struct dispc_device *dispc)
908 {
909 	dispc_irq_t status = 0;
910 	unsigned int i;
911 
912 	for (i = 0; i < dispc->feat->num_vps; ++i)
913 		status |= dispc_k3_vp_read_irqstatus(dispc, i);
914 
915 	for (i = 0; i < dispc->feat->num_vids; ++i)
916 		status |= dispc_k3_vid_read_irqstatus(dispc, i);
917 
918 	dispc_k3_clear_irqstatus(dispc, status);
919 
920 	return status;
921 }
922 
923 static dispc_irq_t dispc_k3_read_irqenable(struct dispc_device *dispc)
924 {
925 	dispc_irq_t enable = 0;
926 	unsigned int i;
927 
928 	for (i = 0; i < dispc->feat->num_vps; ++i)
929 		enable |= dispc_k3_vp_read_irqenable(dispc, i);
930 
931 	for (i = 0; i < dispc->feat->num_vids; ++i)
932 		enable |= dispc_k3_vid_read_irqenable(dispc, i);
933 
934 	return enable;
935 }
936 
937 static void dispc_k3_set_irqenable(struct dispc_device *dispc,
938 				   dispc_irq_t mask)
939 {
940 	unsigned int i;
941 	u32 main_enable = 0, main_disable = 0;
942 	dispc_irq_t old_mask;
943 
944 	old_mask = dispc_k3_read_irqenable(dispc);
945 
946 	/* clear the irqstatus for irqs that will be enabled */
947 	dispc_k3_clear_irqstatus(dispc, (old_mask ^ mask) & mask);
948 
949 	for (i = 0; i < dispc->feat->num_vps; ++i) {
950 		dispc_k3_vp_set_irqenable(dispc, i, mask);
951 		if (mask & DSS_IRQ_VP_MASK(i))
952 			main_enable |= BIT(i);		/* VP IRQ */
953 		else
954 			main_disable |= BIT(i);		/* VP IRQ */
955 	}
956 
957 	for (i = 0; i < dispc->feat->num_vids; ++i) {
958 		u32 hw_id = dispc->feat->vid_info[i].hw_id;
959 
960 		dispc_k3_vid_set_irqenable(dispc, i, mask);
961 
962 		if (mask & DSS_IRQ_PLANE_MASK(i))
963 			main_enable |= BIT(hw_id + 4);	/* VID IRQ */
964 		else
965 			main_disable |= BIT(hw_id + 4);	/* VID IRQ */
966 	}
967 
968 	if (main_enable)
969 		dispc_write(dispc, DISPC_IRQENABLE_SET, main_enable);
970 
971 	if (main_disable)
972 		dispc_write(dispc, DISPC_IRQENABLE_CLR, main_disable);
973 
974 	/* clear the irqstatus for irqs that were disabled */
975 	dispc_k3_clear_irqstatus(dispc, (old_mask ^ mask) & old_mask);
976 
977 	/* Flush posted writes */
978 	dispc_read(dispc, DISPC_IRQENABLE_SET);
979 }
980 
981 dispc_irq_t dispc_read_and_clear_irqstatus(struct dispc_device *dispc)
982 {
983 	switch (dispc->feat->subrev) {
984 	case DISPC_K2G:
985 		return dispc_k2g_read_and_clear_irqstatus(dispc);
986 	case DISPC_AM625:
987 	case DISPC_AM62A7:
988 	case DISPC_AM62L:
989 	case DISPC_AM65X:
990 	case DISPC_J721E:
991 		return dispc_k3_read_and_clear_irqstatus(dispc);
992 	default:
993 		WARN_ON(1);
994 		return 0;
995 	}
996 }
997 
998 void dispc_set_irqenable(struct dispc_device *dispc, dispc_irq_t mask)
999 {
1000 	switch (dispc->feat->subrev) {
1001 	case DISPC_K2G:
1002 		dispc_k2g_set_irqenable(dispc, mask);
1003 		break;
1004 	case DISPC_AM625:
1005 	case DISPC_AM62A7:
1006 	case DISPC_AM62L:
1007 	case DISPC_AM65X:
1008 	case DISPC_J721E:
1009 		dispc_k3_set_irqenable(dispc, mask);
1010 		break;
1011 	default:
1012 		WARN_ON(1);
1013 		break;
1014 	}
1015 }
1016 
1017 struct dispc_bus_format {
1018 	u32 bus_fmt;
1019 	u32 data_width;
1020 	bool is_oldi_fmt;
1021 	enum oldi_mode_reg_val am65x_oldi_mode_reg_val;
1022 };
1023 
1024 static const struct dispc_bus_format dispc_bus_formats[] = {
1025 	{ MEDIA_BUS_FMT_RGB444_1X12,		12, false, 0 },
1026 	{ MEDIA_BUS_FMT_RGB565_1X16,		16, false, 0 },
1027 	{ MEDIA_BUS_FMT_RGB666_1X18,		18, false, 0 },
1028 	{ MEDIA_BUS_FMT_RGB888_1X24,		24, false, 0 },
1029 	{ MEDIA_BUS_FMT_RGB101010_1X30,		30, false, 0 },
1030 	{ MEDIA_BUS_FMT_RGB121212_1X36,		36, false, 0 },
1031 	{ MEDIA_BUS_FMT_RGB666_1X7X3_SPWG,	18, true, SPWG_18 },
1032 	{ MEDIA_BUS_FMT_RGB888_1X7X4_SPWG,	24, true, SPWG_24 },
1033 	{ MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA,	24, true, JEIDA_24 },
1034 };
1035 
1036 static const
1037 struct dispc_bus_format *dispc_vp_find_bus_fmt(struct dispc_device *dispc,
1038 					       u32 hw_videoport,
1039 					       u32 bus_fmt, u32 bus_flags)
1040 {
1041 	unsigned int i;
1042 
1043 	for (i = 0; i < ARRAY_SIZE(dispc_bus_formats); ++i) {
1044 		if (dispc_bus_formats[i].bus_fmt == bus_fmt)
1045 			return &dispc_bus_formats[i];
1046 	}
1047 
1048 	return NULL;
1049 }
1050 
1051 int dispc_vp_bus_check(struct dispc_device *dispc, u32 hw_videoport,
1052 		       const struct drm_crtc_state *state)
1053 {
1054 	struct tidss_device *tidss = dispc->tidss;
1055 	struct drm_device *dev = &tidss->ddev;
1056 	const struct tidss_crtc_state *tstate = to_tidss_crtc_state(state);
1057 	const struct dispc_bus_format *fmt;
1058 
1059 	fmt = dispc_vp_find_bus_fmt(dispc, hw_videoport, tstate->bus_format,
1060 				    tstate->bus_flags);
1061 	if (!fmt) {
1062 		drm_dbg(dev, "%s: Unsupported bus format: %u\n",
1063 			__func__, tstate->bus_format);
1064 		return -EINVAL;
1065 	}
1066 
1067 	if (dispc->feat->vp_bus_type[hw_videoport] != DISPC_VP_OLDI_AM65X &&
1068 	    fmt->is_oldi_fmt) {
1069 		drm_dbg(dev, "%s: %s is not OLDI-port\n",
1070 			__func__, dispc->feat->vp_name[hw_videoport]);
1071 		return -EINVAL;
1072 	}
1073 
1074 	return 0;
1075 }
1076 
1077 static void dispc_am65x_oldi_tx_power(struct dispc_device *dispc, bool power)
1078 {
1079 	u32 val = power ? 0 : AM65X_OLDI_PWRDN_TX;
1080 
1081 	if (WARN_ON(!dispc->am65x_oldi_io_ctrl))
1082 		return;
1083 
1084 	regmap_update_bits(dispc->am65x_oldi_io_ctrl, AM65X_OLDI_DAT0_IO_CTRL,
1085 			   AM65X_OLDI_PWRDN_TX, val);
1086 	regmap_update_bits(dispc->am65x_oldi_io_ctrl, AM65X_OLDI_DAT1_IO_CTRL,
1087 			   AM65X_OLDI_PWRDN_TX, val);
1088 	regmap_update_bits(dispc->am65x_oldi_io_ctrl, AM65X_OLDI_DAT2_IO_CTRL,
1089 			   AM65X_OLDI_PWRDN_TX, val);
1090 	regmap_update_bits(dispc->am65x_oldi_io_ctrl, AM65X_OLDI_DAT3_IO_CTRL,
1091 			   AM65X_OLDI_PWRDN_TX, val);
1092 	regmap_update_bits(dispc->am65x_oldi_io_ctrl, AM65X_OLDI_CLK_IO_CTRL,
1093 			   AM65X_OLDI_PWRDN_TX, val);
1094 }
1095 
1096 static void dispc_set_num_datalines(struct dispc_device *dispc,
1097 				    u32 hw_videoport, int num_lines)
1098 {
1099 	int v;
1100 
1101 	switch (num_lines) {
1102 	case 12:
1103 		v = 0; break;
1104 	case 16:
1105 		v = 1; break;
1106 	case 18:
1107 		v = 2; break;
1108 	case 24:
1109 		v = 3; break;
1110 	case 30:
1111 		v = 4; break;
1112 	case 36:
1113 		v = 5; break;
1114 	default:
1115 		WARN_ON(1);
1116 		v = 3;
1117 	}
1118 
1119 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, v,
1120 		       DISPC_VP_CONTROL_DATALINES_MASK);
1121 }
1122 
1123 static void dispc_enable_am65x_oldi(struct dispc_device *dispc, u32 hw_videoport,
1124 				    const struct dispc_bus_format *fmt)
1125 {
1126 	u32 oldi_cfg = 0;
1127 	u32 oldi_reset_bit = BIT(5 + hw_videoport);
1128 	int count = 0;
1129 
1130 	/*
1131 	 * For the moment DUALMODESYNC, MASTERSLAVE, MODE, and SRC
1132 	 * bits of DISPC_VP_DSS_OLDI_CFG are set statically to 0.
1133 	 */
1134 
1135 	if (fmt->data_width == 24)
1136 		oldi_cfg |= BIT(8); /* MSB */
1137 	else if (fmt->data_width != 18)
1138 		dev_warn(dispc->dev, "%s: %d port width not supported\n",
1139 			 __func__, fmt->data_width);
1140 
1141 	oldi_cfg |= BIT(7); /* DEPOL */
1142 
1143 	FIELD_MODIFY(DISPC_VP_DSS_OLDI_CFG_MAP_MASK, &oldi_cfg,
1144 		     fmt->am65x_oldi_mode_reg_val);
1145 
1146 	oldi_cfg |= BIT(12); /* SOFTRST */
1147 
1148 	oldi_cfg |= BIT(0); /* ENABLE */
1149 
1150 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG, oldi_cfg);
1151 
1152 	while (!(oldi_reset_bit & dispc_read(dispc, DSS_SYSSTATUS)) &&
1153 	       count < 10000)
1154 		count++;
1155 
1156 	if (!(oldi_reset_bit & dispc_read(dispc, DSS_SYSSTATUS)))
1157 		dev_warn(dispc->dev, "%s: timeout waiting OLDI reset done\n",
1158 			 __func__);
1159 }
1160 
1161 void dispc_vp_prepare(struct dispc_device *dispc, u32 hw_videoport,
1162 		      const struct drm_crtc_state *state)
1163 {
1164 	const struct tidss_crtc_state *tstate = to_tidss_crtc_state(state);
1165 	const struct dispc_bus_format *fmt;
1166 
1167 	fmt = dispc_vp_find_bus_fmt(dispc, hw_videoport, tstate->bus_format,
1168 				    tstate->bus_flags);
1169 
1170 	if (WARN_ON(!fmt))
1171 		return;
1172 
1173 	if (dispc->feat->vp_bus_type[hw_videoport] == DISPC_VP_OLDI_AM65X) {
1174 		dispc_am65x_oldi_tx_power(dispc, true);
1175 
1176 		dispc_enable_am65x_oldi(dispc, hw_videoport, fmt);
1177 	}
1178 }
1179 
1180 void dispc_vp_enable(struct dispc_device *dispc, u32 hw_videoport,
1181 		     const struct drm_crtc_state *state)
1182 {
1183 	const struct drm_display_mode *mode = &state->adjusted_mode;
1184 	const struct tidss_crtc_state *tstate = to_tidss_crtc_state(state);
1185 	bool align, onoff, rf, ieo, ipc, ihs, ivs;
1186 	const struct dispc_bus_format *fmt;
1187 	u32 hsw, hfp, hbp, vsw, vfp, vbp;
1188 
1189 	fmt = dispc_vp_find_bus_fmt(dispc, hw_videoport, tstate->bus_format,
1190 				    tstate->bus_flags);
1191 
1192 	if (WARN_ON(!fmt))
1193 		return;
1194 
1195 	dispc_set_num_datalines(dispc, hw_videoport, fmt->data_width);
1196 
1197 	hfp = mode->crtc_hsync_start - mode->crtc_hdisplay;
1198 	hsw = mode->crtc_hsync_end - mode->crtc_hsync_start;
1199 	hbp = mode->crtc_htotal - mode->crtc_hsync_end;
1200 
1201 	vfp = mode->crtc_vsync_start - mode->crtc_vdisplay;
1202 	vsw = mode->crtc_vsync_end - mode->crtc_vsync_start;
1203 	vbp = mode->crtc_vtotal - mode->crtc_vsync_end;
1204 
1205 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_TIMING_H,
1206 		       FIELD_PREP(DISPC_VP_TIMING_H_SYNC_PULSE_MASK, hsw - 1) |
1207 		       FIELD_PREP(DISPC_VP_TIMING_H_FRONT_PORCH_MASK, hfp - 1) |
1208 		       FIELD_PREP(DISPC_VP_TIMING_H_BACK_PORCH_MASK, hbp - 1));
1209 
1210 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_TIMING_V,
1211 		       FIELD_PREP(DISPC_VP_TIMING_V_SYNC_PULSE_MASK, vsw - 1) |
1212 		       FIELD_PREP(DISPC_VP_TIMING_V_FRONT_PORCH_MASK, vfp) |
1213 		       FIELD_PREP(DISPC_VP_TIMING_V_BACK_PORCH_MASK, vbp));
1214 
1215 	ivs = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
1216 
1217 	ihs = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
1218 
1219 	ieo = !!(tstate->bus_flags & DRM_BUS_FLAG_DE_LOW);
1220 
1221 	ipc = !!(tstate->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE);
1222 
1223 	/* always use the 'rf' setting */
1224 	onoff = true;
1225 
1226 	rf = !!(tstate->bus_flags & DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE);
1227 
1228 	/* always use aligned syncs */
1229 	align = true;
1230 
1231 	/* always use DE_HIGH for OLDI */
1232 	if (dispc->feat->vp_bus_type[hw_videoport] == DISPC_VP_OLDI_AM65X)
1233 		ieo = false;
1234 
1235 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_POL_FREQ,
1236 		       FIELD_PREP(DISPC_VP_POL_FREQ_ALIGN_MASK, align) |
1237 		       FIELD_PREP(DISPC_VP_POL_FREQ_ONOFF_MASK, onoff) |
1238 		       FIELD_PREP(DISPC_VP_POL_FREQ_RF_MASK, rf) |
1239 		       FIELD_PREP(DISPC_VP_POL_FREQ_IEO_MASK, ieo) |
1240 		       FIELD_PREP(DISPC_VP_POL_FREQ_IPC_MASK, ipc) |
1241 		       FIELD_PREP(DISPC_VP_POL_FREQ_IHS_MASK, ihs) |
1242 		       FIELD_PREP(DISPC_VP_POL_FREQ_IVS_MASK, ivs));
1243 
1244 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_SIZE_SCREEN,
1245 		       FIELD_PREP(DISPC_VP_SIZE_SCREEN_HDISPLAY_MASK,
1246 				  mode->crtc_hdisplay - 1) |
1247 		       FIELD_PREP(DISPC_VP_SIZE_SCREEN_VDISPLAY_MASK,
1248 				  mode->crtc_vdisplay - 1));
1249 
1250 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 1,
1251 		       DISPC_VP_CONTROL_ENABLE_MASK);
1252 }
1253 
1254 void dispc_vp_disable(struct dispc_device *dispc, u32 hw_videoport)
1255 {
1256 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 0,
1257 		       DISPC_VP_CONTROL_ENABLE_MASK);
1258 }
1259 
1260 void dispc_vp_unprepare(struct dispc_device *dispc, u32 hw_videoport)
1261 {
1262 	if (dispc->feat->vp_bus_type[hw_videoport] == DISPC_VP_OLDI_AM65X) {
1263 		dispc_vp_write(dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG, 0);
1264 
1265 		dispc_am65x_oldi_tx_power(dispc, false);
1266 	}
1267 }
1268 
1269 bool dispc_vp_go_busy(struct dispc_device *dispc, u32 hw_videoport)
1270 {
1271 	return VP_REG_GET(dispc, hw_videoport, DISPC_VP_CONTROL,
1272 			  DISPC_VP_CONTROL_GOBIT_MASK);
1273 }
1274 
1275 void dispc_vp_go(struct dispc_device *dispc, u32 hw_videoport)
1276 {
1277 	WARN_ON(VP_REG_GET(dispc, hw_videoport, DISPC_VP_CONTROL,
1278 			   DISPC_VP_CONTROL_GOBIT_MASK));
1279 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 1,
1280 		       DISPC_VP_CONTROL_GOBIT_MASK);
1281 }
1282 
1283 enum c8_to_c12_mode { C8_TO_C12_REPLICATE, C8_TO_C12_MAX, C8_TO_C12_MIN };
1284 
1285 static u16 c8_to_c12(u8 c8, enum c8_to_c12_mode mode)
1286 {
1287 	u16 c12;
1288 
1289 	c12 = c8 << 4;
1290 
1291 	switch (mode) {
1292 	case C8_TO_C12_REPLICATE:
1293 		/* Copy c8 4 MSB to 4 LSB for full scale c12 */
1294 		c12 |= c8 >> 4;
1295 		break;
1296 	case C8_TO_C12_MAX:
1297 		c12 |= 0xF;
1298 		break;
1299 	default:
1300 	case C8_TO_C12_MIN:
1301 		break;
1302 	}
1303 
1304 	return c12;
1305 }
1306 
1307 static u64 argb8888_to_argb12121212(u32 argb8888, enum c8_to_c12_mode m)
1308 {
1309 	u8 a, r, g, b;
1310 	u64 v;
1311 
1312 	a = (argb8888 >> 24) & 0xff;
1313 	r = (argb8888 >> 16) & 0xff;
1314 	g = (argb8888 >> 8) & 0xff;
1315 	b = (argb8888 >> 0) & 0xff;
1316 
1317 	v = ((u64)c8_to_c12(a, m) << 36) | ((u64)c8_to_c12(r, m) << 24) |
1318 		((u64)c8_to_c12(g, m) << 12) | (u64)c8_to_c12(b, m);
1319 
1320 	return v;
1321 }
1322 
1323 static void dispc_vp_set_default_color(struct dispc_device *dispc,
1324 				       u32 hw_videoport, u32 default_color)
1325 {
1326 	u64 v;
1327 
1328 	v = argb8888_to_argb12121212(default_color, C8_TO_C12_REPLICATE);
1329 
1330 	dispc_ovr_write(dispc, hw_videoport,
1331 			DISPC_OVR_DEFAULT_COLOR, v & 0xffffffff);
1332 	dispc_ovr_write(dispc, hw_videoport,
1333 			DISPC_OVR_DEFAULT_COLOR2, (v >> 32) & 0xffff);
1334 }
1335 
1336 enum drm_mode_status dispc_vp_mode_valid(struct dispc_device *dispc,
1337 					 u32 hw_videoport,
1338 					 const struct drm_display_mode *mode)
1339 {
1340 	u32 hsw, hfp, hbp, vsw, vfp, vbp;
1341 	enum dispc_vp_bus_type bus_type;
1342 	int max_pclk;
1343 
1344 	bus_type = dispc->feat->vp_bus_type[hw_videoport];
1345 
1346 	max_pclk = dispc->feat->max_pclk_khz[bus_type];
1347 
1348 	if (WARN_ON(max_pclk == 0))
1349 		return MODE_BAD;
1350 
1351 	if (mode->clock < dispc->feat->min_pclk_khz)
1352 		return MODE_CLOCK_LOW;
1353 
1354 	if (mode->clock > max_pclk)
1355 		return MODE_CLOCK_HIGH;
1356 
1357 	if (mode->hdisplay > 4096)
1358 		return MODE_BAD;
1359 
1360 	if (mode->vdisplay > 4096)
1361 		return MODE_BAD;
1362 
1363 	/* TODO: add interlace support */
1364 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1365 		return MODE_NO_INTERLACE;
1366 
1367 	/*
1368 	 * Enforce the output width is divisible by 2. Actually this
1369 	 * is only needed in following cases:
1370 	 * - YUV output selected (BT656, BT1120)
1371 	 * - Dithering enabled
1372 	 * - TDM with TDMCycleFormat == 3
1373 	 * But for simplicity we enforce that always.
1374 	 */
1375 	if ((mode->hdisplay % 2) != 0)
1376 		return MODE_BAD_HVALUE;
1377 
1378 	hfp = mode->hsync_start - mode->hdisplay;
1379 	hsw = mode->hsync_end - mode->hsync_start;
1380 	hbp = mode->htotal - mode->hsync_end;
1381 
1382 	vfp = mode->vsync_start - mode->vdisplay;
1383 	vsw = mode->vsync_end - mode->vsync_start;
1384 	vbp = mode->vtotal - mode->vsync_end;
1385 
1386 	if (hsw < 1 || hsw > 256 ||
1387 	    hfp < 1 || hfp > 4096 ||
1388 	    hbp < 1 || hbp > 4096)
1389 		return MODE_BAD_HVALUE;
1390 
1391 	if (vsw < 1 || vsw > 256 ||
1392 	    vfp > 4095 || vbp > 4095)
1393 		return MODE_BAD_VVALUE;
1394 
1395 	if (dispc->memory_bandwidth_limit) {
1396 		const unsigned int bpp = 4;
1397 		u64 bandwidth;
1398 
1399 		bandwidth = 1000 * mode->clock;
1400 		bandwidth = bandwidth * mode->hdisplay * mode->vdisplay * bpp;
1401 		bandwidth = div_u64(bandwidth, mode->htotal * mode->vtotal);
1402 
1403 		if (dispc->memory_bandwidth_limit < bandwidth)
1404 			return MODE_BAD;
1405 	}
1406 
1407 	return MODE_OK;
1408 }
1409 
1410 int dispc_vp_enable_clk(struct dispc_device *dispc, u32 hw_videoport)
1411 {
1412 	int ret = clk_prepare_enable(dispc->vp_clk[hw_videoport]);
1413 
1414 	if (ret)
1415 		dev_err(dispc->dev, "%s: enabling clk failed: %d\n", __func__,
1416 			ret);
1417 
1418 	return ret;
1419 }
1420 
1421 void dispc_vp_disable_clk(struct dispc_device *dispc, u32 hw_videoport)
1422 {
1423 	clk_disable_unprepare(dispc->vp_clk[hw_videoport]);
1424 }
1425 
1426 /*
1427  * Calculate the percentage difference between the requested pixel clock rate
1428  * and the effective rate resulting from calculating the clock divider value.
1429  */
1430 unsigned int dispc_pclk_diff(unsigned long rate, unsigned long real_rate)
1431 {
1432 	int r = rate / 100, rr = real_rate / 100;
1433 
1434 	return (unsigned int)(abs(((rr - r) * 100) / r));
1435 }
1436 
1437 int dispc_vp_set_clk_rate(struct dispc_device *dispc, u32 hw_videoport,
1438 			  unsigned long rate)
1439 {
1440 	int r;
1441 	unsigned long new_rate;
1442 
1443 	r = clk_set_rate(dispc->vp_clk[hw_videoport], rate);
1444 	if (r) {
1445 		dev_err(dispc->dev, "vp%d: failed to set clk rate to %lu\n",
1446 			hw_videoport, rate);
1447 		return r;
1448 	}
1449 
1450 	new_rate = clk_get_rate(dispc->vp_clk[hw_videoport]);
1451 
1452 	if (dispc_pclk_diff(rate, new_rate) > 5)
1453 		dev_warn(dispc->dev,
1454 			 "vp%d: Clock rate %lu differs over 5%% from requested %lu\n",
1455 			 hw_videoport, new_rate, rate);
1456 
1457 	dev_dbg(dispc->dev, "vp%d: new rate %lu Hz (requested %lu Hz)\n",
1458 		hw_videoport, clk_get_rate(dispc->vp_clk[hw_videoport]), rate);
1459 
1460 	return 0;
1461 }
1462 
1463 /* OVR */
1464 static void dispc_k2g_ovr_set_plane(struct dispc_device *dispc,
1465 				    u32 hw_plane, u32 hw_videoport,
1466 				    u32 x, u32 y, u32 layer)
1467 {
1468 	/* On k2g there is only one plane and no need for ovr */
1469 	dispc_vid_write(dispc, hw_plane, DISPC_VID_K2G_POSITION,
1470 			x | (y << 16));
1471 }
1472 
1473 static void dispc_am65x_ovr_set_plane(struct dispc_device *dispc,
1474 				      u32 hw_plane, u32 hw_videoport,
1475 				      u32 x, u32 y, u32 layer)
1476 {
1477 	u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
1478 
1479 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer),
1480 			hw_id, DISPC_OVR_ATTRIBUTES_CHANNELIN_MASK);
1481 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer), x,
1482 			DISPC_OVR_ATTRIBUTES_POSX_MASK);
1483 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer), y,
1484 			DISPC_OVR_ATTRIBUTES_POSY_MASK);
1485 }
1486 
1487 static void dispc_j721e_ovr_set_plane(struct dispc_device *dispc,
1488 				      u32 hw_plane, u32 hw_videoport,
1489 				      u32 x, u32 y, u32 layer)
1490 {
1491 	u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
1492 
1493 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer),
1494 			hw_id, DISPC_OVR_ATTRIBUTES_CHANNELIN_MASK);
1495 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES2(layer), x,
1496 			DISPC_OVR_ATTRIBUTES2_POSX_MASK);
1497 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES2(layer), y,
1498 			DISPC_OVR_ATTRIBUTES2_POSY_MASK);
1499 }
1500 
1501 void dispc_ovr_set_plane(struct dispc_device *dispc, u32 hw_plane,
1502 			 u32 hw_videoport, u32 x, u32 y, u32 layer)
1503 {
1504 	switch (dispc->feat->subrev) {
1505 	case DISPC_K2G:
1506 		dispc_k2g_ovr_set_plane(dispc, hw_plane, hw_videoport,
1507 					x, y, layer);
1508 		break;
1509 	case DISPC_AM625:
1510 	case DISPC_AM62A7:
1511 	case DISPC_AM62L:
1512 	case DISPC_AM65X:
1513 		dispc_am65x_ovr_set_plane(dispc, hw_plane, hw_videoport,
1514 					  x, y, layer);
1515 		break;
1516 	case DISPC_J721E:
1517 		dispc_j721e_ovr_set_plane(dispc, hw_plane, hw_videoport,
1518 					  x, y, layer);
1519 		break;
1520 	default:
1521 		WARN_ON(1);
1522 		break;
1523 	}
1524 }
1525 
1526 void dispc_ovr_enable_layer(struct dispc_device *dispc,
1527 			    u32 hw_videoport, u32 layer, bool enable)
1528 {
1529 	if (dispc->feat->subrev == DISPC_K2G)
1530 		return;
1531 
1532 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer),
1533 			!!enable, DISPC_OVR_ATTRIBUTES_ENABLE_MASK);
1534 }
1535 
1536 /* CSC */
1537 enum csc_ctm {
1538 	CSC_RR, CSC_RG, CSC_RB,
1539 	CSC_GR, CSC_GG, CSC_GB,
1540 	CSC_BR, CSC_BG, CSC_BB,
1541 };
1542 
1543 enum csc_yuv2rgb {
1544 	CSC_RY, CSC_RCB, CSC_RCR,
1545 	CSC_GY, CSC_GCB, CSC_GCR,
1546 	CSC_BY, CSC_BCB, CSC_BCR,
1547 };
1548 
1549 enum csc_rgb2yuv {
1550 	CSC_YR,  CSC_YG,  CSC_YB,
1551 	CSC_CBR, CSC_CBG, CSC_CBB,
1552 	CSC_CRR, CSC_CRG, CSC_CRB,
1553 };
1554 
1555 struct dispc_csc_coef {
1556 	void (*to_regval)(const struct dispc_csc_coef *csc, u32 *regval);
1557 	int m[9];
1558 	int preoffset[3];
1559 	int postoffset[3];
1560 	enum { CLIP_LIMITED_RANGE = 0, CLIP_FULL_RANGE = 1, } cliping;
1561 	const char *name;
1562 };
1563 
1564 #define DISPC_CSC_REGVAL_LEN 8
1565 
1566 static
1567 void dispc_csc_offset_regval(const struct dispc_csc_coef *csc, u32 *regval)
1568 {
1569 #define OVAL(x, y) (FIELD_PREP(GENMASK(15, 3), x) | FIELD_PREP(GENMASK(31, 19), y))
1570 	regval[5] = OVAL(csc->preoffset[0], csc->preoffset[1]);
1571 	regval[6] = OVAL(csc->preoffset[2], csc->postoffset[0]);
1572 	regval[7] = OVAL(csc->postoffset[1], csc->postoffset[2]);
1573 #undef OVAL
1574 }
1575 
1576 #define CVAL(x, y) (FIELD_PREP(GENMASK(10, 0), x) | FIELD_PREP(GENMASK(26, 16), y))
1577 static
1578 void dispc_csc_yuv2rgb_regval(const struct dispc_csc_coef *csc, u32 *regval)
1579 {
1580 	regval[0] = CVAL(csc->m[CSC_RY], csc->m[CSC_RCR]);
1581 	regval[1] = CVAL(csc->m[CSC_RCB], csc->m[CSC_GY]);
1582 	regval[2] = CVAL(csc->m[CSC_GCR], csc->m[CSC_GCB]);
1583 	regval[3] = CVAL(csc->m[CSC_BY], csc->m[CSC_BCR]);
1584 	regval[4] = CVAL(csc->m[CSC_BCB], 0);
1585 
1586 	dispc_csc_offset_regval(csc, regval);
1587 }
1588 
1589 __maybe_unused static
1590 void dispc_csc_rgb2yuv_regval(const struct dispc_csc_coef *csc, u32 *regval)
1591 {
1592 	regval[0] = CVAL(csc->m[CSC_YR], csc->m[CSC_YG]);
1593 	regval[1] = CVAL(csc->m[CSC_YB], csc->m[CSC_CRR]);
1594 	regval[2] = CVAL(csc->m[CSC_CRG], csc->m[CSC_CRB]);
1595 	regval[3] = CVAL(csc->m[CSC_CBR], csc->m[CSC_CBG]);
1596 	regval[4] = CVAL(csc->m[CSC_CBB], 0);
1597 
1598 	dispc_csc_offset_regval(csc, regval);
1599 }
1600 
1601 static void dispc_csc_cpr_regval(const struct dispc_csc_coef *csc,
1602 				 u32 *regval)
1603 {
1604 	regval[0] = CVAL(csc->m[CSC_RR], csc->m[CSC_RG]);
1605 	regval[1] = CVAL(csc->m[CSC_RB], csc->m[CSC_GR]);
1606 	regval[2] = CVAL(csc->m[CSC_GG], csc->m[CSC_GB]);
1607 	regval[3] = CVAL(csc->m[CSC_BR], csc->m[CSC_BG]);
1608 	regval[4] = CVAL(csc->m[CSC_BB], 0);
1609 
1610 	dispc_csc_offset_regval(csc, regval);
1611 }
1612 
1613 #undef CVAL
1614 
1615 static void dispc_k2g_vid_write_csc(struct dispc_device *dispc, u32 hw_plane,
1616 				    const struct dispc_csc_coef *csc)
1617 {
1618 	static const u16 dispc_vid_csc_coef_reg[] = {
1619 		DISPC_VID_CSC_COEF(0), DISPC_VID_CSC_COEF(1),
1620 		DISPC_VID_CSC_COEF(2), DISPC_VID_CSC_COEF(3),
1621 		DISPC_VID_CSC_COEF(4), DISPC_VID_CSC_COEF(5),
1622 		DISPC_VID_CSC_COEF(6), /* K2G has no post offset support */
1623 	};
1624 	u32 regval[DISPC_CSC_REGVAL_LEN];
1625 	unsigned int i;
1626 
1627 	csc->to_regval(csc, regval);
1628 
1629 	if (regval[7] != 0)
1630 		dev_warn(dispc->dev, "%s: No post offset support for %s\n",
1631 			 __func__, csc->name);
1632 
1633 	for (i = 0; i < ARRAY_SIZE(dispc_vid_csc_coef_reg); i++)
1634 		dispc_vid_write(dispc, hw_plane, dispc_vid_csc_coef_reg[i],
1635 				regval[i]);
1636 }
1637 
1638 static void dispc_k3_vid_write_csc(struct dispc_device *dispc, u32 hw_plane,
1639 				   const struct dispc_csc_coef *csc)
1640 {
1641 	static const u16 dispc_vid_csc_coef_reg[DISPC_CSC_REGVAL_LEN] = {
1642 		DISPC_VID_CSC_COEF(0), DISPC_VID_CSC_COEF(1),
1643 		DISPC_VID_CSC_COEF(2), DISPC_VID_CSC_COEF(3),
1644 		DISPC_VID_CSC_COEF(4), DISPC_VID_CSC_COEF(5),
1645 		DISPC_VID_CSC_COEF(6), DISPC_VID_CSC_COEF7,
1646 	};
1647 	u32 regval[DISPC_CSC_REGVAL_LEN];
1648 	unsigned int i;
1649 
1650 	csc->to_regval(csc, regval);
1651 
1652 	for (i = 0; i < ARRAY_SIZE(dispc_vid_csc_coef_reg); i++)
1653 		dispc_vid_write(dispc, hw_plane, dispc_vid_csc_coef_reg[i],
1654 				regval[i]);
1655 }
1656 
1657 /* YUV -> RGB, ITU-R BT.601, full range */
1658 static const struct dispc_csc_coef csc_yuv2rgb_bt601_full = {
1659 	dispc_csc_yuv2rgb_regval,
1660 	{ 256,   0,  358,	/* ry, rcb, rcr |1.000  0.000  1.402|*/
1661 	  256, -88, -182,	/* gy, gcb, gcr |1.000 -0.344 -0.714|*/
1662 	  256, 452,    0, },	/* by, bcb, bcr |1.000  1.772  0.000|*/
1663 	{    0, -2048, -2048, },	/* full range */
1664 	{    0,     0,     0, },
1665 	CLIP_FULL_RANGE,
1666 	"BT.601 Full",
1667 };
1668 
1669 /* YUV -> RGB, ITU-R BT.601, limited range */
1670 static const struct dispc_csc_coef csc_yuv2rgb_bt601_lim = {
1671 	dispc_csc_yuv2rgb_regval,
1672 	{ 298,    0,  409,	/* ry, rcb, rcr |1.164  0.000  1.596|*/
1673 	  298, -100, -208,	/* gy, gcb, gcr |1.164 -0.392 -0.813|*/
1674 	  298,  516,    0, },	/* by, bcb, bcr |1.164  2.017  0.000|*/
1675 	{ -256, -2048, -2048, },	/* limited range */
1676 	{    0,     0,     0, },
1677 	CLIP_FULL_RANGE,
1678 	"BT.601 Limited",
1679 };
1680 
1681 /* YUV -> RGB, ITU-R BT.709, full range */
1682 static const struct dispc_csc_coef csc_yuv2rgb_bt709_full = {
1683 	dispc_csc_yuv2rgb_regval,
1684 	{ 256,	  0,  402,	/* ry, rcb, rcr |1.000	0.000  1.570|*/
1685 	  256,  -48, -120,	/* gy, gcb, gcr |1.000 -0.187 -0.467|*/
1686 	  256,  475,    0, },	/* by, bcb, bcr |1.000	1.856  0.000|*/
1687 	{    0, -2048, -2048, },	/* full range */
1688 	{    0,     0,     0, },
1689 	CLIP_FULL_RANGE,
1690 	"BT.709 Full",
1691 };
1692 
1693 /* YUV -> RGB, ITU-R BT.709, limited range */
1694 static const struct dispc_csc_coef csc_yuv2rgb_bt709_lim = {
1695 	dispc_csc_yuv2rgb_regval,
1696 	{ 298,    0,  459,	/* ry, rcb, rcr |1.164  0.000  1.793|*/
1697 	  298,  -55, -136,	/* gy, gcb, gcr |1.164 -0.213 -0.533|*/
1698 	  298,  541,    0, },	/* by, bcb, bcr |1.164  2.112  0.000|*/
1699 	{ -256, -2048, -2048, },	/* limited range */
1700 	{    0,     0,     0, },
1701 	CLIP_FULL_RANGE,
1702 	"BT.709 Limited",
1703 };
1704 
1705 static const struct {
1706 	enum drm_color_encoding encoding;
1707 	enum drm_color_range range;
1708 	const struct dispc_csc_coef *csc;
1709 } dispc_csc_table[] = {
1710 	{ DRM_COLOR_YCBCR_BT601, DRM_COLOR_YCBCR_FULL_RANGE,
1711 	  &csc_yuv2rgb_bt601_full, },
1712 	{ DRM_COLOR_YCBCR_BT601, DRM_COLOR_YCBCR_LIMITED_RANGE,
1713 	  &csc_yuv2rgb_bt601_lim, },
1714 	{ DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_FULL_RANGE,
1715 	  &csc_yuv2rgb_bt709_full, },
1716 	{ DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE,
1717 	  &csc_yuv2rgb_bt709_lim, },
1718 };
1719 
1720 static const
1721 struct dispc_csc_coef *dispc_find_csc(enum drm_color_encoding encoding,
1722 				      enum drm_color_range range)
1723 {
1724 	unsigned int i;
1725 
1726 	for (i = 0; i < ARRAY_SIZE(dispc_csc_table); i++) {
1727 		if (dispc_csc_table[i].encoding == encoding &&
1728 		    dispc_csc_table[i].range == range) {
1729 			return dispc_csc_table[i].csc;
1730 		}
1731 	}
1732 	return NULL;
1733 }
1734 
1735 static void dispc_vid_csc_setup(struct dispc_device *dispc, u32 hw_plane,
1736 				const struct drm_plane_state *state)
1737 {
1738 	const struct dispc_csc_coef *coef;
1739 
1740 	coef = dispc_find_csc(state->color_encoding, state->color_range);
1741 	if (!coef) {
1742 		dev_err(dispc->dev, "%s: CSC (%u,%u) not found\n",
1743 			__func__, state->color_encoding, state->color_range);
1744 		return;
1745 	}
1746 
1747 	if (dispc->feat->subrev == DISPC_K2G)
1748 		dispc_k2g_vid_write_csc(dispc, hw_plane, coef);
1749 	else
1750 		dispc_k3_vid_write_csc(dispc, hw_plane, coef);
1751 }
1752 
1753 static void dispc_vid_csc_enable(struct dispc_device *dispc, u32 hw_plane,
1754 				 bool enable)
1755 {
1756 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, !!enable,
1757 			DISPC_VID_ATTRIBUTES_COLORCONVENABLE_MASK);
1758 }
1759 
1760 /* SCALER */
1761 
1762 static u32 dispc_calc_fir_inc(u32 in, u32 out)
1763 {
1764 	return (u32)div_u64(0x200000ull * in, out);
1765 }
1766 
1767 enum dispc_vid_fir_coef_set {
1768 	DISPC_VID_FIR_COEF_HORIZ,
1769 	DISPC_VID_FIR_COEF_HORIZ_UV,
1770 	DISPC_VID_FIR_COEF_VERT,
1771 	DISPC_VID_FIR_COEF_VERT_UV,
1772 };
1773 
1774 static void dispc_vid_write_fir_coefs(struct dispc_device *dispc,
1775 				      u32 hw_plane,
1776 				      enum dispc_vid_fir_coef_set coef_set,
1777 				      const struct tidss_scale_coefs *coefs)
1778 {
1779 	static const u16 c0_regs[] = {
1780 		[DISPC_VID_FIR_COEF_HORIZ] = DISPC_VID_FIR_COEFS_H0,
1781 		[DISPC_VID_FIR_COEF_HORIZ_UV] = DISPC_VID_FIR_COEFS_H0_C,
1782 		[DISPC_VID_FIR_COEF_VERT] = DISPC_VID_FIR_COEFS_V0,
1783 		[DISPC_VID_FIR_COEF_VERT_UV] = DISPC_VID_FIR_COEFS_V0_C,
1784 	};
1785 
1786 	static const u16 c12_regs[] = {
1787 		[DISPC_VID_FIR_COEF_HORIZ] = DISPC_VID_FIR_COEFS_H12,
1788 		[DISPC_VID_FIR_COEF_HORIZ_UV] = DISPC_VID_FIR_COEFS_H12_C,
1789 		[DISPC_VID_FIR_COEF_VERT] = DISPC_VID_FIR_COEFS_V12,
1790 		[DISPC_VID_FIR_COEF_VERT_UV] = DISPC_VID_FIR_COEFS_V12_C,
1791 	};
1792 
1793 	const u16 c0_base = c0_regs[coef_set];
1794 	const u16 c12_base = c12_regs[coef_set];
1795 	int phase;
1796 
1797 	if (!coefs) {
1798 		dev_err(dispc->dev, "%s: No coefficients given.\n", __func__);
1799 		return;
1800 	}
1801 
1802 	for (phase = 0; phase <= 8; ++phase) {
1803 		u16 reg = c0_base + phase * 4;
1804 		u16 c0 = coefs->c0[phase];
1805 
1806 		dispc_vid_write(dispc, hw_plane, reg, c0);
1807 	}
1808 
1809 	for (phase = 0; phase <= 15; ++phase) {
1810 		u16 reg = c12_base + phase * 4;
1811 		s16 c1, c2;
1812 		u32 c12;
1813 
1814 		c1 = coefs->c1[phase];
1815 		c2 = coefs->c2[phase];
1816 		c12 = FIELD_PREP(GENMASK(19, 10), c1) | FIELD_PREP(GENMASK(29, 20),
1817 								   c2);
1818 
1819 		dispc_vid_write(dispc, hw_plane, reg, c12);
1820 	}
1821 }
1822 
1823 static bool dispc_fourcc_is_yuv(u32 fourcc)
1824 {
1825 	switch (fourcc) {
1826 	case DRM_FORMAT_YUYV:
1827 	case DRM_FORMAT_UYVY:
1828 	case DRM_FORMAT_NV12:
1829 		return true;
1830 	default:
1831 		return false;
1832 	}
1833 }
1834 
1835 struct dispc_scaling_params {
1836 	int xinc, yinc;
1837 	u32 in_w, in_h, in_w_uv, in_h_uv;
1838 	u32 fir_xinc, fir_yinc, fir_xinc_uv, fir_yinc_uv;
1839 	bool scale_x, scale_y;
1840 	const struct tidss_scale_coefs *xcoef, *ycoef, *xcoef_uv, *ycoef_uv;
1841 	bool five_taps;
1842 };
1843 
1844 static int dispc_vid_calc_scaling(struct dispc_device *dispc,
1845 				  const struct drm_plane_state *state,
1846 				  struct dispc_scaling_params *sp,
1847 				  bool lite_plane)
1848 {
1849 	const struct dispc_features_scaling *f = &dispc->feat->scaling;
1850 	u32 fourcc = state->fb->format->format;
1851 	u32 in_width_max_5tap = f->in_width_max_5tap_rgb;
1852 	u32 in_width_max_3tap = f->in_width_max_3tap_rgb;
1853 	u32 downscale_limit;
1854 	u32 in_width_max;
1855 
1856 	memset(sp, 0, sizeof(*sp));
1857 	sp->xinc = 1;
1858 	sp->yinc = 1;
1859 	sp->in_w = state->src_w >> 16;
1860 	sp->in_w_uv = sp->in_w;
1861 	sp->in_h = state->src_h >> 16;
1862 	sp->in_h_uv = sp->in_h;
1863 
1864 	sp->scale_x = sp->in_w != state->crtc_w;
1865 	sp->scale_y = sp->in_h != state->crtc_h;
1866 
1867 	if (dispc_fourcc_is_yuv(fourcc)) {
1868 		in_width_max_5tap = f->in_width_max_5tap_yuv;
1869 		in_width_max_3tap = f->in_width_max_3tap_yuv;
1870 
1871 		sp->in_w_uv >>= 1;
1872 		sp->scale_x = true;
1873 
1874 		if (fourcc == DRM_FORMAT_NV12) {
1875 			sp->in_h_uv >>= 1;
1876 			sp->scale_y = true;
1877 		}
1878 	}
1879 
1880 	/* Skip the rest if no scaling is used */
1881 	if ((!sp->scale_x && !sp->scale_y) || lite_plane)
1882 		return 0;
1883 
1884 	if (sp->in_w > in_width_max_5tap) {
1885 		sp->five_taps = false;
1886 		in_width_max = in_width_max_3tap;
1887 		downscale_limit = f->downscale_limit_3tap;
1888 	} else {
1889 		sp->five_taps = true;
1890 		in_width_max = in_width_max_5tap;
1891 		downscale_limit = f->downscale_limit_5tap;
1892 	}
1893 
1894 	if (sp->scale_x) {
1895 		sp->fir_xinc = dispc_calc_fir_inc(sp->in_w, state->crtc_w);
1896 
1897 		if (sp->fir_xinc < dispc_calc_fir_inc(1, f->upscale_limit)) {
1898 			dev_dbg(dispc->dev,
1899 				"%s: X-scaling factor %u/%u > %u\n",
1900 				__func__, state->crtc_w, state->src_w >> 16,
1901 				f->upscale_limit);
1902 			return -EINVAL;
1903 		}
1904 
1905 		if (sp->fir_xinc >= dispc_calc_fir_inc(downscale_limit, 1)) {
1906 			sp->xinc = DIV_ROUND_UP(DIV_ROUND_UP(sp->in_w,
1907 							     state->crtc_w),
1908 						downscale_limit);
1909 
1910 			if (sp->xinc > f->xinc_max) {
1911 				dev_dbg(dispc->dev,
1912 					"%s: X-scaling factor %u/%u < 1/%u\n",
1913 					__func__, state->crtc_w,
1914 					state->src_w >> 16,
1915 					downscale_limit * f->xinc_max);
1916 				return -EINVAL;
1917 			}
1918 
1919 			sp->in_w = (state->src_w >> 16) / sp->xinc;
1920 		}
1921 
1922 		while (sp->in_w > in_width_max) {
1923 			sp->xinc++;
1924 			sp->in_w = (state->src_w >> 16) / sp->xinc;
1925 		}
1926 
1927 		if (sp->xinc > f->xinc_max) {
1928 			dev_dbg(dispc->dev,
1929 				"%s: Too wide input buffer %u > %u\n", __func__,
1930 				state->src_w >> 16, in_width_max * f->xinc_max);
1931 			return -EINVAL;
1932 		}
1933 
1934 		/*
1935 		 * We need even line length for YUV formats. Decimation
1936 		 * can lead to odd length, so we need to make it even
1937 		 * again.
1938 		 */
1939 		if (dispc_fourcc_is_yuv(fourcc))
1940 			sp->in_w &= ~1;
1941 
1942 		sp->fir_xinc = dispc_calc_fir_inc(sp->in_w, state->crtc_w);
1943 	}
1944 
1945 	if (sp->scale_y) {
1946 		sp->fir_yinc = dispc_calc_fir_inc(sp->in_h, state->crtc_h);
1947 
1948 		if (sp->fir_yinc < dispc_calc_fir_inc(1, f->upscale_limit)) {
1949 			dev_dbg(dispc->dev,
1950 				"%s: Y-scaling factor %u/%u > %u\n",
1951 				__func__, state->crtc_h, state->src_h >> 16,
1952 				f->upscale_limit);
1953 			return -EINVAL;
1954 		}
1955 
1956 		if (sp->fir_yinc >= dispc_calc_fir_inc(downscale_limit, 1)) {
1957 			sp->yinc = DIV_ROUND_UP(DIV_ROUND_UP(sp->in_h,
1958 							     state->crtc_h),
1959 						downscale_limit);
1960 
1961 			sp->in_h /= sp->yinc;
1962 			sp->fir_yinc = dispc_calc_fir_inc(sp->in_h,
1963 							  state->crtc_h);
1964 		}
1965 	}
1966 
1967 	dev_dbg(dispc->dev,
1968 		"%s: %ux%u decim %ux%u -> %ux%u firinc %u.%03ux%u.%03u taps %u -> %ux%u\n",
1969 		__func__, state->src_w >> 16, state->src_h >> 16,
1970 		sp->xinc, sp->yinc, sp->in_w, sp->in_h,
1971 		sp->fir_xinc / 0x200000u,
1972 		((sp->fir_xinc & 0x1FFFFFu) * 999u) / 0x1FFFFFu,
1973 		sp->fir_yinc / 0x200000u,
1974 		((sp->fir_yinc & 0x1FFFFFu) * 999u) / 0x1FFFFFu,
1975 		sp->five_taps ? 5 : 3,
1976 		state->crtc_w, state->crtc_h);
1977 
1978 	if (dispc_fourcc_is_yuv(fourcc)) {
1979 		if (sp->scale_x) {
1980 			sp->in_w_uv /= sp->xinc;
1981 			sp->fir_xinc_uv = dispc_calc_fir_inc(sp->in_w_uv,
1982 							     state->crtc_w);
1983 			sp->xcoef_uv = tidss_get_scale_coefs(dispc->dev,
1984 							     sp->fir_xinc_uv,
1985 							     true);
1986 		}
1987 		if (sp->scale_y) {
1988 			sp->in_h_uv /= sp->yinc;
1989 			sp->fir_yinc_uv = dispc_calc_fir_inc(sp->in_h_uv,
1990 							     state->crtc_h);
1991 			sp->ycoef_uv = tidss_get_scale_coefs(dispc->dev,
1992 							     sp->fir_yinc_uv,
1993 							     sp->five_taps);
1994 		}
1995 	}
1996 
1997 	if (sp->scale_x)
1998 		sp->xcoef = tidss_get_scale_coefs(dispc->dev, sp->fir_xinc,
1999 						  true);
2000 
2001 	if (sp->scale_y)
2002 		sp->ycoef = tidss_get_scale_coefs(dispc->dev, sp->fir_yinc,
2003 						  sp->five_taps);
2004 
2005 	return 0;
2006 }
2007 
2008 static void dispc_vid_set_scaling(struct dispc_device *dispc,
2009 				  u32 hw_plane,
2010 				  struct dispc_scaling_params *sp,
2011 				  u32 fourcc)
2012 {
2013 	/* HORIZONTAL RESIZE ENABLE */
2014 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, sp->scale_x,
2015 			DISPC_VID_ATTRIBUTES_HRESIZEENABLE_MASK);
2016 
2017 	/* VERTICAL RESIZE ENABLE */
2018 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, sp->scale_y,
2019 			DISPC_VID_ATTRIBUTES_VRESIZEENABLE_MASK);
2020 
2021 	/* Skip the rest if no scaling is used */
2022 	if (!sp->scale_x && !sp->scale_y)
2023 		return;
2024 
2025 	/* VERTICAL 5-TAPS  */
2026 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, sp->five_taps,
2027 			DISPC_VID_ATTRIBUTES_VERTICALTAPS_MASK);
2028 
2029 	if (dispc_fourcc_is_yuv(fourcc)) {
2030 		if (sp->scale_x) {
2031 			dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRH2,
2032 					sp->fir_xinc_uv);
2033 			dispc_vid_write_fir_coefs(dispc, hw_plane,
2034 						  DISPC_VID_FIR_COEF_HORIZ_UV,
2035 						  sp->xcoef_uv);
2036 		}
2037 		if (sp->scale_y) {
2038 			dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRV2,
2039 					sp->fir_yinc_uv);
2040 			dispc_vid_write_fir_coefs(dispc, hw_plane,
2041 						  DISPC_VID_FIR_COEF_VERT_UV,
2042 						  sp->ycoef_uv);
2043 		}
2044 	}
2045 
2046 	if (sp->scale_x) {
2047 		dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRH, sp->fir_xinc);
2048 		dispc_vid_write_fir_coefs(dispc, hw_plane,
2049 					  DISPC_VID_FIR_COEF_HORIZ,
2050 					  sp->xcoef);
2051 	}
2052 
2053 	if (sp->scale_y) {
2054 		dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRV, sp->fir_yinc);
2055 		dispc_vid_write_fir_coefs(dispc, hw_plane,
2056 					  DISPC_VID_FIR_COEF_VERT, sp->ycoef);
2057 	}
2058 }
2059 
2060 /* OTHER */
2061 
2062 static const struct {
2063 	u32 fourcc;
2064 	u8 dss_code;
2065 } dispc_color_formats[] = {
2066 	{ DRM_FORMAT_ARGB4444, 0x0, },
2067 	{ DRM_FORMAT_ABGR4444, 0x1, },
2068 	{ DRM_FORMAT_RGBA4444, 0x2, },
2069 
2070 	{ DRM_FORMAT_RGB565, 0x3, },
2071 	{ DRM_FORMAT_BGR565, 0x4, },
2072 
2073 	{ DRM_FORMAT_ARGB1555, 0x5, },
2074 	{ DRM_FORMAT_ABGR1555, 0x6, },
2075 
2076 	{ DRM_FORMAT_ARGB8888, 0x7, },
2077 	{ DRM_FORMAT_ABGR8888, 0x8, },
2078 	{ DRM_FORMAT_RGBA8888, 0x9, },
2079 	{ DRM_FORMAT_BGRA8888, 0xa, },
2080 
2081 	{ DRM_FORMAT_RGB888, 0xb, },
2082 	{ DRM_FORMAT_BGR888, 0xc, },
2083 
2084 	{ DRM_FORMAT_ARGB2101010, 0xe, },
2085 	{ DRM_FORMAT_ABGR2101010, 0xf, },
2086 
2087 	{ DRM_FORMAT_XRGB4444, 0x20, },
2088 	{ DRM_FORMAT_XBGR4444, 0x21, },
2089 	{ DRM_FORMAT_RGBX4444, 0x22, },
2090 
2091 	{ DRM_FORMAT_XRGB1555, 0x25, },
2092 	{ DRM_FORMAT_XBGR1555, 0x26, },
2093 
2094 	{ DRM_FORMAT_XRGB8888, 0x27, },
2095 	{ DRM_FORMAT_XBGR8888, 0x28, },
2096 	{ DRM_FORMAT_RGBX8888, 0x29, },
2097 	{ DRM_FORMAT_BGRX8888, 0x2a, },
2098 
2099 	{ DRM_FORMAT_XRGB2101010, 0x2e, },
2100 	{ DRM_FORMAT_XBGR2101010, 0x2f, },
2101 
2102 	{ DRM_FORMAT_YUYV, 0x3e, },
2103 	{ DRM_FORMAT_UYVY, 0x3f, },
2104 
2105 	{ DRM_FORMAT_NV12, 0x3d, },
2106 };
2107 
2108 static void dispc_plane_set_pixel_format(struct dispc_device *dispc,
2109 					 u32 hw_plane, u32 fourcc)
2110 {
2111 	unsigned int i;
2112 
2113 	for (i = 0; i < ARRAY_SIZE(dispc_color_formats); ++i) {
2114 		if (dispc_color_formats[i].fourcc == fourcc) {
2115 			VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES,
2116 					dispc_color_formats[i].dss_code,
2117 					DISPC_VID_ATTRIBUTES_FORMAT_MASK);
2118 			return;
2119 		}
2120 	}
2121 
2122 	WARN_ON(1);
2123 }
2124 
2125 const u32 *dispc_plane_formats(struct dispc_device *dispc, unsigned int *len)
2126 {
2127 	WARN_ON(!dispc->fourccs);
2128 
2129 	*len = dispc->num_fourccs;
2130 
2131 	return dispc->fourccs;
2132 }
2133 
2134 static s32 pixinc(int pixels, u8 ps)
2135 {
2136 	if (pixels == 1)
2137 		return 1;
2138 	else if (pixels > 1)
2139 		return 1 + (pixels - 1) * ps;
2140 	else if (pixels < 0)
2141 		return 1 - (-pixels + 1) * ps;
2142 
2143 	WARN_ON(1);
2144 	return 0;
2145 }
2146 
2147 int dispc_plane_check(struct dispc_device *dispc, u32 hw_plane,
2148 		      const struct drm_plane_state *state,
2149 		      u32 hw_videoport)
2150 {
2151 	bool lite = dispc->feat->vid_info[hw_plane].is_lite;
2152 	u32 fourcc = state->fb->format->format;
2153 	bool need_scaling = state->src_w >> 16 != state->crtc_w ||
2154 		state->src_h >> 16 != state->crtc_h;
2155 	struct dispc_scaling_params scaling;
2156 	int ret;
2157 
2158 	if (dispc_fourcc_is_yuv(fourcc)) {
2159 		if (!dispc_find_csc(state->color_encoding,
2160 				    state->color_range)) {
2161 			dev_dbg(dispc->dev,
2162 				"%s: Unsupported CSC (%u,%u) for HW plane %u\n",
2163 				__func__, state->color_encoding,
2164 				state->color_range, hw_plane);
2165 			return -EINVAL;
2166 		}
2167 	}
2168 
2169 	if (need_scaling) {
2170 		if (lite) {
2171 			dev_dbg(dispc->dev,
2172 				"%s: Lite plane %u can't scale %ux%u!=%ux%u\n",
2173 				__func__, hw_plane,
2174 				state->src_w >> 16, state->src_h >> 16,
2175 				state->crtc_w, state->crtc_h);
2176 			return -EINVAL;
2177 		}
2178 		ret = dispc_vid_calc_scaling(dispc, state, &scaling, false);
2179 		if (ret)
2180 			return ret;
2181 	}
2182 
2183 	return 0;
2184 }
2185 
2186 static
2187 dma_addr_t dispc_plane_state_dma_addr(const struct drm_plane_state *state)
2188 {
2189 	struct drm_framebuffer *fb = state->fb;
2190 	struct drm_gem_dma_object *gem;
2191 	u32 x = state->src_x >> 16;
2192 	u32 y = state->src_y >> 16;
2193 
2194 	gem = drm_fb_dma_get_gem_obj(state->fb, 0);
2195 
2196 	return gem->dma_addr + fb->offsets[0] + x * fb->format->cpp[0] +
2197 		y * fb->pitches[0];
2198 }
2199 
2200 static
2201 dma_addr_t dispc_plane_state_p_uv_addr(const struct drm_plane_state *state)
2202 {
2203 	struct drm_framebuffer *fb = state->fb;
2204 	struct drm_gem_dma_object *gem;
2205 	u32 x = state->src_x >> 16;
2206 	u32 y = state->src_y >> 16;
2207 
2208 	if (WARN_ON(state->fb->format->num_planes != 2))
2209 		return 0;
2210 
2211 	gem = drm_fb_dma_get_gem_obj(fb, 1);
2212 
2213 	return gem->dma_addr + fb->offsets[1] +
2214 		(x * fb->format->cpp[1] / fb->format->hsub) +
2215 		(y * fb->pitches[1] / fb->format->vsub);
2216 }
2217 
2218 void dispc_plane_setup(struct dispc_device *dispc, u32 hw_plane,
2219 		       const struct drm_plane_state *state,
2220 		       u32 hw_videoport)
2221 {
2222 	bool lite = dispc->feat->vid_info[hw_plane].is_lite;
2223 	u32 fourcc = state->fb->format->format;
2224 	u16 cpp = state->fb->format->cpp[0];
2225 	u32 fb_width = state->fb->pitches[0] / cpp;
2226 	dma_addr_t dma_addr = dispc_plane_state_dma_addr(state);
2227 	struct dispc_scaling_params scale;
2228 
2229 	dispc_vid_calc_scaling(dispc, state, &scale, lite);
2230 
2231 	dispc_plane_set_pixel_format(dispc, hw_plane, fourcc);
2232 
2233 	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_0, dma_addr & 0xffffffff);
2234 	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_EXT_0, (u64)dma_addr >> 32);
2235 	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_1, dma_addr & 0xffffffff);
2236 	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_EXT_1, (u64)dma_addr >> 32);
2237 
2238 	dispc_vid_write(dispc, hw_plane, DISPC_VID_PICTURE_SIZE,
2239 			FIELD_PREP(DISPC_VID_PICTURE_SIZE_MEMSIZEY_MASK, scale.in_h - 1) |
2240 			FIELD_PREP(DISPC_VID_PICTURE_SIZE_MEMSIZEX_MASK, scale.in_w - 1));
2241 
2242 	/* For YUV422 format we use the macropixel size for pixel inc */
2243 	if (fourcc == DRM_FORMAT_YUYV || fourcc == DRM_FORMAT_UYVY)
2244 		dispc_vid_write(dispc, hw_plane, DISPC_VID_PIXEL_INC,
2245 				pixinc(scale.xinc, cpp * 2));
2246 	else
2247 		dispc_vid_write(dispc, hw_plane, DISPC_VID_PIXEL_INC,
2248 				pixinc(scale.xinc, cpp));
2249 
2250 	dispc_vid_write(dispc, hw_plane, DISPC_VID_ROW_INC,
2251 			pixinc(1 + (scale.yinc * fb_width -
2252 				    scale.xinc * scale.in_w),
2253 			       cpp));
2254 
2255 	if (state->fb->format->num_planes == 2) {
2256 		u16 cpp_uv = state->fb->format->cpp[1];
2257 		u32 fb_width_uv = state->fb->pitches[1] / cpp_uv;
2258 		dma_addr_t p_uv_addr = dispc_plane_state_p_uv_addr(state);
2259 
2260 		dispc_vid_write(dispc, hw_plane,
2261 				DISPC_VID_BA_UV_0, p_uv_addr & 0xffffffff);
2262 		dispc_vid_write(dispc, hw_plane,
2263 				DISPC_VID_BA_UV_EXT_0, (u64)p_uv_addr >> 32);
2264 		dispc_vid_write(dispc, hw_plane,
2265 				DISPC_VID_BA_UV_1, p_uv_addr & 0xffffffff);
2266 		dispc_vid_write(dispc, hw_plane,
2267 				DISPC_VID_BA_UV_EXT_1, (u64)p_uv_addr >> 32);
2268 
2269 		dispc_vid_write(dispc, hw_plane, DISPC_VID_ROW_INC_UV,
2270 				pixinc(1 + (scale.yinc * fb_width_uv -
2271 					    scale.xinc * scale.in_w_uv),
2272 				       cpp_uv));
2273 	}
2274 
2275 	if (!lite) {
2276 		dispc_vid_write(dispc, hw_plane, DISPC_VID_SIZE,
2277 				FIELD_PREP(DISPC_VID_SIZE_SIZEY_MASK,
2278 					   state->crtc_h - 1) |
2279 				FIELD_PREP(DISPC_VID_SIZE_SIZEX_MASK,
2280 					   state->crtc_w - 1));
2281 
2282 		dispc_vid_set_scaling(dispc, hw_plane, &scale, fourcc);
2283 	}
2284 
2285 	/* enable YUV->RGB color conversion */
2286 	if (dispc_fourcc_is_yuv(fourcc)) {
2287 		dispc_vid_csc_setup(dispc, hw_plane, state);
2288 		dispc_vid_csc_enable(dispc, hw_plane, true);
2289 	} else {
2290 		dispc_vid_csc_enable(dispc, hw_plane, false);
2291 	}
2292 
2293 	dispc_vid_write(dispc, hw_plane, DISPC_VID_GLOBAL_ALPHA,
2294 			FIELD_PREP(DISPC_VID_GLOBAL_ALPHA_GLOBALALPHA_MASK,
2295 				   state->alpha >> 8));
2296 
2297 	if (state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
2298 		VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 1,
2299 				DISPC_VID_ATTRIBUTES_PREMULTIPLYALPHA_MASK);
2300 	else
2301 		VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 0,
2302 				DISPC_VID_ATTRIBUTES_PREMULTIPLYALPHA_MASK);
2303 }
2304 
2305 void dispc_plane_enable(struct dispc_device *dispc, u32 hw_plane, bool enable)
2306 {
2307 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, !!enable,
2308 			DISPC_VID_ATTRIBUTES_ENABLE_MASK);
2309 }
2310 
2311 static u32 dispc_vid_get_fifo_size(struct dispc_device *dispc, u32 hw_plane)
2312 {
2313 	return VID_REG_GET(dispc, hw_plane, DISPC_VID_BUF_SIZE_STATUS,
2314 			   DISPC_VID_BUF_SIZE_STATUS_BUFSIZE_MASK);
2315 }
2316 
2317 static void dispc_vid_set_mflag_threshold(struct dispc_device *dispc,
2318 					  u32 hw_plane, u32 low, u32 high)
2319 {
2320 	dispc_vid_write(dispc, hw_plane, DISPC_VID_MFLAG_THRESHOLD,
2321 			FIELD_PREP(DISPC_VID_MFLAG_THRESHOLD_HT_MFLAG_MASK, high) |
2322 			FIELD_PREP(DISPC_VID_MFLAG_THRESHOLD_LT_MFLAG_MASK, low));
2323 }
2324 
2325 static void dispc_vid_set_buf_threshold(struct dispc_device *dispc,
2326 					u32 hw_plane, u32 low, u32 high)
2327 {
2328 	dispc_vid_write(dispc, hw_plane, DISPC_VID_BUF_THRESHOLD,
2329 			FIELD_PREP(DISPC_VID_BUF_THRESHOLD_BUFHIGHTHRESHOLD_MASK,
2330 				   high) |
2331 			FIELD_PREP(DISPC_VID_BUF_THRESHOLD_BUFLOWTHRESHOLD_MASK,
2332 				   low));
2333 }
2334 
2335 static void dispc_k2g_plane_init(struct dispc_device *dispc)
2336 {
2337 	unsigned int hw_plane;
2338 
2339 	dev_dbg(dispc->dev, "%s()\n", __func__);
2340 
2341 	/* MFLAG_CTRL = ENABLED */
2342 	REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 2,
2343 		    DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_CTRL_MASK);
2344 	/* MFLAG_START = MFLAGNORMALSTARTMODE */
2345 	REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 0,
2346 		    DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_START_MASK);
2347 
2348 	for (hw_plane = 0; hw_plane < dispc->feat->num_vids; hw_plane++) {
2349 		u32 size = dispc_vid_get_fifo_size(dispc, hw_plane);
2350 		u32 thr_low, thr_high;
2351 		u32 mflag_low, mflag_high;
2352 		u32 preload;
2353 
2354 		thr_high = size - 1;
2355 		thr_low = size / 2;
2356 
2357 		mflag_high = size * 2 / 3;
2358 		mflag_low = size / 3;
2359 
2360 		preload = thr_low;
2361 
2362 		dev_dbg(dispc->dev,
2363 			"%s: bufsize %u, buf_threshold %u/%u, mflag threshold %u/%u preload %u\n",
2364 			dispc->feat->vid_info[hw_plane].name,
2365 			size,
2366 			thr_high, thr_low,
2367 			mflag_high, mflag_low,
2368 			preload);
2369 
2370 		dispc_vid_set_buf_threshold(dispc, hw_plane,
2371 					    thr_low, thr_high);
2372 		dispc_vid_set_mflag_threshold(dispc, hw_plane,
2373 					      mflag_low, mflag_high);
2374 
2375 		dispc_vid_write(dispc, hw_plane, DISPC_VID_PRELOAD, preload);
2376 
2377 		/*
2378 		 * Prefetch up to fifo high-threshold value to minimize the
2379 		 * possibility of underflows. Note that this means the PRELOAD
2380 		 * register is ignored.
2381 		 */
2382 		VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 1,
2383 				DISPC_VID_ATTRIBUTES_BUFPRELOAD_MASK);
2384 	}
2385 }
2386 
2387 static void dispc_k3_plane_init(struct dispc_device *dispc)
2388 {
2389 	unsigned int hw_plane;
2390 	u32 cba_lo_pri = 1;
2391 	u32 cba_hi_pri = 0;
2392 
2393 	dev_dbg(dispc->dev, "%s()\n", __func__);
2394 
2395 	REG_FLD_MOD(dispc, DSS_CBA_CFG, cba_lo_pri, DSS_CBA_CFG_PRI_LO_MASK);
2396 	REG_FLD_MOD(dispc, DSS_CBA_CFG, cba_hi_pri, DSS_CBA_CFG_PRI_HI_MASK);
2397 
2398 	/* MFLAG_CTRL = ENABLED */
2399 	REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 2,
2400 		    DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_CTRL_MASK);
2401 	/* MFLAG_START = MFLAGNORMALSTARTMODE */
2402 	REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 0,
2403 		    DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_START_MASK);
2404 
2405 	for (hw_plane = 0; hw_plane < dispc->feat->num_vids; hw_plane++) {
2406 		u32 size = dispc_vid_get_fifo_size(dispc, hw_plane);
2407 		u32 thr_low, thr_high;
2408 		u32 mflag_low, mflag_high;
2409 		u32 preload;
2410 
2411 		thr_high = size - 1;
2412 		thr_low = size / 2;
2413 
2414 		mflag_high = size * 2 / 3;
2415 		mflag_low = size / 3;
2416 
2417 		preload = thr_low;
2418 
2419 		dev_dbg(dispc->dev,
2420 			"%s: bufsize %u, buf_threshold %u/%u, mflag threshold %u/%u preload %u\n",
2421 			dispc->feat->vid_info[hw_plane].name,
2422 			size,
2423 			thr_high, thr_low,
2424 			mflag_high, mflag_low,
2425 			preload);
2426 
2427 		dispc_vid_set_buf_threshold(dispc, hw_plane,
2428 					    thr_low, thr_high);
2429 		dispc_vid_set_mflag_threshold(dispc, hw_plane,
2430 					      mflag_low, mflag_high);
2431 
2432 		dispc_vid_write(dispc, hw_plane, DISPC_VID_PRELOAD, preload);
2433 
2434 		/* Prefech up to PRELOAD value */
2435 		VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 0,
2436 				DISPC_VID_ATTRIBUTES_BUFPRELOAD_MASK);
2437 	}
2438 }
2439 
2440 static void dispc_plane_init(struct dispc_device *dispc)
2441 {
2442 	switch (dispc->feat->subrev) {
2443 	case DISPC_K2G:
2444 		dispc_k2g_plane_init(dispc);
2445 		break;
2446 	case DISPC_AM625:
2447 	case DISPC_AM62A7:
2448 	case DISPC_AM62L:
2449 	case DISPC_AM65X:
2450 	case DISPC_J721E:
2451 		dispc_k3_plane_init(dispc);
2452 		break;
2453 	default:
2454 		WARN_ON(1);
2455 	}
2456 }
2457 
2458 static void dispc_vp_init(struct dispc_device *dispc)
2459 {
2460 	unsigned int i;
2461 
2462 	dev_dbg(dispc->dev, "%s()\n", __func__);
2463 
2464 	/* Enable the gamma Shadow bit-field for all VPs*/
2465 	for (i = 0; i < dispc->feat->num_vps; i++)
2466 		VP_REG_FLD_MOD(dispc, i, DISPC_VP_CONFIG, 1,
2467 			       DISPC_VP_CONFIG_GAMMAENABLE_MASK);
2468 }
2469 
2470 static void dispc_initial_config(struct dispc_device *dispc)
2471 {
2472 	dispc_plane_init(dispc);
2473 	dispc_vp_init(dispc);
2474 
2475 	/* Note: Hardcoded DPI routing on J721E for now */
2476 	if (dispc->feat->subrev == DISPC_J721E) {
2477 		dispc_write(dispc, DISPC_CONNECTIONS,
2478 			    FIELD_PREP(DISPC_CONNECTIONS_DPI_0_CONN_MASK, 2) |		/* VP1 to DPI0 */
2479 			    FIELD_PREP(DISPC_CONNECTIONS_DPI_1_CONN_MASK, 8)		/* VP3 to DPI1 */
2480 			);
2481 	}
2482 }
2483 
2484 static void dispc_k2g_vp_write_gamma_table(struct dispc_device *dispc,
2485 					   u32 hw_videoport)
2486 {
2487 	u32 *table = dispc->vp_data[hw_videoport].gamma_table;
2488 	u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
2489 	unsigned int i;
2490 
2491 	dev_dbg(dispc->dev, "%s: hw_videoport %d\n", __func__, hw_videoport);
2492 
2493 	if (WARN_ON(dispc->feat->vp_feat.color.gamma_type != TIDSS_GAMMA_8BIT))
2494 		return;
2495 
2496 	for (i = 0; i < hwlen; ++i) {
2497 		u32 v = table[i];
2498 
2499 		v |= i << 24;
2500 
2501 		dispc_vp_write(dispc, hw_videoport, DISPC_VP_K2G_GAMMA_TABLE,
2502 			       v);
2503 	}
2504 }
2505 
2506 static void dispc_am65x_vp_write_gamma_table(struct dispc_device *dispc,
2507 					     u32 hw_videoport)
2508 {
2509 	u32 *table = dispc->vp_data[hw_videoport].gamma_table;
2510 	u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
2511 	unsigned int i;
2512 
2513 	dev_dbg(dispc->dev, "%s: hw_videoport %d\n", __func__, hw_videoport);
2514 
2515 	if (WARN_ON(dispc->feat->vp_feat.color.gamma_type != TIDSS_GAMMA_8BIT))
2516 		return;
2517 
2518 	for (i = 0; i < hwlen; ++i) {
2519 		u32 v = table[i];
2520 
2521 		v |= i << 24;
2522 
2523 		dispc_vp_write(dispc, hw_videoport, DISPC_VP_GAMMA_TABLE, v);
2524 	}
2525 }
2526 
2527 static void dispc_j721e_vp_write_gamma_table(struct dispc_device *dispc,
2528 					     u32 hw_videoport)
2529 {
2530 	u32 *table = dispc->vp_data[hw_videoport].gamma_table;
2531 	u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
2532 	unsigned int i;
2533 
2534 	dev_dbg(dispc->dev, "%s: hw_videoport %d\n", __func__, hw_videoport);
2535 
2536 	if (WARN_ON(dispc->feat->vp_feat.color.gamma_type != TIDSS_GAMMA_10BIT))
2537 		return;
2538 
2539 	for (i = 0; i < hwlen; ++i) {
2540 		u32 v = table[i];
2541 
2542 		if (i == 0)
2543 			v |= 1 << 31;
2544 
2545 		dispc_vp_write(dispc, hw_videoport, DISPC_VP_GAMMA_TABLE, v);
2546 	}
2547 }
2548 
2549 static void dispc_vp_write_gamma_table(struct dispc_device *dispc,
2550 				       u32 hw_videoport)
2551 {
2552 	switch (dispc->feat->subrev) {
2553 	case DISPC_K2G:
2554 		dispc_k2g_vp_write_gamma_table(dispc, hw_videoport);
2555 		break;
2556 	case DISPC_AM625:
2557 	case DISPC_AM62A7:
2558 	case DISPC_AM62L:
2559 	case DISPC_AM65X:
2560 		dispc_am65x_vp_write_gamma_table(dispc, hw_videoport);
2561 		break;
2562 	case DISPC_J721E:
2563 		dispc_j721e_vp_write_gamma_table(dispc, hw_videoport);
2564 		break;
2565 	default:
2566 		WARN_ON(1);
2567 		break;
2568 	}
2569 }
2570 
2571 static const struct drm_color_lut dispc_vp_gamma_default_lut[] = {
2572 	{ .red = 0, .green = 0, .blue = 0, },
2573 	{ .red = U16_MAX, .green = U16_MAX, .blue = U16_MAX, },
2574 };
2575 
2576 static void dispc_vp_set_gamma(struct dispc_device *dispc,
2577 			       u32 hw_videoport,
2578 			       const struct drm_color_lut *lut,
2579 			       unsigned int length)
2580 {
2581 	u32 *table = dispc->vp_data[hw_videoport].gamma_table;
2582 	u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
2583 	u32 hwbits;
2584 	unsigned int i;
2585 
2586 	dev_dbg(dispc->dev, "%s: hw_videoport %d, lut len %u, hw len %u\n",
2587 		__func__, hw_videoport, length, hwlen);
2588 
2589 	if (dispc->feat->vp_feat.color.gamma_type == TIDSS_GAMMA_10BIT)
2590 		hwbits = 10;
2591 	else
2592 		hwbits = 8;
2593 
2594 	if (!lut || length < 2) {
2595 		lut = dispc_vp_gamma_default_lut;
2596 		length = ARRAY_SIZE(dispc_vp_gamma_default_lut);
2597 	}
2598 
2599 	for (i = 0; i < length - 1; ++i) {
2600 		unsigned int first = i * (hwlen - 1) / (length - 1);
2601 		unsigned int last = (i + 1) * (hwlen - 1) / (length - 1);
2602 		unsigned int w = last - first;
2603 		u16 r, g, b;
2604 		unsigned int j;
2605 
2606 		if (w == 0)
2607 			continue;
2608 
2609 		for (j = 0; j <= w; j++) {
2610 			r = (lut[i].red * (w - j) + lut[i + 1].red * j) / w;
2611 			g = (lut[i].green * (w - j) + lut[i + 1].green * j) / w;
2612 			b = (lut[i].blue * (w - j) + lut[i + 1].blue * j) / w;
2613 
2614 			r >>= 16 - hwbits;
2615 			g >>= 16 - hwbits;
2616 			b >>= 16 - hwbits;
2617 
2618 			table[first + j] = (r << (hwbits * 2)) |
2619 				(g << hwbits) | b;
2620 		}
2621 	}
2622 
2623 	dispc_vp_write_gamma_table(dispc, hw_videoport);
2624 }
2625 
2626 static s16 dispc_S31_32_to_s2_8(s64 coef)
2627 {
2628 	u64 sign_bit = 1ULL << 63;
2629 	u64 cbits = (u64)coef;
2630 	s16 ret;
2631 
2632 	if (cbits & sign_bit)
2633 		ret = -clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x200);
2634 	else
2635 		ret = clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x1FF);
2636 
2637 	return ret;
2638 }
2639 
2640 static void dispc_k2g_cpr_from_ctm(const struct drm_color_ctm *ctm,
2641 				   struct dispc_csc_coef *cpr)
2642 {
2643 	memset(cpr, 0, sizeof(*cpr));
2644 
2645 	cpr->to_regval = dispc_csc_cpr_regval;
2646 	cpr->m[CSC_RR] = dispc_S31_32_to_s2_8(ctm->matrix[0]);
2647 	cpr->m[CSC_RG] = dispc_S31_32_to_s2_8(ctm->matrix[1]);
2648 	cpr->m[CSC_RB] = dispc_S31_32_to_s2_8(ctm->matrix[2]);
2649 	cpr->m[CSC_GR] = dispc_S31_32_to_s2_8(ctm->matrix[3]);
2650 	cpr->m[CSC_GG] = dispc_S31_32_to_s2_8(ctm->matrix[4]);
2651 	cpr->m[CSC_GB] = dispc_S31_32_to_s2_8(ctm->matrix[5]);
2652 	cpr->m[CSC_BR] = dispc_S31_32_to_s2_8(ctm->matrix[6]);
2653 	cpr->m[CSC_BG] = dispc_S31_32_to_s2_8(ctm->matrix[7]);
2654 	cpr->m[CSC_BB] = dispc_S31_32_to_s2_8(ctm->matrix[8]);
2655 }
2656 
2657 #define CVAL(xR, xG, xB) (FIELD_PREP(GENMASK(9, 0), xR) | FIELD_PREP(GENMASK(20, 11), xG) |	\
2658 			  FIELD_PREP(GENMASK(31, 22), xB))
2659 
2660 static void dispc_k2g_vp_csc_cpr_regval(const struct dispc_csc_coef *csc,
2661 					u32 *regval)
2662 {
2663 	regval[0] = CVAL(csc->m[CSC_BB], csc->m[CSC_BG], csc->m[CSC_BR]);
2664 	regval[1] = CVAL(csc->m[CSC_GB], csc->m[CSC_GG], csc->m[CSC_GR]);
2665 	regval[2] = CVAL(csc->m[CSC_RB], csc->m[CSC_RG], csc->m[CSC_RR]);
2666 }
2667 
2668 #undef CVAL
2669 
2670 static void dispc_k2g_vp_write_csc(struct dispc_device *dispc, u32 hw_videoport,
2671 				   const struct dispc_csc_coef *csc)
2672 {
2673 	static const u16 dispc_vp_cpr_coef_reg[] = {
2674 		DISPC_VP_CSC_COEF0, DISPC_VP_CSC_COEF1, DISPC_VP_CSC_COEF2,
2675 		/* K2G CPR is packed to three registers. */
2676 	};
2677 	u32 regval[DISPC_CSC_REGVAL_LEN];
2678 	unsigned int i;
2679 
2680 	dispc_k2g_vp_csc_cpr_regval(csc, regval);
2681 
2682 	for (i = 0; i < ARRAY_SIZE(dispc_vp_cpr_coef_reg); i++)
2683 		dispc_vp_write(dispc, hw_videoport, dispc_vp_cpr_coef_reg[i],
2684 			       regval[i]);
2685 }
2686 
2687 static void dispc_k2g_vp_set_ctm(struct dispc_device *dispc, u32 hw_videoport,
2688 				 struct drm_color_ctm *ctm)
2689 {
2690 	u32 cprenable = 0;
2691 
2692 	if (ctm) {
2693 		struct dispc_csc_coef cpr;
2694 
2695 		dispc_k2g_cpr_from_ctm(ctm, &cpr);
2696 		dispc_k2g_vp_write_csc(dispc, hw_videoport, &cpr);
2697 		cprenable = 1;
2698 	}
2699 
2700 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONFIG, cprenable,
2701 		       DISPC_VP_CONFIG_CPR_MASK);
2702 }
2703 
2704 static s16 dispc_S31_32_to_s3_8(s64 coef)
2705 {
2706 	u64 sign_bit = 1ULL << 63;
2707 	u64 cbits = (u64)coef;
2708 	s16 ret;
2709 
2710 	if (cbits & sign_bit)
2711 		ret = -clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x400);
2712 	else
2713 		ret = clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x3FF);
2714 
2715 	return ret;
2716 }
2717 
2718 static void dispc_csc_from_ctm(const struct drm_color_ctm *ctm,
2719 			       struct dispc_csc_coef *cpr)
2720 {
2721 	memset(cpr, 0, sizeof(*cpr));
2722 
2723 	cpr->to_regval = dispc_csc_cpr_regval;
2724 	cpr->m[CSC_RR] = dispc_S31_32_to_s3_8(ctm->matrix[0]);
2725 	cpr->m[CSC_RG] = dispc_S31_32_to_s3_8(ctm->matrix[1]);
2726 	cpr->m[CSC_RB] = dispc_S31_32_to_s3_8(ctm->matrix[2]);
2727 	cpr->m[CSC_GR] = dispc_S31_32_to_s3_8(ctm->matrix[3]);
2728 	cpr->m[CSC_GG] = dispc_S31_32_to_s3_8(ctm->matrix[4]);
2729 	cpr->m[CSC_GB] = dispc_S31_32_to_s3_8(ctm->matrix[5]);
2730 	cpr->m[CSC_BR] = dispc_S31_32_to_s3_8(ctm->matrix[6]);
2731 	cpr->m[CSC_BG] = dispc_S31_32_to_s3_8(ctm->matrix[7]);
2732 	cpr->m[CSC_BB] = dispc_S31_32_to_s3_8(ctm->matrix[8]);
2733 }
2734 
2735 static void dispc_k3_vp_write_csc(struct dispc_device *dispc, u32 hw_videoport,
2736 				  const struct dispc_csc_coef *csc)
2737 {
2738 	static const u16 dispc_vp_csc_coef_reg[DISPC_CSC_REGVAL_LEN] = {
2739 		DISPC_VP_CSC_COEF0, DISPC_VP_CSC_COEF1, DISPC_VP_CSC_COEF2,
2740 		DISPC_VP_CSC_COEF3, DISPC_VP_CSC_COEF4, DISPC_VP_CSC_COEF5,
2741 		DISPC_VP_CSC_COEF6, DISPC_VP_CSC_COEF7,
2742 	};
2743 	u32 regval[DISPC_CSC_REGVAL_LEN];
2744 	unsigned int i;
2745 
2746 	csc->to_regval(csc, regval);
2747 
2748 	for (i = 0; i < ARRAY_SIZE(regval); i++)
2749 		dispc_vp_write(dispc, hw_videoport, dispc_vp_csc_coef_reg[i],
2750 			       regval[i]);
2751 }
2752 
2753 static void dispc_k3_vp_set_ctm(struct dispc_device *dispc, u32 hw_videoport,
2754 				struct drm_color_ctm *ctm)
2755 {
2756 	u32 colorconvenable = 0;
2757 
2758 	if (ctm) {
2759 		struct dispc_csc_coef csc;
2760 
2761 		dispc_csc_from_ctm(ctm, &csc);
2762 		dispc_k3_vp_write_csc(dispc, hw_videoport, &csc);
2763 		colorconvenable = 1;
2764 	}
2765 
2766 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONFIG, colorconvenable,
2767 		       DISPC_VP_CONFIG_COLORCONVENABLE_MASK);
2768 }
2769 
2770 static void dispc_vp_set_color_mgmt(struct dispc_device *dispc,
2771 				    u32 hw_videoport,
2772 				    const struct drm_crtc_state *state,
2773 				    bool newmodeset)
2774 {
2775 	struct drm_color_lut *lut = NULL;
2776 	struct drm_color_ctm *ctm = NULL;
2777 	unsigned int length = 0;
2778 
2779 	if (!(state->color_mgmt_changed || newmodeset))
2780 		return;
2781 
2782 	if (state->gamma_lut) {
2783 		lut = (struct drm_color_lut *)state->gamma_lut->data;
2784 		length = state->gamma_lut->length / sizeof(*lut);
2785 	}
2786 
2787 	dispc_vp_set_gamma(dispc, hw_videoport, lut, length);
2788 
2789 	if (state->ctm)
2790 		ctm = (struct drm_color_ctm *)state->ctm->data;
2791 
2792 	if (dispc->feat->subrev == DISPC_K2G)
2793 		dispc_k2g_vp_set_ctm(dispc, hw_videoport, ctm);
2794 	else
2795 		dispc_k3_vp_set_ctm(dispc, hw_videoport, ctm);
2796 }
2797 
2798 void dispc_vp_setup(struct dispc_device *dispc, u32 hw_videoport,
2799 		    const struct drm_crtc_state *state, bool newmodeset)
2800 {
2801 	dispc_vp_set_default_color(dispc, hw_videoport, 0);
2802 	dispc_vp_set_color_mgmt(dispc, hw_videoport, state, newmodeset);
2803 }
2804 
2805 int dispc_runtime_suspend(struct dispc_device *dispc)
2806 {
2807 	dev_dbg(dispc->dev, "suspend\n");
2808 
2809 	dispc->is_enabled = false;
2810 
2811 	clk_disable_unprepare(dispc->fclk);
2812 
2813 	return 0;
2814 }
2815 
2816 int dispc_runtime_resume(struct dispc_device *dispc)
2817 {
2818 	dev_dbg(dispc->dev, "resume\n");
2819 
2820 	clk_prepare_enable(dispc->fclk);
2821 
2822 	if (REG_GET(dispc, DSS_SYSSTATUS, DSS_SYSSTATUS_DISPC_FUNC_RESETDONE) == 0)
2823 		dev_warn(dispc->dev, "DSS FUNC RESET not done!\n");
2824 
2825 	dev_dbg(dispc->dev, "OMAP DSS7 rev 0x%x\n",
2826 		dispc_read(dispc, DSS_REVISION));
2827 
2828 	dev_dbg(dispc->dev, "VP RESETDONE %d,%d,%d\n",
2829 		REG_GET(dispc, DSS_SYSSTATUS, GENMASK(1, 1)),
2830 		REG_GET(dispc, DSS_SYSSTATUS, GENMASK(2, 2)),
2831 		REG_GET(dispc, DSS_SYSSTATUS, GENMASK(3, 3)));
2832 
2833 	if (dispc->feat->subrev == DISPC_AM625 ||
2834 	    dispc->feat->subrev == DISPC_AM65X)
2835 		dev_dbg(dispc->dev, "OLDI RESETDONE %d,%d,%d\n",
2836 			REG_GET(dispc, DSS_SYSSTATUS, GENMASK(5, 5)),
2837 			REG_GET(dispc, DSS_SYSSTATUS, GENMASK(6, 6)),
2838 			REG_GET(dispc, DSS_SYSSTATUS, GENMASK(7, 7)));
2839 
2840 	dev_dbg(dispc->dev, "DISPC IDLE %d\n",
2841 		REG_GET(dispc, DSS_SYSSTATUS, DSS_SYSSTATUS_DISPC_IDLE_STATUS));
2842 
2843 	dispc_initial_config(dispc);
2844 
2845 	dispc->is_enabled = true;
2846 
2847 	tidss_irq_resume(dispc->tidss);
2848 
2849 	return 0;
2850 }
2851 
2852 void dispc_remove(struct tidss_device *tidss)
2853 {
2854 	tidss->dispc = NULL;
2855 }
2856 
2857 static int dispc_iomap_resource(struct platform_device *pdev, const char *name,
2858 				void __iomem **base)
2859 {
2860 	void __iomem *b;
2861 
2862 	b = devm_platform_ioremap_resource_byname(pdev, name);
2863 	if (IS_ERR(b)) {
2864 		dev_err(&pdev->dev, "cannot ioremap resource '%s'\n", name);
2865 		return PTR_ERR(b);
2866 	}
2867 
2868 	*base = b;
2869 
2870 	return 0;
2871 }
2872 
2873 static int dispc_init_am65x_oldi_io_ctrl(struct device *dev,
2874 					 struct dispc_device *dispc)
2875 {
2876 	dispc->am65x_oldi_io_ctrl =
2877 		syscon_regmap_lookup_by_phandle(dev->of_node,
2878 						"ti,am65x-oldi-io-ctrl");
2879 	if (PTR_ERR(dispc->am65x_oldi_io_ctrl) == -ENODEV) {
2880 		dispc->am65x_oldi_io_ctrl = NULL;
2881 	} else if (IS_ERR(dispc->am65x_oldi_io_ctrl)) {
2882 		dev_err(dev, "%s: syscon_regmap_lookup_by_phandle failed %ld\n",
2883 			__func__, PTR_ERR(dispc->am65x_oldi_io_ctrl));
2884 		return PTR_ERR(dispc->am65x_oldi_io_ctrl);
2885 	}
2886 	return 0;
2887 }
2888 
2889 static void dispc_init_errata(struct dispc_device *dispc)
2890 {
2891 	static const struct soc_device_attribute am65x_sr10_soc_devices[] = {
2892 		{ .family = "AM65X", .revision = "SR1.0" },
2893 		{ /* sentinel */ }
2894 	};
2895 
2896 	if (soc_device_match(am65x_sr10_soc_devices)) {
2897 		dispc->errata.i2000 = true;
2898 		dev_info(dispc->dev, "WA for erratum i2000: YUV formats disabled\n");
2899 	}
2900 }
2901 
2902 /*
2903  * K2G display controller does not support soft reset, so we do a basic manual
2904  * reset here: make sure the IRQs are masked and VPs are disabled.
2905  */
2906 static void dispc_softreset_k2g(struct dispc_device *dispc)
2907 {
2908 	unsigned long flags;
2909 
2910 	spin_lock_irqsave(&dispc->tidss->irq_lock, flags);
2911 	dispc_set_irqenable(dispc, 0);
2912 	dispc_read_and_clear_irqstatus(dispc);
2913 	spin_unlock_irqrestore(&dispc->tidss->irq_lock, flags);
2914 
2915 	for (unsigned int vp_idx = 0; vp_idx < dispc->feat->num_vps; ++vp_idx)
2916 		VP_REG_FLD_MOD(dispc, vp_idx, DISPC_VP_CONTROL, 0,
2917 			       DISPC_VP_CONTROL_ENABLE_MASK);
2918 }
2919 
2920 static int dispc_softreset(struct dispc_device *dispc)
2921 {
2922 	u32 val;
2923 	int ret;
2924 
2925 	if (dispc->feat->subrev == DISPC_K2G) {
2926 		dispc_softreset_k2g(dispc);
2927 		return 0;
2928 	}
2929 
2930 	/* Soft reset */
2931 	REG_FLD_MOD(dispc, DSS_SYSCONFIG, 1, DSS_SYSCONFIG_SOFTRESET_MASK);
2932 	/* Wait for reset to complete */
2933 	ret = readl_poll_timeout(dispc->base_common + DSS_SYSSTATUS,
2934 				 val, val & 1, 100, 5000);
2935 	if (ret) {
2936 		dev_err(dispc->dev, "failed to reset dispc\n");
2937 		return ret;
2938 	}
2939 
2940 	return 0;
2941 }
2942 
2943 static int dispc_init_hw(struct dispc_device *dispc)
2944 {
2945 	struct device *dev = dispc->dev;
2946 	int ret;
2947 
2948 	ret = pm_runtime_set_active(dev);
2949 	if (ret) {
2950 		dev_err(dev, "Failed to set DSS PM to active\n");
2951 		return ret;
2952 	}
2953 
2954 	ret = clk_prepare_enable(dispc->fclk);
2955 	if (ret) {
2956 		dev_err(dev, "Failed to enable DSS fclk\n");
2957 		goto err_runtime_suspend;
2958 	}
2959 
2960 	ret = dispc_softreset(dispc);
2961 	if (ret)
2962 		goto err_clk_disable;
2963 
2964 	clk_disable_unprepare(dispc->fclk);
2965 	ret = pm_runtime_set_suspended(dev);
2966 	if (ret) {
2967 		dev_err(dev, "Failed to set DSS PM to suspended\n");
2968 		return ret;
2969 	}
2970 
2971 	return 0;
2972 
2973 err_clk_disable:
2974 	clk_disable_unprepare(dispc->fclk);
2975 
2976 err_runtime_suspend:
2977 	ret = pm_runtime_set_suspended(dev);
2978 	if (ret) {
2979 		dev_err(dev, "Failed to set DSS PM to suspended\n");
2980 		return ret;
2981 	}
2982 
2983 	return ret;
2984 }
2985 
2986 int dispc_init(struct tidss_device *tidss)
2987 {
2988 	struct device *dev = tidss->dev;
2989 	struct platform_device *pdev = to_platform_device(dev);
2990 	struct dispc_device *dispc;
2991 	const struct dispc_features *feat;
2992 	unsigned int i, num_fourccs;
2993 	int r = 0;
2994 
2995 	feat = tidss->feat;
2996 
2997 	if (feat->subrev != DISPC_K2G) {
2998 		r = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
2999 		if (r)
3000 			dev_warn(dev, "cannot set DMA masks to 48-bit\n");
3001 	}
3002 
3003 	dma_set_max_seg_size(dev, UINT_MAX);
3004 
3005 	dispc = devm_kzalloc(dev, sizeof(*dispc), GFP_KERNEL);
3006 	if (!dispc)
3007 		return -ENOMEM;
3008 
3009 	dispc->tidss = tidss;
3010 	dispc->dev = dev;
3011 	dispc->feat = feat;
3012 
3013 	dispc_init_errata(dispc);
3014 
3015 	dispc->fourccs = devm_kcalloc(dev, ARRAY_SIZE(dispc_color_formats),
3016 				      sizeof(*dispc->fourccs), GFP_KERNEL);
3017 	if (!dispc->fourccs)
3018 		return -ENOMEM;
3019 
3020 	num_fourccs = 0;
3021 	for (i = 0; i < ARRAY_SIZE(dispc_color_formats); ++i) {
3022 		if (dispc->errata.i2000 &&
3023 		    dispc_fourcc_is_yuv(dispc_color_formats[i].fourcc)) {
3024 			continue;
3025 		}
3026 		dispc->fourccs[num_fourccs++] = dispc_color_formats[i].fourcc;
3027 	}
3028 
3029 	dispc->num_fourccs = num_fourccs;
3030 
3031 	dispc_common_regmap = dispc->feat->common_regs;
3032 
3033 	r = dispc_iomap_resource(pdev, dispc->feat->common,
3034 				 &dispc->base_common);
3035 	if (r)
3036 		return r;
3037 
3038 	for (i = 0; i < dispc->feat->num_vids; i++) {
3039 		r = dispc_iomap_resource(pdev, dispc->feat->vid_info[i].name,
3040 					 &dispc->base_vid[i]);
3041 		if (r)
3042 			return r;
3043 	}
3044 
3045 	for (i = 0; i < dispc->feat->num_vps; i++) {
3046 		u32 gamma_size = dispc->feat->vp_feat.color.gamma_size;
3047 		u32 *gamma_table;
3048 		struct clk *clk;
3049 
3050 		r = dispc_iomap_resource(pdev, dispc->feat->ovr_name[i],
3051 					 &dispc->base_ovr[i]);
3052 		if (r)
3053 			return r;
3054 
3055 		r = dispc_iomap_resource(pdev, dispc->feat->vp_name[i],
3056 					 &dispc->base_vp[i]);
3057 		if (r)
3058 			return r;
3059 
3060 		clk = devm_clk_get(dev, dispc->feat->vpclk_name[i]);
3061 		if (IS_ERR(clk)) {
3062 			dev_err(dev, "%s: Failed to get clk %s:%ld\n", __func__,
3063 				dispc->feat->vpclk_name[i], PTR_ERR(clk));
3064 			return PTR_ERR(clk);
3065 		}
3066 		dispc->vp_clk[i] = clk;
3067 
3068 		gamma_table = devm_kmalloc_array(dev, gamma_size,
3069 						 sizeof(*gamma_table),
3070 						 GFP_KERNEL);
3071 		if (!gamma_table)
3072 			return -ENOMEM;
3073 		dispc->vp_data[i].gamma_table = gamma_table;
3074 	}
3075 
3076 	if (feat->subrev == DISPC_AM65X) {
3077 		r = dispc_init_am65x_oldi_io_ctrl(dev, dispc);
3078 		if (r)
3079 			return r;
3080 	}
3081 
3082 	dispc->fclk = devm_clk_get(dev, "fck");
3083 	if (IS_ERR(dispc->fclk)) {
3084 		dev_err(dev, "%s: Failed to get fclk: %ld\n",
3085 			__func__, PTR_ERR(dispc->fclk));
3086 		return PTR_ERR(dispc->fclk);
3087 	}
3088 	dev_dbg(dev, "DSS fclk %lu Hz\n", clk_get_rate(dispc->fclk));
3089 
3090 	of_property_read_u32(dispc->dev->of_node, "max-memory-bandwidth",
3091 			     &dispc->memory_bandwidth_limit);
3092 
3093 	r = dispc_init_hw(dispc);
3094 	if (r)
3095 		return r;
3096 
3097 	tidss->dispc = dispc;
3098 
3099 	return 0;
3100 }
3101