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