xref: /linux/drivers/gpu/drm/tidss/tidss_dispc.c (revision 74ba587f402d5501af2c85e50cf1e4044263b6ca)
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 	const struct drm_display_mode *mode = &state->adjusted_mode;
1168 	bool align, onoff, rf, ieo, ipc, ihs, ivs;
1169 	u32 hsw, hfp, hbp, vsw, vfp, vbp;
1170 
1171 	fmt = dispc_vp_find_bus_fmt(dispc, hw_videoport, tstate->bus_format,
1172 				    tstate->bus_flags);
1173 
1174 	if (WARN_ON(!fmt))
1175 		return;
1176 
1177 	if (dispc->feat->vp_bus_type[hw_videoport] == DISPC_VP_OLDI_AM65X) {
1178 		dispc_am65x_oldi_tx_power(dispc, true);
1179 
1180 		dispc_enable_am65x_oldi(dispc, hw_videoport, fmt);
1181 	}
1182 
1183 	dispc_set_num_datalines(dispc, hw_videoport, fmt->data_width);
1184 
1185 	hfp = mode->crtc_hsync_start - mode->crtc_hdisplay;
1186 	hsw = mode->crtc_hsync_end - mode->crtc_hsync_start;
1187 	hbp = mode->crtc_htotal - mode->crtc_hsync_end;
1188 
1189 	vfp = mode->crtc_vsync_start - mode->crtc_vdisplay;
1190 	vsw = mode->crtc_vsync_end - mode->crtc_vsync_start;
1191 	vbp = mode->crtc_vtotal - mode->crtc_vsync_end;
1192 
1193 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_TIMING_H,
1194 		       FIELD_PREP(DISPC_VP_TIMING_H_SYNC_PULSE_MASK, hsw - 1) |
1195 		       FIELD_PREP(DISPC_VP_TIMING_H_FRONT_PORCH_MASK, hfp - 1) |
1196 		       FIELD_PREP(DISPC_VP_TIMING_H_BACK_PORCH_MASK, hbp - 1));
1197 
1198 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_TIMING_V,
1199 		       FIELD_PREP(DISPC_VP_TIMING_V_SYNC_PULSE_MASK, vsw - 1) |
1200 		       FIELD_PREP(DISPC_VP_TIMING_V_FRONT_PORCH_MASK, vfp) |
1201 		       FIELD_PREP(DISPC_VP_TIMING_V_BACK_PORCH_MASK, vbp));
1202 
1203 	ivs = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
1204 
1205 	ihs = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
1206 
1207 	ieo = !!(tstate->bus_flags & DRM_BUS_FLAG_DE_LOW);
1208 
1209 	ipc = !!(tstate->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE);
1210 
1211 	/* always use the 'rf' setting */
1212 	onoff = true;
1213 
1214 	rf = !!(tstate->bus_flags & DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE);
1215 
1216 	/* always use aligned syncs */
1217 	align = true;
1218 
1219 	/* always use DE_HIGH for OLDI */
1220 	if (dispc->feat->vp_bus_type[hw_videoport] == DISPC_VP_OLDI_AM65X)
1221 		ieo = false;
1222 
1223 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_POL_FREQ,
1224 		       FIELD_PREP(DISPC_VP_POL_FREQ_ALIGN_MASK, align) |
1225 		       FIELD_PREP(DISPC_VP_POL_FREQ_ONOFF_MASK, onoff) |
1226 		       FIELD_PREP(DISPC_VP_POL_FREQ_RF_MASK, rf) |
1227 		       FIELD_PREP(DISPC_VP_POL_FREQ_IEO_MASK, ieo) |
1228 		       FIELD_PREP(DISPC_VP_POL_FREQ_IPC_MASK, ipc) |
1229 		       FIELD_PREP(DISPC_VP_POL_FREQ_IHS_MASK, ihs) |
1230 		       FIELD_PREP(DISPC_VP_POL_FREQ_IVS_MASK, ivs));
1231 
1232 	dispc_vp_write(dispc, hw_videoport, DISPC_VP_SIZE_SCREEN,
1233 		       FIELD_PREP(DISPC_VP_SIZE_SCREEN_HDISPLAY_MASK,
1234 				  mode->crtc_hdisplay - 1) |
1235 		       FIELD_PREP(DISPC_VP_SIZE_SCREEN_VDISPLAY_MASK,
1236 				  mode->crtc_vdisplay - 1));
1237 }
1238 
1239 void dispc_vp_enable(struct dispc_device *dispc, u32 hw_videoport)
1240 {
1241 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 1,
1242 		       DISPC_VP_CONTROL_ENABLE_MASK);
1243 }
1244 
1245 void dispc_vp_disable(struct dispc_device *dispc, u32 hw_videoport)
1246 {
1247 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 0,
1248 		       DISPC_VP_CONTROL_ENABLE_MASK);
1249 }
1250 
1251 void dispc_vp_unprepare(struct dispc_device *dispc, u32 hw_videoport)
1252 {
1253 	if (dispc->feat->vp_bus_type[hw_videoport] == DISPC_VP_OLDI_AM65X) {
1254 		dispc_vp_write(dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG, 0);
1255 
1256 		dispc_am65x_oldi_tx_power(dispc, false);
1257 	}
1258 }
1259 
1260 bool dispc_vp_go_busy(struct dispc_device *dispc, u32 hw_videoport)
1261 {
1262 	return VP_REG_GET(dispc, hw_videoport, DISPC_VP_CONTROL,
1263 			  DISPC_VP_CONTROL_GOBIT_MASK);
1264 }
1265 
1266 void dispc_vp_go(struct dispc_device *dispc, u32 hw_videoport)
1267 {
1268 	WARN_ON(VP_REG_GET(dispc, hw_videoport, DISPC_VP_CONTROL,
1269 			   DISPC_VP_CONTROL_GOBIT_MASK));
1270 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 1,
1271 		       DISPC_VP_CONTROL_GOBIT_MASK);
1272 }
1273 
1274 enum c8_to_c12_mode { C8_TO_C12_REPLICATE, C8_TO_C12_MAX, C8_TO_C12_MIN };
1275 
1276 static u16 c8_to_c12(u8 c8, enum c8_to_c12_mode mode)
1277 {
1278 	u16 c12;
1279 
1280 	c12 = c8 << 4;
1281 
1282 	switch (mode) {
1283 	case C8_TO_C12_REPLICATE:
1284 		/* Copy c8 4 MSB to 4 LSB for full scale c12 */
1285 		c12 |= c8 >> 4;
1286 		break;
1287 	case C8_TO_C12_MAX:
1288 		c12 |= 0xF;
1289 		break;
1290 	default:
1291 	case C8_TO_C12_MIN:
1292 		break;
1293 	}
1294 
1295 	return c12;
1296 }
1297 
1298 static u64 argb8888_to_argb12121212(u32 argb8888, enum c8_to_c12_mode m)
1299 {
1300 	u8 a, r, g, b;
1301 	u64 v;
1302 
1303 	a = (argb8888 >> 24) & 0xff;
1304 	r = (argb8888 >> 16) & 0xff;
1305 	g = (argb8888 >> 8) & 0xff;
1306 	b = (argb8888 >> 0) & 0xff;
1307 
1308 	v = ((u64)c8_to_c12(a, m) << 36) | ((u64)c8_to_c12(r, m) << 24) |
1309 		((u64)c8_to_c12(g, m) << 12) | (u64)c8_to_c12(b, m);
1310 
1311 	return v;
1312 }
1313 
1314 static void dispc_vp_set_default_color(struct dispc_device *dispc,
1315 				       u32 hw_videoport, u32 default_color)
1316 {
1317 	u64 v;
1318 
1319 	v = argb8888_to_argb12121212(default_color, C8_TO_C12_REPLICATE);
1320 
1321 	dispc_ovr_write(dispc, hw_videoport,
1322 			DISPC_OVR_DEFAULT_COLOR, v & 0xffffffff);
1323 	dispc_ovr_write(dispc, hw_videoport,
1324 			DISPC_OVR_DEFAULT_COLOR2, (v >> 32) & 0xffff);
1325 }
1326 
1327 enum drm_mode_status dispc_vp_mode_valid(struct dispc_device *dispc,
1328 					 u32 hw_videoport,
1329 					 const struct drm_display_mode *mode)
1330 {
1331 	u32 hsw, hfp, hbp, vsw, vfp, vbp;
1332 	enum dispc_vp_bus_type bus_type;
1333 	int max_pclk;
1334 
1335 	bus_type = dispc->feat->vp_bus_type[hw_videoport];
1336 
1337 	max_pclk = dispc->feat->max_pclk_khz[bus_type];
1338 
1339 	if (WARN_ON(max_pclk == 0))
1340 		return MODE_BAD;
1341 
1342 	if (mode->clock < dispc->feat->min_pclk_khz)
1343 		return MODE_CLOCK_LOW;
1344 
1345 	if (mode->clock > max_pclk)
1346 		return MODE_CLOCK_HIGH;
1347 
1348 	if (mode->hdisplay > 4096)
1349 		return MODE_BAD;
1350 
1351 	if (mode->vdisplay > 4096)
1352 		return MODE_BAD;
1353 
1354 	/* TODO: add interlace support */
1355 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1356 		return MODE_NO_INTERLACE;
1357 
1358 	/*
1359 	 * Enforce the output width is divisible by 2. Actually this
1360 	 * is only needed in following cases:
1361 	 * - YUV output selected (BT656, BT1120)
1362 	 * - Dithering enabled
1363 	 * - TDM with TDMCycleFormat == 3
1364 	 * But for simplicity we enforce that always.
1365 	 */
1366 	if ((mode->hdisplay % 2) != 0)
1367 		return MODE_BAD_HVALUE;
1368 
1369 	hfp = mode->hsync_start - mode->hdisplay;
1370 	hsw = mode->hsync_end - mode->hsync_start;
1371 	hbp = mode->htotal - mode->hsync_end;
1372 
1373 	vfp = mode->vsync_start - mode->vdisplay;
1374 	vsw = mode->vsync_end - mode->vsync_start;
1375 	vbp = mode->vtotal - mode->vsync_end;
1376 
1377 	if (hsw < 1 || hsw > 256 ||
1378 	    hfp < 1 || hfp > 4096 ||
1379 	    hbp < 1 || hbp > 4096)
1380 		return MODE_BAD_HVALUE;
1381 
1382 	if (vsw < 1 || vsw > 256 ||
1383 	    vfp > 4095 || vbp > 4095)
1384 		return MODE_BAD_VVALUE;
1385 
1386 	if (dispc->memory_bandwidth_limit) {
1387 		const unsigned int bpp = 4;
1388 		u64 bandwidth;
1389 
1390 		bandwidth = 1000 * mode->clock;
1391 		bandwidth = bandwidth * mode->hdisplay * mode->vdisplay * bpp;
1392 		bandwidth = div_u64(bandwidth, mode->htotal * mode->vtotal);
1393 
1394 		if (dispc->memory_bandwidth_limit < bandwidth)
1395 			return MODE_BAD;
1396 	}
1397 
1398 	return MODE_OK;
1399 }
1400 
1401 int dispc_vp_enable_clk(struct dispc_device *dispc, u32 hw_videoport)
1402 {
1403 	int ret = clk_prepare_enable(dispc->vp_clk[hw_videoport]);
1404 
1405 	if (ret)
1406 		dev_err(dispc->dev, "%s: enabling clk failed: %d\n", __func__,
1407 			ret);
1408 
1409 	return ret;
1410 }
1411 
1412 void dispc_vp_disable_clk(struct dispc_device *dispc, u32 hw_videoport)
1413 {
1414 	clk_disable_unprepare(dispc->vp_clk[hw_videoport]);
1415 }
1416 
1417 /*
1418  * Calculate the percentage difference between the requested pixel clock rate
1419  * and the effective rate resulting from calculating the clock divider value.
1420  */
1421 unsigned int dispc_pclk_diff(unsigned long rate, unsigned long real_rate)
1422 {
1423 	int r = rate / 100, rr = real_rate / 100;
1424 
1425 	return (unsigned int)(abs(((rr - r) * 100) / r));
1426 }
1427 
1428 int dispc_vp_set_clk_rate(struct dispc_device *dispc, u32 hw_videoport,
1429 			  unsigned long rate)
1430 {
1431 	int r;
1432 	unsigned long new_rate;
1433 
1434 	r = clk_set_rate(dispc->vp_clk[hw_videoport], rate);
1435 	if (r) {
1436 		dev_err(dispc->dev, "vp%d: failed to set clk rate to %lu\n",
1437 			hw_videoport, rate);
1438 		return r;
1439 	}
1440 
1441 	new_rate = clk_get_rate(dispc->vp_clk[hw_videoport]);
1442 
1443 	if (dispc_pclk_diff(rate, new_rate) > 5)
1444 		dev_warn(dispc->dev,
1445 			 "vp%d: Clock rate %lu differs over 5%% from requested %lu\n",
1446 			 hw_videoport, new_rate, rate);
1447 
1448 	dev_dbg(dispc->dev, "vp%d: new rate %lu Hz (requested %lu Hz)\n",
1449 		hw_videoport, clk_get_rate(dispc->vp_clk[hw_videoport]), rate);
1450 
1451 	return 0;
1452 }
1453 
1454 /* OVR */
1455 static void dispc_k2g_ovr_set_plane(struct dispc_device *dispc,
1456 				    u32 hw_plane, u32 hw_videoport,
1457 				    u32 x, u32 y, u32 layer)
1458 {
1459 	/* On k2g there is only one plane and no need for ovr */
1460 	dispc_vid_write(dispc, hw_plane, DISPC_VID_K2G_POSITION,
1461 			x | (y << 16));
1462 }
1463 
1464 static void dispc_am65x_ovr_set_plane(struct dispc_device *dispc,
1465 				      u32 hw_plane, u32 hw_videoport,
1466 				      u32 x, u32 y, u32 layer)
1467 {
1468 	u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
1469 
1470 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer),
1471 			hw_id, DISPC_OVR_ATTRIBUTES_CHANNELIN_MASK);
1472 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer), x,
1473 			DISPC_OVR_ATTRIBUTES_POSX_MASK);
1474 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer), y,
1475 			DISPC_OVR_ATTRIBUTES_POSY_MASK);
1476 }
1477 
1478 static void dispc_j721e_ovr_set_plane(struct dispc_device *dispc,
1479 				      u32 hw_plane, u32 hw_videoport,
1480 				      u32 x, u32 y, u32 layer)
1481 {
1482 	u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
1483 
1484 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer),
1485 			hw_id, DISPC_OVR_ATTRIBUTES_CHANNELIN_MASK);
1486 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES2(layer), x,
1487 			DISPC_OVR_ATTRIBUTES2_POSX_MASK);
1488 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES2(layer), y,
1489 			DISPC_OVR_ATTRIBUTES2_POSY_MASK);
1490 }
1491 
1492 void dispc_ovr_set_plane(struct dispc_device *dispc, u32 hw_plane,
1493 			 u32 hw_videoport, u32 x, u32 y, u32 layer)
1494 {
1495 	switch (dispc->feat->subrev) {
1496 	case DISPC_K2G:
1497 		dispc_k2g_ovr_set_plane(dispc, hw_plane, hw_videoport,
1498 					x, y, layer);
1499 		break;
1500 	case DISPC_AM625:
1501 	case DISPC_AM62A7:
1502 	case DISPC_AM62L:
1503 	case DISPC_AM65X:
1504 		dispc_am65x_ovr_set_plane(dispc, hw_plane, hw_videoport,
1505 					  x, y, layer);
1506 		break;
1507 	case DISPC_J721E:
1508 		dispc_j721e_ovr_set_plane(dispc, hw_plane, hw_videoport,
1509 					  x, y, layer);
1510 		break;
1511 	default:
1512 		WARN_ON(1);
1513 		break;
1514 	}
1515 }
1516 
1517 void dispc_ovr_enable_layer(struct dispc_device *dispc,
1518 			    u32 hw_videoport, u32 layer, bool enable)
1519 {
1520 	if (dispc->feat->subrev == DISPC_K2G)
1521 		return;
1522 
1523 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer),
1524 			!!enable, DISPC_OVR_ATTRIBUTES_ENABLE_MASK);
1525 }
1526 
1527 /* CSC */
1528 enum csc_ctm {
1529 	CSC_RR, CSC_RG, CSC_RB,
1530 	CSC_GR, CSC_GG, CSC_GB,
1531 	CSC_BR, CSC_BG, CSC_BB,
1532 };
1533 
1534 enum csc_yuv2rgb {
1535 	CSC_RY, CSC_RCB, CSC_RCR,
1536 	CSC_GY, CSC_GCB, CSC_GCR,
1537 	CSC_BY, CSC_BCB, CSC_BCR,
1538 };
1539 
1540 enum csc_rgb2yuv {
1541 	CSC_YR,  CSC_YG,  CSC_YB,
1542 	CSC_CBR, CSC_CBG, CSC_CBB,
1543 	CSC_CRR, CSC_CRG, CSC_CRB,
1544 };
1545 
1546 struct dispc_csc_coef {
1547 	void (*to_regval)(const struct dispc_csc_coef *csc, u32 *regval);
1548 	int m[9];
1549 	int preoffset[3];
1550 	int postoffset[3];
1551 	enum { CLIP_LIMITED_RANGE = 0, CLIP_FULL_RANGE = 1, } cliping;
1552 	const char *name;
1553 };
1554 
1555 #define DISPC_CSC_REGVAL_LEN 8
1556 
1557 static
1558 void dispc_csc_offset_regval(const struct dispc_csc_coef *csc, u32 *regval)
1559 {
1560 #define OVAL(x, y) (FIELD_PREP(GENMASK(15, 3), x) | FIELD_PREP(GENMASK(31, 19), y))
1561 	regval[5] = OVAL(csc->preoffset[0], csc->preoffset[1]);
1562 	regval[6] = OVAL(csc->preoffset[2], csc->postoffset[0]);
1563 	regval[7] = OVAL(csc->postoffset[1], csc->postoffset[2]);
1564 #undef OVAL
1565 }
1566 
1567 #define CVAL(x, y) (FIELD_PREP(GENMASK(10, 0), x) | FIELD_PREP(GENMASK(26, 16), y))
1568 static
1569 void dispc_csc_yuv2rgb_regval(const struct dispc_csc_coef *csc, u32 *regval)
1570 {
1571 	regval[0] = CVAL(csc->m[CSC_RY], csc->m[CSC_RCR]);
1572 	regval[1] = CVAL(csc->m[CSC_RCB], csc->m[CSC_GY]);
1573 	regval[2] = CVAL(csc->m[CSC_GCR], csc->m[CSC_GCB]);
1574 	regval[3] = CVAL(csc->m[CSC_BY], csc->m[CSC_BCR]);
1575 	regval[4] = CVAL(csc->m[CSC_BCB], 0);
1576 
1577 	dispc_csc_offset_regval(csc, regval);
1578 }
1579 
1580 __maybe_unused static
1581 void dispc_csc_rgb2yuv_regval(const struct dispc_csc_coef *csc, u32 *regval)
1582 {
1583 	regval[0] = CVAL(csc->m[CSC_YR], csc->m[CSC_YG]);
1584 	regval[1] = CVAL(csc->m[CSC_YB], csc->m[CSC_CRR]);
1585 	regval[2] = CVAL(csc->m[CSC_CRG], csc->m[CSC_CRB]);
1586 	regval[3] = CVAL(csc->m[CSC_CBR], csc->m[CSC_CBG]);
1587 	regval[4] = CVAL(csc->m[CSC_CBB], 0);
1588 
1589 	dispc_csc_offset_regval(csc, regval);
1590 }
1591 
1592 static void dispc_csc_cpr_regval(const struct dispc_csc_coef *csc,
1593 				 u32 *regval)
1594 {
1595 	regval[0] = CVAL(csc->m[CSC_RR], csc->m[CSC_RG]);
1596 	regval[1] = CVAL(csc->m[CSC_RB], csc->m[CSC_GR]);
1597 	regval[2] = CVAL(csc->m[CSC_GG], csc->m[CSC_GB]);
1598 	regval[3] = CVAL(csc->m[CSC_BR], csc->m[CSC_BG]);
1599 	regval[4] = CVAL(csc->m[CSC_BB], 0);
1600 
1601 	dispc_csc_offset_regval(csc, regval);
1602 }
1603 
1604 #undef CVAL
1605 
1606 static void dispc_k2g_vid_write_csc(struct dispc_device *dispc, u32 hw_plane,
1607 				    const struct dispc_csc_coef *csc)
1608 {
1609 	static const u16 dispc_vid_csc_coef_reg[] = {
1610 		DISPC_VID_CSC_COEF(0), DISPC_VID_CSC_COEF(1),
1611 		DISPC_VID_CSC_COEF(2), DISPC_VID_CSC_COEF(3),
1612 		DISPC_VID_CSC_COEF(4), DISPC_VID_CSC_COEF(5),
1613 		DISPC_VID_CSC_COEF(6), /* K2G has no post offset support */
1614 	};
1615 	u32 regval[DISPC_CSC_REGVAL_LEN];
1616 	unsigned int i;
1617 
1618 	csc->to_regval(csc, regval);
1619 
1620 	if (regval[7] != 0)
1621 		dev_warn(dispc->dev, "%s: No post offset support for %s\n",
1622 			 __func__, csc->name);
1623 
1624 	for (i = 0; i < ARRAY_SIZE(dispc_vid_csc_coef_reg); i++)
1625 		dispc_vid_write(dispc, hw_plane, dispc_vid_csc_coef_reg[i],
1626 				regval[i]);
1627 }
1628 
1629 static void dispc_k3_vid_write_csc(struct dispc_device *dispc, u32 hw_plane,
1630 				   const struct dispc_csc_coef *csc)
1631 {
1632 	static const u16 dispc_vid_csc_coef_reg[DISPC_CSC_REGVAL_LEN] = {
1633 		DISPC_VID_CSC_COEF(0), DISPC_VID_CSC_COEF(1),
1634 		DISPC_VID_CSC_COEF(2), DISPC_VID_CSC_COEF(3),
1635 		DISPC_VID_CSC_COEF(4), DISPC_VID_CSC_COEF(5),
1636 		DISPC_VID_CSC_COEF(6), DISPC_VID_CSC_COEF7,
1637 	};
1638 	u32 regval[DISPC_CSC_REGVAL_LEN];
1639 	unsigned int i;
1640 
1641 	csc->to_regval(csc, regval);
1642 
1643 	for (i = 0; i < ARRAY_SIZE(dispc_vid_csc_coef_reg); i++)
1644 		dispc_vid_write(dispc, hw_plane, dispc_vid_csc_coef_reg[i],
1645 				regval[i]);
1646 }
1647 
1648 /* YUV -> RGB, ITU-R BT.601, full range */
1649 static const struct dispc_csc_coef csc_yuv2rgb_bt601_full = {
1650 	dispc_csc_yuv2rgb_regval,
1651 	{ 256,   0,  358,	/* ry, rcb, rcr |1.000  0.000  1.402|*/
1652 	  256, -88, -182,	/* gy, gcb, gcr |1.000 -0.344 -0.714|*/
1653 	  256, 452,    0, },	/* by, bcb, bcr |1.000  1.772  0.000|*/
1654 	{    0, -2048, -2048, },	/* full range */
1655 	{    0,     0,     0, },
1656 	CLIP_FULL_RANGE,
1657 	"BT.601 Full",
1658 };
1659 
1660 /* YUV -> RGB, ITU-R BT.601, limited range */
1661 static const struct dispc_csc_coef csc_yuv2rgb_bt601_lim = {
1662 	dispc_csc_yuv2rgb_regval,
1663 	{ 298,    0,  409,	/* ry, rcb, rcr |1.164  0.000  1.596|*/
1664 	  298, -100, -208,	/* gy, gcb, gcr |1.164 -0.392 -0.813|*/
1665 	  298,  516,    0, },	/* by, bcb, bcr |1.164  2.017  0.000|*/
1666 	{ -256, -2048, -2048, },	/* limited range */
1667 	{    0,     0,     0, },
1668 	CLIP_FULL_RANGE,
1669 	"BT.601 Limited",
1670 };
1671 
1672 /* YUV -> RGB, ITU-R BT.709, full range */
1673 static const struct dispc_csc_coef csc_yuv2rgb_bt709_full = {
1674 	dispc_csc_yuv2rgb_regval,
1675 	{ 256,	  0,  402,	/* ry, rcb, rcr |1.000	0.000  1.570|*/
1676 	  256,  -48, -120,	/* gy, gcb, gcr |1.000 -0.187 -0.467|*/
1677 	  256,  475,    0, },	/* by, bcb, bcr |1.000	1.856  0.000|*/
1678 	{    0, -2048, -2048, },	/* full range */
1679 	{    0,     0,     0, },
1680 	CLIP_FULL_RANGE,
1681 	"BT.709 Full",
1682 };
1683 
1684 /* YUV -> RGB, ITU-R BT.709, limited range */
1685 static const struct dispc_csc_coef csc_yuv2rgb_bt709_lim = {
1686 	dispc_csc_yuv2rgb_regval,
1687 	{ 298,    0,  459,	/* ry, rcb, rcr |1.164  0.000  1.793|*/
1688 	  298,  -55, -136,	/* gy, gcb, gcr |1.164 -0.213 -0.533|*/
1689 	  298,  541,    0, },	/* by, bcb, bcr |1.164  2.112  0.000|*/
1690 	{ -256, -2048, -2048, },	/* limited range */
1691 	{    0,     0,     0, },
1692 	CLIP_FULL_RANGE,
1693 	"BT.709 Limited",
1694 };
1695 
1696 static const struct {
1697 	enum drm_color_encoding encoding;
1698 	enum drm_color_range range;
1699 	const struct dispc_csc_coef *csc;
1700 } dispc_csc_table[] = {
1701 	{ DRM_COLOR_YCBCR_BT601, DRM_COLOR_YCBCR_FULL_RANGE,
1702 	  &csc_yuv2rgb_bt601_full, },
1703 	{ DRM_COLOR_YCBCR_BT601, DRM_COLOR_YCBCR_LIMITED_RANGE,
1704 	  &csc_yuv2rgb_bt601_lim, },
1705 	{ DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_FULL_RANGE,
1706 	  &csc_yuv2rgb_bt709_full, },
1707 	{ DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE,
1708 	  &csc_yuv2rgb_bt709_lim, },
1709 };
1710 
1711 static const
1712 struct dispc_csc_coef *dispc_find_csc(enum drm_color_encoding encoding,
1713 				      enum drm_color_range range)
1714 {
1715 	unsigned int i;
1716 
1717 	for (i = 0; i < ARRAY_SIZE(dispc_csc_table); i++) {
1718 		if (dispc_csc_table[i].encoding == encoding &&
1719 		    dispc_csc_table[i].range == range) {
1720 			return dispc_csc_table[i].csc;
1721 		}
1722 	}
1723 	return NULL;
1724 }
1725 
1726 static void dispc_vid_csc_setup(struct dispc_device *dispc, u32 hw_plane,
1727 				const struct drm_plane_state *state)
1728 {
1729 	const struct dispc_csc_coef *coef;
1730 
1731 	coef = dispc_find_csc(state->color_encoding, state->color_range);
1732 	if (!coef) {
1733 		dev_err(dispc->dev, "%s: CSC (%u,%u) not found\n",
1734 			__func__, state->color_encoding, state->color_range);
1735 		return;
1736 	}
1737 
1738 	if (dispc->feat->subrev == DISPC_K2G)
1739 		dispc_k2g_vid_write_csc(dispc, hw_plane, coef);
1740 	else
1741 		dispc_k3_vid_write_csc(dispc, hw_plane, coef);
1742 }
1743 
1744 static void dispc_vid_csc_enable(struct dispc_device *dispc, u32 hw_plane,
1745 				 bool enable)
1746 {
1747 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, !!enable,
1748 			DISPC_VID_ATTRIBUTES_COLORCONVENABLE_MASK);
1749 }
1750 
1751 /* SCALER */
1752 
1753 static u32 dispc_calc_fir_inc(u32 in, u32 out)
1754 {
1755 	return (u32)div_u64(0x200000ull * in, out);
1756 }
1757 
1758 enum dispc_vid_fir_coef_set {
1759 	DISPC_VID_FIR_COEF_HORIZ,
1760 	DISPC_VID_FIR_COEF_HORIZ_UV,
1761 	DISPC_VID_FIR_COEF_VERT,
1762 	DISPC_VID_FIR_COEF_VERT_UV,
1763 };
1764 
1765 static void dispc_vid_write_fir_coefs(struct dispc_device *dispc,
1766 				      u32 hw_plane,
1767 				      enum dispc_vid_fir_coef_set coef_set,
1768 				      const struct tidss_scale_coefs *coefs)
1769 {
1770 	static const u16 c0_regs[] = {
1771 		[DISPC_VID_FIR_COEF_HORIZ] = DISPC_VID_FIR_COEFS_H0,
1772 		[DISPC_VID_FIR_COEF_HORIZ_UV] = DISPC_VID_FIR_COEFS_H0_C,
1773 		[DISPC_VID_FIR_COEF_VERT] = DISPC_VID_FIR_COEFS_V0,
1774 		[DISPC_VID_FIR_COEF_VERT_UV] = DISPC_VID_FIR_COEFS_V0_C,
1775 	};
1776 
1777 	static const u16 c12_regs[] = {
1778 		[DISPC_VID_FIR_COEF_HORIZ] = DISPC_VID_FIR_COEFS_H12,
1779 		[DISPC_VID_FIR_COEF_HORIZ_UV] = DISPC_VID_FIR_COEFS_H12_C,
1780 		[DISPC_VID_FIR_COEF_VERT] = DISPC_VID_FIR_COEFS_V12,
1781 		[DISPC_VID_FIR_COEF_VERT_UV] = DISPC_VID_FIR_COEFS_V12_C,
1782 	};
1783 
1784 	const u16 c0_base = c0_regs[coef_set];
1785 	const u16 c12_base = c12_regs[coef_set];
1786 	int phase;
1787 
1788 	if (!coefs) {
1789 		dev_err(dispc->dev, "%s: No coefficients given.\n", __func__);
1790 		return;
1791 	}
1792 
1793 	for (phase = 0; phase <= 8; ++phase) {
1794 		u16 reg = c0_base + phase * 4;
1795 		u16 c0 = coefs->c0[phase];
1796 
1797 		dispc_vid_write(dispc, hw_plane, reg, c0);
1798 	}
1799 
1800 	for (phase = 0; phase <= 15; ++phase) {
1801 		u16 reg = c12_base + phase * 4;
1802 		s16 c1, c2;
1803 		u32 c12;
1804 
1805 		c1 = coefs->c1[phase];
1806 		c2 = coefs->c2[phase];
1807 		c12 = FIELD_PREP(GENMASK(19, 10), c1) | FIELD_PREP(GENMASK(29, 20),
1808 								   c2);
1809 
1810 		dispc_vid_write(dispc, hw_plane, reg, c12);
1811 	}
1812 }
1813 
1814 static bool dispc_fourcc_is_yuv(u32 fourcc)
1815 {
1816 	switch (fourcc) {
1817 	case DRM_FORMAT_YUYV:
1818 	case DRM_FORMAT_UYVY:
1819 	case DRM_FORMAT_NV12:
1820 		return true;
1821 	default:
1822 		return false;
1823 	}
1824 }
1825 
1826 struct dispc_scaling_params {
1827 	int xinc, yinc;
1828 	u32 in_w, in_h, in_w_uv, in_h_uv;
1829 	u32 fir_xinc, fir_yinc, fir_xinc_uv, fir_yinc_uv;
1830 	bool scale_x, scale_y;
1831 	const struct tidss_scale_coefs *xcoef, *ycoef, *xcoef_uv, *ycoef_uv;
1832 	bool five_taps;
1833 };
1834 
1835 static int dispc_vid_calc_scaling(struct dispc_device *dispc,
1836 				  const struct drm_plane_state *state,
1837 				  struct dispc_scaling_params *sp,
1838 				  bool lite_plane)
1839 {
1840 	const struct dispc_features_scaling *f = &dispc->feat->scaling;
1841 	u32 fourcc = state->fb->format->format;
1842 	u32 in_width_max_5tap = f->in_width_max_5tap_rgb;
1843 	u32 in_width_max_3tap = f->in_width_max_3tap_rgb;
1844 	u32 downscale_limit;
1845 	u32 in_width_max;
1846 
1847 	memset(sp, 0, sizeof(*sp));
1848 	sp->xinc = 1;
1849 	sp->yinc = 1;
1850 	sp->in_w = state->src_w >> 16;
1851 	sp->in_w_uv = sp->in_w;
1852 	sp->in_h = state->src_h >> 16;
1853 	sp->in_h_uv = sp->in_h;
1854 
1855 	sp->scale_x = sp->in_w != state->crtc_w;
1856 	sp->scale_y = sp->in_h != state->crtc_h;
1857 
1858 	if (dispc_fourcc_is_yuv(fourcc)) {
1859 		in_width_max_5tap = f->in_width_max_5tap_yuv;
1860 		in_width_max_3tap = f->in_width_max_3tap_yuv;
1861 
1862 		sp->in_w_uv >>= 1;
1863 		sp->scale_x = true;
1864 
1865 		if (fourcc == DRM_FORMAT_NV12) {
1866 			sp->in_h_uv >>= 1;
1867 			sp->scale_y = true;
1868 		}
1869 	}
1870 
1871 	/* Skip the rest if no scaling is used */
1872 	if ((!sp->scale_x && !sp->scale_y) || lite_plane)
1873 		return 0;
1874 
1875 	if (sp->in_w > in_width_max_5tap) {
1876 		sp->five_taps = false;
1877 		in_width_max = in_width_max_3tap;
1878 		downscale_limit = f->downscale_limit_3tap;
1879 	} else {
1880 		sp->five_taps = true;
1881 		in_width_max = in_width_max_5tap;
1882 		downscale_limit = f->downscale_limit_5tap;
1883 	}
1884 
1885 	if (sp->scale_x) {
1886 		sp->fir_xinc = dispc_calc_fir_inc(sp->in_w, state->crtc_w);
1887 
1888 		if (sp->fir_xinc < dispc_calc_fir_inc(1, f->upscale_limit)) {
1889 			dev_dbg(dispc->dev,
1890 				"%s: X-scaling factor %u/%u > %u\n",
1891 				__func__, state->crtc_w, state->src_w >> 16,
1892 				f->upscale_limit);
1893 			return -EINVAL;
1894 		}
1895 
1896 		if (sp->fir_xinc >= dispc_calc_fir_inc(downscale_limit, 1)) {
1897 			sp->xinc = DIV_ROUND_UP(DIV_ROUND_UP(sp->in_w,
1898 							     state->crtc_w),
1899 						downscale_limit);
1900 
1901 			if (sp->xinc > f->xinc_max) {
1902 				dev_dbg(dispc->dev,
1903 					"%s: X-scaling factor %u/%u < 1/%u\n",
1904 					__func__, state->crtc_w,
1905 					state->src_w >> 16,
1906 					downscale_limit * f->xinc_max);
1907 				return -EINVAL;
1908 			}
1909 
1910 			sp->in_w = (state->src_w >> 16) / sp->xinc;
1911 		}
1912 
1913 		while (sp->in_w > in_width_max) {
1914 			sp->xinc++;
1915 			sp->in_w = (state->src_w >> 16) / sp->xinc;
1916 		}
1917 
1918 		if (sp->xinc > f->xinc_max) {
1919 			dev_dbg(dispc->dev,
1920 				"%s: Too wide input buffer %u > %u\n", __func__,
1921 				state->src_w >> 16, in_width_max * f->xinc_max);
1922 			return -EINVAL;
1923 		}
1924 
1925 		/*
1926 		 * We need even line length for YUV formats. Decimation
1927 		 * can lead to odd length, so we need to make it even
1928 		 * again.
1929 		 */
1930 		if (dispc_fourcc_is_yuv(fourcc))
1931 			sp->in_w &= ~1;
1932 
1933 		sp->fir_xinc = dispc_calc_fir_inc(sp->in_w, state->crtc_w);
1934 	}
1935 
1936 	if (sp->scale_y) {
1937 		sp->fir_yinc = dispc_calc_fir_inc(sp->in_h, state->crtc_h);
1938 
1939 		if (sp->fir_yinc < dispc_calc_fir_inc(1, f->upscale_limit)) {
1940 			dev_dbg(dispc->dev,
1941 				"%s: Y-scaling factor %u/%u > %u\n",
1942 				__func__, state->crtc_h, state->src_h >> 16,
1943 				f->upscale_limit);
1944 			return -EINVAL;
1945 		}
1946 
1947 		if (sp->fir_yinc >= dispc_calc_fir_inc(downscale_limit, 1)) {
1948 			sp->yinc = DIV_ROUND_UP(DIV_ROUND_UP(sp->in_h,
1949 							     state->crtc_h),
1950 						downscale_limit);
1951 
1952 			sp->in_h /= sp->yinc;
1953 			sp->fir_yinc = dispc_calc_fir_inc(sp->in_h,
1954 							  state->crtc_h);
1955 		}
1956 	}
1957 
1958 	dev_dbg(dispc->dev,
1959 		"%s: %ux%u decim %ux%u -> %ux%u firinc %u.%03ux%u.%03u taps %u -> %ux%u\n",
1960 		__func__, state->src_w >> 16, state->src_h >> 16,
1961 		sp->xinc, sp->yinc, sp->in_w, sp->in_h,
1962 		sp->fir_xinc / 0x200000u,
1963 		((sp->fir_xinc & 0x1FFFFFu) * 999u) / 0x1FFFFFu,
1964 		sp->fir_yinc / 0x200000u,
1965 		((sp->fir_yinc & 0x1FFFFFu) * 999u) / 0x1FFFFFu,
1966 		sp->five_taps ? 5 : 3,
1967 		state->crtc_w, state->crtc_h);
1968 
1969 	if (dispc_fourcc_is_yuv(fourcc)) {
1970 		if (sp->scale_x) {
1971 			sp->in_w_uv /= sp->xinc;
1972 			sp->fir_xinc_uv = dispc_calc_fir_inc(sp->in_w_uv,
1973 							     state->crtc_w);
1974 			sp->xcoef_uv = tidss_get_scale_coefs(dispc->dev,
1975 							     sp->fir_xinc_uv,
1976 							     true);
1977 		}
1978 		if (sp->scale_y) {
1979 			sp->in_h_uv /= sp->yinc;
1980 			sp->fir_yinc_uv = dispc_calc_fir_inc(sp->in_h_uv,
1981 							     state->crtc_h);
1982 			sp->ycoef_uv = tidss_get_scale_coefs(dispc->dev,
1983 							     sp->fir_yinc_uv,
1984 							     sp->five_taps);
1985 		}
1986 	}
1987 
1988 	if (sp->scale_x)
1989 		sp->xcoef = tidss_get_scale_coefs(dispc->dev, sp->fir_xinc,
1990 						  true);
1991 
1992 	if (sp->scale_y)
1993 		sp->ycoef = tidss_get_scale_coefs(dispc->dev, sp->fir_yinc,
1994 						  sp->five_taps);
1995 
1996 	return 0;
1997 }
1998 
1999 static void dispc_vid_set_scaling(struct dispc_device *dispc,
2000 				  u32 hw_plane,
2001 				  struct dispc_scaling_params *sp,
2002 				  u32 fourcc)
2003 {
2004 	/* HORIZONTAL RESIZE ENABLE */
2005 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, sp->scale_x,
2006 			DISPC_VID_ATTRIBUTES_HRESIZEENABLE_MASK);
2007 
2008 	/* VERTICAL RESIZE ENABLE */
2009 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, sp->scale_y,
2010 			DISPC_VID_ATTRIBUTES_VRESIZEENABLE_MASK);
2011 
2012 	/* Skip the rest if no scaling is used */
2013 	if (!sp->scale_x && !sp->scale_y)
2014 		return;
2015 
2016 	/* VERTICAL 5-TAPS  */
2017 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, sp->five_taps,
2018 			DISPC_VID_ATTRIBUTES_VERTICALTAPS_MASK);
2019 
2020 	if (dispc_fourcc_is_yuv(fourcc)) {
2021 		if (sp->scale_x) {
2022 			dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRH2,
2023 					sp->fir_xinc_uv);
2024 			dispc_vid_write_fir_coefs(dispc, hw_plane,
2025 						  DISPC_VID_FIR_COEF_HORIZ_UV,
2026 						  sp->xcoef_uv);
2027 		}
2028 		if (sp->scale_y) {
2029 			dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRV2,
2030 					sp->fir_yinc_uv);
2031 			dispc_vid_write_fir_coefs(dispc, hw_plane,
2032 						  DISPC_VID_FIR_COEF_VERT_UV,
2033 						  sp->ycoef_uv);
2034 		}
2035 	}
2036 
2037 	if (sp->scale_x) {
2038 		dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRH, sp->fir_xinc);
2039 		dispc_vid_write_fir_coefs(dispc, hw_plane,
2040 					  DISPC_VID_FIR_COEF_HORIZ,
2041 					  sp->xcoef);
2042 	}
2043 
2044 	if (sp->scale_y) {
2045 		dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRV, sp->fir_yinc);
2046 		dispc_vid_write_fir_coefs(dispc, hw_plane,
2047 					  DISPC_VID_FIR_COEF_VERT, sp->ycoef);
2048 	}
2049 }
2050 
2051 /* OTHER */
2052 
2053 static const struct {
2054 	u32 fourcc;
2055 	u8 dss_code;
2056 } dispc_color_formats[] = {
2057 	{ DRM_FORMAT_ARGB4444, 0x0, },
2058 	{ DRM_FORMAT_ABGR4444, 0x1, },
2059 	{ DRM_FORMAT_RGBA4444, 0x2, },
2060 
2061 	{ DRM_FORMAT_RGB565, 0x3, },
2062 	{ DRM_FORMAT_BGR565, 0x4, },
2063 
2064 	{ DRM_FORMAT_ARGB1555, 0x5, },
2065 	{ DRM_FORMAT_ABGR1555, 0x6, },
2066 
2067 	{ DRM_FORMAT_ARGB8888, 0x7, },
2068 	{ DRM_FORMAT_ABGR8888, 0x8, },
2069 	{ DRM_FORMAT_RGBA8888, 0x9, },
2070 	{ DRM_FORMAT_BGRA8888, 0xa, },
2071 
2072 	{ DRM_FORMAT_RGB888, 0xb, },
2073 	{ DRM_FORMAT_BGR888, 0xc, },
2074 
2075 	{ DRM_FORMAT_ARGB2101010, 0xe, },
2076 	{ DRM_FORMAT_ABGR2101010, 0xf, },
2077 
2078 	{ DRM_FORMAT_XRGB4444, 0x20, },
2079 	{ DRM_FORMAT_XBGR4444, 0x21, },
2080 	{ DRM_FORMAT_RGBX4444, 0x22, },
2081 
2082 	{ DRM_FORMAT_XRGB1555, 0x25, },
2083 	{ DRM_FORMAT_XBGR1555, 0x26, },
2084 
2085 	{ DRM_FORMAT_XRGB8888, 0x27, },
2086 	{ DRM_FORMAT_XBGR8888, 0x28, },
2087 	{ DRM_FORMAT_RGBX8888, 0x29, },
2088 	{ DRM_FORMAT_BGRX8888, 0x2a, },
2089 
2090 	{ DRM_FORMAT_XRGB2101010, 0x2e, },
2091 	{ DRM_FORMAT_XBGR2101010, 0x2f, },
2092 
2093 	{ DRM_FORMAT_YUYV, 0x3e, },
2094 	{ DRM_FORMAT_UYVY, 0x3f, },
2095 
2096 	{ DRM_FORMAT_NV12, 0x3d, },
2097 };
2098 
2099 static void dispc_plane_set_pixel_format(struct dispc_device *dispc,
2100 					 u32 hw_plane, u32 fourcc)
2101 {
2102 	unsigned int i;
2103 
2104 	for (i = 0; i < ARRAY_SIZE(dispc_color_formats); ++i) {
2105 		if (dispc_color_formats[i].fourcc == fourcc) {
2106 			VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES,
2107 					dispc_color_formats[i].dss_code,
2108 					DISPC_VID_ATTRIBUTES_FORMAT_MASK);
2109 			return;
2110 		}
2111 	}
2112 
2113 	WARN_ON(1);
2114 }
2115 
2116 const u32 *dispc_plane_formats(struct dispc_device *dispc, unsigned int *len)
2117 {
2118 	WARN_ON(!dispc->fourccs);
2119 
2120 	*len = dispc->num_fourccs;
2121 
2122 	return dispc->fourccs;
2123 }
2124 
2125 static s32 pixinc(int pixels, u8 ps)
2126 {
2127 	if (pixels == 1)
2128 		return 1;
2129 	else if (pixels > 1)
2130 		return 1 + (pixels - 1) * ps;
2131 	else if (pixels < 0)
2132 		return 1 - (-pixels + 1) * ps;
2133 
2134 	WARN_ON(1);
2135 	return 0;
2136 }
2137 
2138 int dispc_plane_check(struct dispc_device *dispc, u32 hw_plane,
2139 		      const struct drm_plane_state *state,
2140 		      u32 hw_videoport)
2141 {
2142 	bool lite = dispc->feat->vid_info[hw_plane].is_lite;
2143 	u32 fourcc = state->fb->format->format;
2144 	bool need_scaling = state->src_w >> 16 != state->crtc_w ||
2145 		state->src_h >> 16 != state->crtc_h;
2146 	struct dispc_scaling_params scaling;
2147 	int ret;
2148 
2149 	if (dispc_fourcc_is_yuv(fourcc)) {
2150 		if (!dispc_find_csc(state->color_encoding,
2151 				    state->color_range)) {
2152 			dev_dbg(dispc->dev,
2153 				"%s: Unsupported CSC (%u,%u) for HW plane %u\n",
2154 				__func__, state->color_encoding,
2155 				state->color_range, hw_plane);
2156 			return -EINVAL;
2157 		}
2158 	}
2159 
2160 	if (need_scaling) {
2161 		if (lite) {
2162 			dev_dbg(dispc->dev,
2163 				"%s: Lite plane %u can't scale %ux%u!=%ux%u\n",
2164 				__func__, hw_plane,
2165 				state->src_w >> 16, state->src_h >> 16,
2166 				state->crtc_w, state->crtc_h);
2167 			return -EINVAL;
2168 		}
2169 		ret = dispc_vid_calc_scaling(dispc, state, &scaling, false);
2170 		if (ret)
2171 			return ret;
2172 	}
2173 
2174 	return 0;
2175 }
2176 
2177 static
2178 dma_addr_t dispc_plane_state_dma_addr(const struct drm_plane_state *state)
2179 {
2180 	struct drm_framebuffer *fb = state->fb;
2181 	struct drm_gem_dma_object *gem;
2182 	u32 x = state->src_x >> 16;
2183 	u32 y = state->src_y >> 16;
2184 
2185 	gem = drm_fb_dma_get_gem_obj(state->fb, 0);
2186 
2187 	return gem->dma_addr + fb->offsets[0] + x * fb->format->cpp[0] +
2188 		y * fb->pitches[0];
2189 }
2190 
2191 static
2192 dma_addr_t dispc_plane_state_p_uv_addr(const struct drm_plane_state *state)
2193 {
2194 	struct drm_framebuffer *fb = state->fb;
2195 	struct drm_gem_dma_object *gem;
2196 	u32 x = state->src_x >> 16;
2197 	u32 y = state->src_y >> 16;
2198 
2199 	if (WARN_ON(state->fb->format->num_planes != 2))
2200 		return 0;
2201 
2202 	gem = drm_fb_dma_get_gem_obj(fb, 1);
2203 
2204 	return gem->dma_addr + fb->offsets[1] +
2205 		(x * fb->format->cpp[1] / fb->format->hsub) +
2206 		(y * fb->pitches[1] / fb->format->vsub);
2207 }
2208 
2209 void dispc_plane_setup(struct dispc_device *dispc, u32 hw_plane,
2210 		       const struct drm_plane_state *state,
2211 		       u32 hw_videoport)
2212 {
2213 	bool lite = dispc->feat->vid_info[hw_plane].is_lite;
2214 	u32 fourcc = state->fb->format->format;
2215 	u16 cpp = state->fb->format->cpp[0];
2216 	u32 fb_width = state->fb->pitches[0] / cpp;
2217 	dma_addr_t dma_addr = dispc_plane_state_dma_addr(state);
2218 	struct dispc_scaling_params scale;
2219 
2220 	dispc_vid_calc_scaling(dispc, state, &scale, lite);
2221 
2222 	dispc_plane_set_pixel_format(dispc, hw_plane, fourcc);
2223 
2224 	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_0, dma_addr & 0xffffffff);
2225 	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_EXT_0, (u64)dma_addr >> 32);
2226 	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_1, dma_addr & 0xffffffff);
2227 	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_EXT_1, (u64)dma_addr >> 32);
2228 
2229 	dispc_vid_write(dispc, hw_plane, DISPC_VID_PICTURE_SIZE,
2230 			FIELD_PREP(DISPC_VID_PICTURE_SIZE_MEMSIZEY_MASK, scale.in_h - 1) |
2231 			FIELD_PREP(DISPC_VID_PICTURE_SIZE_MEMSIZEX_MASK, scale.in_w - 1));
2232 
2233 	/* For YUV422 format we use the macropixel size for pixel inc */
2234 	if (fourcc == DRM_FORMAT_YUYV || fourcc == DRM_FORMAT_UYVY)
2235 		dispc_vid_write(dispc, hw_plane, DISPC_VID_PIXEL_INC,
2236 				pixinc(scale.xinc, cpp * 2));
2237 	else
2238 		dispc_vid_write(dispc, hw_plane, DISPC_VID_PIXEL_INC,
2239 				pixinc(scale.xinc, cpp));
2240 
2241 	dispc_vid_write(dispc, hw_plane, DISPC_VID_ROW_INC,
2242 			pixinc(1 + (scale.yinc * fb_width -
2243 				    scale.xinc * scale.in_w),
2244 			       cpp));
2245 
2246 	if (state->fb->format->num_planes == 2) {
2247 		u16 cpp_uv = state->fb->format->cpp[1];
2248 		u32 fb_width_uv = state->fb->pitches[1] / cpp_uv;
2249 		dma_addr_t p_uv_addr = dispc_plane_state_p_uv_addr(state);
2250 
2251 		dispc_vid_write(dispc, hw_plane,
2252 				DISPC_VID_BA_UV_0, p_uv_addr & 0xffffffff);
2253 		dispc_vid_write(dispc, hw_plane,
2254 				DISPC_VID_BA_UV_EXT_0, (u64)p_uv_addr >> 32);
2255 		dispc_vid_write(dispc, hw_plane,
2256 				DISPC_VID_BA_UV_1, p_uv_addr & 0xffffffff);
2257 		dispc_vid_write(dispc, hw_plane,
2258 				DISPC_VID_BA_UV_EXT_1, (u64)p_uv_addr >> 32);
2259 
2260 		dispc_vid_write(dispc, hw_plane, DISPC_VID_ROW_INC_UV,
2261 				pixinc(1 + (scale.yinc * fb_width_uv -
2262 					    scale.xinc * scale.in_w_uv),
2263 				       cpp_uv));
2264 	}
2265 
2266 	if (!lite) {
2267 		dispc_vid_write(dispc, hw_plane, DISPC_VID_SIZE,
2268 				FIELD_PREP(DISPC_VID_SIZE_SIZEY_MASK,
2269 					   state->crtc_h - 1) |
2270 				FIELD_PREP(DISPC_VID_SIZE_SIZEX_MASK,
2271 					   state->crtc_w - 1));
2272 
2273 		dispc_vid_set_scaling(dispc, hw_plane, &scale, fourcc);
2274 	}
2275 
2276 	/* enable YUV->RGB color conversion */
2277 	if (dispc_fourcc_is_yuv(fourcc)) {
2278 		dispc_vid_csc_setup(dispc, hw_plane, state);
2279 		dispc_vid_csc_enable(dispc, hw_plane, true);
2280 	} else {
2281 		dispc_vid_csc_enable(dispc, hw_plane, false);
2282 	}
2283 
2284 	dispc_vid_write(dispc, hw_plane, DISPC_VID_GLOBAL_ALPHA,
2285 			FIELD_PREP(DISPC_VID_GLOBAL_ALPHA_GLOBALALPHA_MASK,
2286 				   state->alpha >> 8));
2287 
2288 	if (state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
2289 		VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 1,
2290 				DISPC_VID_ATTRIBUTES_PREMULTIPLYALPHA_MASK);
2291 	else
2292 		VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 0,
2293 				DISPC_VID_ATTRIBUTES_PREMULTIPLYALPHA_MASK);
2294 }
2295 
2296 void dispc_plane_enable(struct dispc_device *dispc, u32 hw_plane, bool enable)
2297 {
2298 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, !!enable,
2299 			DISPC_VID_ATTRIBUTES_ENABLE_MASK);
2300 }
2301 
2302 static u32 dispc_vid_get_fifo_size(struct dispc_device *dispc, u32 hw_plane)
2303 {
2304 	return VID_REG_GET(dispc, hw_plane, DISPC_VID_BUF_SIZE_STATUS,
2305 			   DISPC_VID_BUF_SIZE_STATUS_BUFSIZE_MASK);
2306 }
2307 
2308 static void dispc_vid_set_mflag_threshold(struct dispc_device *dispc,
2309 					  u32 hw_plane, u32 low, u32 high)
2310 {
2311 	dispc_vid_write(dispc, hw_plane, DISPC_VID_MFLAG_THRESHOLD,
2312 			FIELD_PREP(DISPC_VID_MFLAG_THRESHOLD_HT_MFLAG_MASK, high) |
2313 			FIELD_PREP(DISPC_VID_MFLAG_THRESHOLD_LT_MFLAG_MASK, low));
2314 }
2315 
2316 static void dispc_vid_set_buf_threshold(struct dispc_device *dispc,
2317 					u32 hw_plane, u32 low, u32 high)
2318 {
2319 	dispc_vid_write(dispc, hw_plane, DISPC_VID_BUF_THRESHOLD,
2320 			FIELD_PREP(DISPC_VID_BUF_THRESHOLD_BUFHIGHTHRESHOLD_MASK,
2321 				   high) |
2322 			FIELD_PREP(DISPC_VID_BUF_THRESHOLD_BUFLOWTHRESHOLD_MASK,
2323 				   low));
2324 }
2325 
2326 static void dispc_k2g_plane_init(struct dispc_device *dispc)
2327 {
2328 	unsigned int hw_plane;
2329 
2330 	dev_dbg(dispc->dev, "%s()\n", __func__);
2331 
2332 	/* MFLAG_CTRL = ENABLED */
2333 	REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 2,
2334 		    DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_CTRL_MASK);
2335 	/* MFLAG_START = MFLAGNORMALSTARTMODE */
2336 	REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 0,
2337 		    DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_START_MASK);
2338 
2339 	for (hw_plane = 0; hw_plane < dispc->feat->num_vids; hw_plane++) {
2340 		u32 size = dispc_vid_get_fifo_size(dispc, hw_plane);
2341 		u32 thr_low, thr_high;
2342 		u32 mflag_low, mflag_high;
2343 		u32 preload;
2344 
2345 		thr_high = size - 1;
2346 		thr_low = size / 2;
2347 
2348 		mflag_high = size * 2 / 3;
2349 		mflag_low = size / 3;
2350 
2351 		preload = thr_low;
2352 
2353 		dev_dbg(dispc->dev,
2354 			"%s: bufsize %u, buf_threshold %u/%u, mflag threshold %u/%u preload %u\n",
2355 			dispc->feat->vid_info[hw_plane].name,
2356 			size,
2357 			thr_high, thr_low,
2358 			mflag_high, mflag_low,
2359 			preload);
2360 
2361 		dispc_vid_set_buf_threshold(dispc, hw_plane,
2362 					    thr_low, thr_high);
2363 		dispc_vid_set_mflag_threshold(dispc, hw_plane,
2364 					      mflag_low, mflag_high);
2365 
2366 		dispc_vid_write(dispc, hw_plane, DISPC_VID_PRELOAD, preload);
2367 
2368 		/*
2369 		 * Prefetch up to fifo high-threshold value to minimize the
2370 		 * possibility of underflows. Note that this means the PRELOAD
2371 		 * register is ignored.
2372 		 */
2373 		VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 1,
2374 				DISPC_VID_ATTRIBUTES_BUFPRELOAD_MASK);
2375 	}
2376 }
2377 
2378 static void dispc_k3_plane_init(struct dispc_device *dispc)
2379 {
2380 	unsigned int hw_plane;
2381 	u32 cba_lo_pri = 1;
2382 	u32 cba_hi_pri = 0;
2383 
2384 	dev_dbg(dispc->dev, "%s()\n", __func__);
2385 
2386 	REG_FLD_MOD(dispc, DSS_CBA_CFG, cba_lo_pri, DSS_CBA_CFG_PRI_LO_MASK);
2387 	REG_FLD_MOD(dispc, DSS_CBA_CFG, cba_hi_pri, DSS_CBA_CFG_PRI_HI_MASK);
2388 
2389 	/* MFLAG_CTRL = ENABLED */
2390 	REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 2,
2391 		    DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_CTRL_MASK);
2392 	/* MFLAG_START = MFLAGNORMALSTARTMODE */
2393 	REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 0,
2394 		    DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_START_MASK);
2395 
2396 	for (hw_plane = 0; hw_plane < dispc->feat->num_vids; hw_plane++) {
2397 		u32 size = dispc_vid_get_fifo_size(dispc, hw_plane);
2398 		u32 thr_low, thr_high;
2399 		u32 mflag_low, mflag_high;
2400 		u32 preload;
2401 
2402 		thr_high = size - 1;
2403 		thr_low = size / 2;
2404 
2405 		mflag_high = size * 2 / 3;
2406 		mflag_low = size / 3;
2407 
2408 		preload = thr_low;
2409 
2410 		dev_dbg(dispc->dev,
2411 			"%s: bufsize %u, buf_threshold %u/%u, mflag threshold %u/%u preload %u\n",
2412 			dispc->feat->vid_info[hw_plane].name,
2413 			size,
2414 			thr_high, thr_low,
2415 			mflag_high, mflag_low,
2416 			preload);
2417 
2418 		dispc_vid_set_buf_threshold(dispc, hw_plane,
2419 					    thr_low, thr_high);
2420 		dispc_vid_set_mflag_threshold(dispc, hw_plane,
2421 					      mflag_low, mflag_high);
2422 
2423 		dispc_vid_write(dispc, hw_plane, DISPC_VID_PRELOAD, preload);
2424 
2425 		/* Prefech up to PRELOAD value */
2426 		VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 0,
2427 				DISPC_VID_ATTRIBUTES_BUFPRELOAD_MASK);
2428 	}
2429 }
2430 
2431 static void dispc_plane_init(struct dispc_device *dispc)
2432 {
2433 	switch (dispc->feat->subrev) {
2434 	case DISPC_K2G:
2435 		dispc_k2g_plane_init(dispc);
2436 		break;
2437 	case DISPC_AM625:
2438 	case DISPC_AM62A7:
2439 	case DISPC_AM62L:
2440 	case DISPC_AM65X:
2441 	case DISPC_J721E:
2442 		dispc_k3_plane_init(dispc);
2443 		break;
2444 	default:
2445 		WARN_ON(1);
2446 	}
2447 }
2448 
2449 static void dispc_vp_init(struct dispc_device *dispc)
2450 {
2451 	unsigned int i;
2452 
2453 	dev_dbg(dispc->dev, "%s()\n", __func__);
2454 
2455 	/* Enable the gamma Shadow bit-field for all VPs*/
2456 	for (i = 0; i < dispc->feat->num_vps; i++)
2457 		VP_REG_FLD_MOD(dispc, i, DISPC_VP_CONFIG, 1,
2458 			       DISPC_VP_CONFIG_GAMMAENABLE_MASK);
2459 }
2460 
2461 static void dispc_initial_config(struct dispc_device *dispc)
2462 {
2463 	dispc_plane_init(dispc);
2464 	dispc_vp_init(dispc);
2465 
2466 	/* Note: Hardcoded DPI routing on J721E for now */
2467 	if (dispc->feat->subrev == DISPC_J721E) {
2468 		dispc_write(dispc, DISPC_CONNECTIONS,
2469 			    FIELD_PREP(DISPC_CONNECTIONS_DPI_0_CONN_MASK, 2) |		/* VP1 to DPI0 */
2470 			    FIELD_PREP(DISPC_CONNECTIONS_DPI_1_CONN_MASK, 8)		/* VP3 to DPI1 */
2471 			);
2472 	}
2473 }
2474 
2475 static void dispc_k2g_vp_write_gamma_table(struct dispc_device *dispc,
2476 					   u32 hw_videoport)
2477 {
2478 	u32 *table = dispc->vp_data[hw_videoport].gamma_table;
2479 	u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
2480 	unsigned int i;
2481 
2482 	dev_dbg(dispc->dev, "%s: hw_videoport %d\n", __func__, hw_videoport);
2483 
2484 	if (WARN_ON(dispc->feat->vp_feat.color.gamma_type != TIDSS_GAMMA_8BIT))
2485 		return;
2486 
2487 	for (i = 0; i < hwlen; ++i) {
2488 		u32 v = table[i];
2489 
2490 		v |= i << 24;
2491 
2492 		dispc_vp_write(dispc, hw_videoport, DISPC_VP_K2G_GAMMA_TABLE,
2493 			       v);
2494 	}
2495 }
2496 
2497 static void dispc_am65x_vp_write_gamma_table(struct dispc_device *dispc,
2498 					     u32 hw_videoport)
2499 {
2500 	u32 *table = dispc->vp_data[hw_videoport].gamma_table;
2501 	u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
2502 	unsigned int i;
2503 
2504 	dev_dbg(dispc->dev, "%s: hw_videoport %d\n", __func__, hw_videoport);
2505 
2506 	if (WARN_ON(dispc->feat->vp_feat.color.gamma_type != TIDSS_GAMMA_8BIT))
2507 		return;
2508 
2509 	for (i = 0; i < hwlen; ++i) {
2510 		u32 v = table[i];
2511 
2512 		v |= i << 24;
2513 
2514 		dispc_vp_write(dispc, hw_videoport, DISPC_VP_GAMMA_TABLE, v);
2515 	}
2516 }
2517 
2518 static void dispc_j721e_vp_write_gamma_table(struct dispc_device *dispc,
2519 					     u32 hw_videoport)
2520 {
2521 	u32 *table = dispc->vp_data[hw_videoport].gamma_table;
2522 	u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
2523 	unsigned int i;
2524 
2525 	dev_dbg(dispc->dev, "%s: hw_videoport %d\n", __func__, hw_videoport);
2526 
2527 	if (WARN_ON(dispc->feat->vp_feat.color.gamma_type != TIDSS_GAMMA_10BIT))
2528 		return;
2529 
2530 	for (i = 0; i < hwlen; ++i) {
2531 		u32 v = table[i];
2532 
2533 		if (i == 0)
2534 			v |= 1 << 31;
2535 
2536 		dispc_vp_write(dispc, hw_videoport, DISPC_VP_GAMMA_TABLE, v);
2537 	}
2538 }
2539 
2540 static void dispc_vp_write_gamma_table(struct dispc_device *dispc,
2541 				       u32 hw_videoport)
2542 {
2543 	switch (dispc->feat->subrev) {
2544 	case DISPC_K2G:
2545 		dispc_k2g_vp_write_gamma_table(dispc, hw_videoport);
2546 		break;
2547 	case DISPC_AM625:
2548 	case DISPC_AM62A7:
2549 	case DISPC_AM62L:
2550 	case DISPC_AM65X:
2551 		dispc_am65x_vp_write_gamma_table(dispc, hw_videoport);
2552 		break;
2553 	case DISPC_J721E:
2554 		dispc_j721e_vp_write_gamma_table(dispc, hw_videoport);
2555 		break;
2556 	default:
2557 		WARN_ON(1);
2558 		break;
2559 	}
2560 }
2561 
2562 static const struct drm_color_lut dispc_vp_gamma_default_lut[] = {
2563 	{ .red = 0, .green = 0, .blue = 0, },
2564 	{ .red = U16_MAX, .green = U16_MAX, .blue = U16_MAX, },
2565 };
2566 
2567 static void dispc_vp_set_gamma(struct dispc_device *dispc,
2568 			       u32 hw_videoport,
2569 			       const struct drm_color_lut *lut,
2570 			       unsigned int length)
2571 {
2572 	u32 *table = dispc->vp_data[hw_videoport].gamma_table;
2573 	u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
2574 	u32 hwbits;
2575 	unsigned int i;
2576 
2577 	dev_dbg(dispc->dev, "%s: hw_videoport %d, lut len %u, hw len %u\n",
2578 		__func__, hw_videoport, length, hwlen);
2579 
2580 	if (dispc->feat->vp_feat.color.gamma_type == TIDSS_GAMMA_10BIT)
2581 		hwbits = 10;
2582 	else
2583 		hwbits = 8;
2584 
2585 	if (!lut || length < 2) {
2586 		lut = dispc_vp_gamma_default_lut;
2587 		length = ARRAY_SIZE(dispc_vp_gamma_default_lut);
2588 	}
2589 
2590 	for (i = 0; i < length - 1; ++i) {
2591 		unsigned int first = i * (hwlen - 1) / (length - 1);
2592 		unsigned int last = (i + 1) * (hwlen - 1) / (length - 1);
2593 		unsigned int w = last - first;
2594 		u16 r, g, b;
2595 		unsigned int j;
2596 
2597 		if (w == 0)
2598 			continue;
2599 
2600 		for (j = 0; j <= w; j++) {
2601 			r = (lut[i].red * (w - j) + lut[i + 1].red * j) / w;
2602 			g = (lut[i].green * (w - j) + lut[i + 1].green * j) / w;
2603 			b = (lut[i].blue * (w - j) + lut[i + 1].blue * j) / w;
2604 
2605 			r >>= 16 - hwbits;
2606 			g >>= 16 - hwbits;
2607 			b >>= 16 - hwbits;
2608 
2609 			table[first + j] = (r << (hwbits * 2)) |
2610 				(g << hwbits) | b;
2611 		}
2612 	}
2613 
2614 	dispc_vp_write_gamma_table(dispc, hw_videoport);
2615 }
2616 
2617 static s16 dispc_S31_32_to_s2_8(s64 coef)
2618 {
2619 	u64 sign_bit = 1ULL << 63;
2620 	u64 cbits = (u64)coef;
2621 	s16 ret;
2622 
2623 	if (cbits & sign_bit)
2624 		ret = -clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x200);
2625 	else
2626 		ret = clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x1FF);
2627 
2628 	return ret;
2629 }
2630 
2631 static void dispc_k2g_cpr_from_ctm(const struct drm_color_ctm *ctm,
2632 				   struct dispc_csc_coef *cpr)
2633 {
2634 	memset(cpr, 0, sizeof(*cpr));
2635 
2636 	cpr->to_regval = dispc_csc_cpr_regval;
2637 	cpr->m[CSC_RR] = dispc_S31_32_to_s2_8(ctm->matrix[0]);
2638 	cpr->m[CSC_RG] = dispc_S31_32_to_s2_8(ctm->matrix[1]);
2639 	cpr->m[CSC_RB] = dispc_S31_32_to_s2_8(ctm->matrix[2]);
2640 	cpr->m[CSC_GR] = dispc_S31_32_to_s2_8(ctm->matrix[3]);
2641 	cpr->m[CSC_GG] = dispc_S31_32_to_s2_8(ctm->matrix[4]);
2642 	cpr->m[CSC_GB] = dispc_S31_32_to_s2_8(ctm->matrix[5]);
2643 	cpr->m[CSC_BR] = dispc_S31_32_to_s2_8(ctm->matrix[6]);
2644 	cpr->m[CSC_BG] = dispc_S31_32_to_s2_8(ctm->matrix[7]);
2645 	cpr->m[CSC_BB] = dispc_S31_32_to_s2_8(ctm->matrix[8]);
2646 }
2647 
2648 #define CVAL(xR, xG, xB) (FIELD_PREP(GENMASK(9, 0), xR) | FIELD_PREP(GENMASK(20, 11), xG) |	\
2649 			  FIELD_PREP(GENMASK(31, 22), xB))
2650 
2651 static void dispc_k2g_vp_csc_cpr_regval(const struct dispc_csc_coef *csc,
2652 					u32 *regval)
2653 {
2654 	regval[0] = CVAL(csc->m[CSC_BB], csc->m[CSC_BG], csc->m[CSC_BR]);
2655 	regval[1] = CVAL(csc->m[CSC_GB], csc->m[CSC_GG], csc->m[CSC_GR]);
2656 	regval[2] = CVAL(csc->m[CSC_RB], csc->m[CSC_RG], csc->m[CSC_RR]);
2657 }
2658 
2659 #undef CVAL
2660 
2661 static void dispc_k2g_vp_write_csc(struct dispc_device *dispc, u32 hw_videoport,
2662 				   const struct dispc_csc_coef *csc)
2663 {
2664 	static const u16 dispc_vp_cpr_coef_reg[] = {
2665 		DISPC_VP_CSC_COEF0, DISPC_VP_CSC_COEF1, DISPC_VP_CSC_COEF2,
2666 		/* K2G CPR is packed to three registers. */
2667 	};
2668 	u32 regval[DISPC_CSC_REGVAL_LEN];
2669 	unsigned int i;
2670 
2671 	dispc_k2g_vp_csc_cpr_regval(csc, regval);
2672 
2673 	for (i = 0; i < ARRAY_SIZE(dispc_vp_cpr_coef_reg); i++)
2674 		dispc_vp_write(dispc, hw_videoport, dispc_vp_cpr_coef_reg[i],
2675 			       regval[i]);
2676 }
2677 
2678 static void dispc_k2g_vp_set_ctm(struct dispc_device *dispc, u32 hw_videoport,
2679 				 struct drm_color_ctm *ctm)
2680 {
2681 	u32 cprenable = 0;
2682 
2683 	if (ctm) {
2684 		struct dispc_csc_coef cpr;
2685 
2686 		dispc_k2g_cpr_from_ctm(ctm, &cpr);
2687 		dispc_k2g_vp_write_csc(dispc, hw_videoport, &cpr);
2688 		cprenable = 1;
2689 	}
2690 
2691 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONFIG, cprenable,
2692 		       DISPC_VP_CONFIG_CPR_MASK);
2693 }
2694 
2695 static s16 dispc_S31_32_to_s3_8(s64 coef)
2696 {
2697 	u64 sign_bit = 1ULL << 63;
2698 	u64 cbits = (u64)coef;
2699 	s16 ret;
2700 
2701 	if (cbits & sign_bit)
2702 		ret = -clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x400);
2703 	else
2704 		ret = clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x3FF);
2705 
2706 	return ret;
2707 }
2708 
2709 static void dispc_csc_from_ctm(const struct drm_color_ctm *ctm,
2710 			       struct dispc_csc_coef *cpr)
2711 {
2712 	memset(cpr, 0, sizeof(*cpr));
2713 
2714 	cpr->to_regval = dispc_csc_cpr_regval;
2715 	cpr->m[CSC_RR] = dispc_S31_32_to_s3_8(ctm->matrix[0]);
2716 	cpr->m[CSC_RG] = dispc_S31_32_to_s3_8(ctm->matrix[1]);
2717 	cpr->m[CSC_RB] = dispc_S31_32_to_s3_8(ctm->matrix[2]);
2718 	cpr->m[CSC_GR] = dispc_S31_32_to_s3_8(ctm->matrix[3]);
2719 	cpr->m[CSC_GG] = dispc_S31_32_to_s3_8(ctm->matrix[4]);
2720 	cpr->m[CSC_GB] = dispc_S31_32_to_s3_8(ctm->matrix[5]);
2721 	cpr->m[CSC_BR] = dispc_S31_32_to_s3_8(ctm->matrix[6]);
2722 	cpr->m[CSC_BG] = dispc_S31_32_to_s3_8(ctm->matrix[7]);
2723 	cpr->m[CSC_BB] = dispc_S31_32_to_s3_8(ctm->matrix[8]);
2724 }
2725 
2726 static void dispc_k3_vp_write_csc(struct dispc_device *dispc, u32 hw_videoport,
2727 				  const struct dispc_csc_coef *csc)
2728 {
2729 	static const u16 dispc_vp_csc_coef_reg[DISPC_CSC_REGVAL_LEN] = {
2730 		DISPC_VP_CSC_COEF0, DISPC_VP_CSC_COEF1, DISPC_VP_CSC_COEF2,
2731 		DISPC_VP_CSC_COEF3, DISPC_VP_CSC_COEF4, DISPC_VP_CSC_COEF5,
2732 		DISPC_VP_CSC_COEF6, DISPC_VP_CSC_COEF7,
2733 	};
2734 	u32 regval[DISPC_CSC_REGVAL_LEN];
2735 	unsigned int i;
2736 
2737 	csc->to_regval(csc, regval);
2738 
2739 	for (i = 0; i < ARRAY_SIZE(regval); i++)
2740 		dispc_vp_write(dispc, hw_videoport, dispc_vp_csc_coef_reg[i],
2741 			       regval[i]);
2742 }
2743 
2744 static void dispc_k3_vp_set_ctm(struct dispc_device *dispc, u32 hw_videoport,
2745 				struct drm_color_ctm *ctm)
2746 {
2747 	u32 colorconvenable = 0;
2748 
2749 	if (ctm) {
2750 		struct dispc_csc_coef csc;
2751 
2752 		dispc_csc_from_ctm(ctm, &csc);
2753 		dispc_k3_vp_write_csc(dispc, hw_videoport, &csc);
2754 		colorconvenable = 1;
2755 	}
2756 
2757 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONFIG, colorconvenable,
2758 		       DISPC_VP_CONFIG_COLORCONVENABLE_MASK);
2759 }
2760 
2761 static void dispc_vp_set_color_mgmt(struct dispc_device *dispc,
2762 				    u32 hw_videoport,
2763 				    const struct drm_crtc_state *state,
2764 				    bool newmodeset)
2765 {
2766 	struct drm_color_lut *lut = NULL;
2767 	struct drm_color_ctm *ctm = NULL;
2768 	unsigned int length = 0;
2769 
2770 	if (!(state->color_mgmt_changed || newmodeset))
2771 		return;
2772 
2773 	if (state->gamma_lut) {
2774 		lut = (struct drm_color_lut *)state->gamma_lut->data;
2775 		length = state->gamma_lut->length / sizeof(*lut);
2776 	}
2777 
2778 	dispc_vp_set_gamma(dispc, hw_videoport, lut, length);
2779 
2780 	if (state->ctm)
2781 		ctm = (struct drm_color_ctm *)state->ctm->data;
2782 
2783 	if (dispc->feat->subrev == DISPC_K2G)
2784 		dispc_k2g_vp_set_ctm(dispc, hw_videoport, ctm);
2785 	else
2786 		dispc_k3_vp_set_ctm(dispc, hw_videoport, ctm);
2787 }
2788 
2789 void dispc_vp_setup(struct dispc_device *dispc, u32 hw_videoport,
2790 		    const struct drm_crtc_state *state, bool newmodeset)
2791 {
2792 	dispc_vp_set_default_color(dispc, hw_videoport, 0);
2793 	dispc_vp_set_color_mgmt(dispc, hw_videoport, state, newmodeset);
2794 }
2795 
2796 int dispc_runtime_suspend(struct dispc_device *dispc)
2797 {
2798 	dev_dbg(dispc->dev, "suspend\n");
2799 
2800 	dispc->is_enabled = false;
2801 
2802 	clk_disable_unprepare(dispc->fclk);
2803 
2804 	return 0;
2805 }
2806 
2807 int dispc_runtime_resume(struct dispc_device *dispc)
2808 {
2809 	dev_dbg(dispc->dev, "resume\n");
2810 
2811 	clk_prepare_enable(dispc->fclk);
2812 
2813 	if (REG_GET(dispc, DSS_SYSSTATUS, DSS_SYSSTATUS_DISPC_FUNC_RESETDONE) == 0)
2814 		dev_warn(dispc->dev, "DSS FUNC RESET not done!\n");
2815 
2816 	dev_dbg(dispc->dev, "OMAP DSS7 rev 0x%x\n",
2817 		dispc_read(dispc, DSS_REVISION));
2818 
2819 	dev_dbg(dispc->dev, "VP RESETDONE %d,%d,%d\n",
2820 		REG_GET(dispc, DSS_SYSSTATUS, GENMASK(1, 1)),
2821 		REG_GET(dispc, DSS_SYSSTATUS, GENMASK(2, 2)),
2822 		REG_GET(dispc, DSS_SYSSTATUS, GENMASK(3, 3)));
2823 
2824 	if (dispc->feat->subrev == DISPC_AM625 ||
2825 	    dispc->feat->subrev == DISPC_AM65X)
2826 		dev_dbg(dispc->dev, "OLDI RESETDONE %d,%d,%d\n",
2827 			REG_GET(dispc, DSS_SYSSTATUS, GENMASK(5, 5)),
2828 			REG_GET(dispc, DSS_SYSSTATUS, GENMASK(6, 6)),
2829 			REG_GET(dispc, DSS_SYSSTATUS, GENMASK(7, 7)));
2830 
2831 	dev_dbg(dispc->dev, "DISPC IDLE %d\n",
2832 		REG_GET(dispc, DSS_SYSSTATUS, DSS_SYSSTATUS_DISPC_IDLE_STATUS));
2833 
2834 	dispc_initial_config(dispc);
2835 
2836 	dispc->is_enabled = true;
2837 
2838 	tidss_irq_resume(dispc->tidss);
2839 
2840 	return 0;
2841 }
2842 
2843 void dispc_remove(struct tidss_device *tidss)
2844 {
2845 	tidss->dispc = NULL;
2846 }
2847 
2848 static int dispc_iomap_resource(struct platform_device *pdev, const char *name,
2849 				void __iomem **base)
2850 {
2851 	void __iomem *b;
2852 
2853 	b = devm_platform_ioremap_resource_byname(pdev, name);
2854 	if (IS_ERR(b)) {
2855 		dev_err(&pdev->dev, "cannot ioremap resource '%s'\n", name);
2856 		return PTR_ERR(b);
2857 	}
2858 
2859 	*base = b;
2860 
2861 	return 0;
2862 }
2863 
2864 static int dispc_init_am65x_oldi_io_ctrl(struct device *dev,
2865 					 struct dispc_device *dispc)
2866 {
2867 	dispc->am65x_oldi_io_ctrl =
2868 		syscon_regmap_lookup_by_phandle(dev->of_node,
2869 						"ti,am65x-oldi-io-ctrl");
2870 	if (PTR_ERR(dispc->am65x_oldi_io_ctrl) == -ENODEV) {
2871 		dispc->am65x_oldi_io_ctrl = NULL;
2872 	} else if (IS_ERR(dispc->am65x_oldi_io_ctrl)) {
2873 		dev_err(dev, "%s: syscon_regmap_lookup_by_phandle failed %ld\n",
2874 			__func__, PTR_ERR(dispc->am65x_oldi_io_ctrl));
2875 		return PTR_ERR(dispc->am65x_oldi_io_ctrl);
2876 	}
2877 	return 0;
2878 }
2879 
2880 static void dispc_init_errata(struct dispc_device *dispc)
2881 {
2882 	static const struct soc_device_attribute am65x_sr10_soc_devices[] = {
2883 		{ .family = "AM65X", .revision = "SR1.0" },
2884 		{ /* sentinel */ }
2885 	};
2886 
2887 	if (soc_device_match(am65x_sr10_soc_devices)) {
2888 		dispc->errata.i2000 = true;
2889 		dev_info(dispc->dev, "WA for erratum i2000: YUV formats disabled\n");
2890 	}
2891 }
2892 
2893 /*
2894  * K2G display controller does not support soft reset, so we do a basic manual
2895  * reset here: make sure the IRQs are masked and VPs are disabled.
2896  */
2897 static void dispc_softreset_k2g(struct dispc_device *dispc)
2898 {
2899 	unsigned long flags;
2900 
2901 	spin_lock_irqsave(&dispc->tidss->irq_lock, flags);
2902 	dispc_set_irqenable(dispc, 0);
2903 	dispc_read_and_clear_irqstatus(dispc);
2904 	spin_unlock_irqrestore(&dispc->tidss->irq_lock, flags);
2905 
2906 	for (unsigned int vp_idx = 0; vp_idx < dispc->feat->num_vps; ++vp_idx)
2907 		VP_REG_FLD_MOD(dispc, vp_idx, DISPC_VP_CONTROL, 0,
2908 			       DISPC_VP_CONTROL_ENABLE_MASK);
2909 }
2910 
2911 static int dispc_softreset(struct dispc_device *dispc)
2912 {
2913 	u32 val;
2914 	int ret;
2915 
2916 	if (dispc->feat->subrev == DISPC_K2G) {
2917 		dispc_softreset_k2g(dispc);
2918 		return 0;
2919 	}
2920 
2921 	/* Soft reset */
2922 	REG_FLD_MOD(dispc, DSS_SYSCONFIG, 1, DSS_SYSCONFIG_SOFTRESET_MASK);
2923 	/* Wait for reset to complete */
2924 	ret = readl_poll_timeout(dispc->base_common + DSS_SYSSTATUS,
2925 				 val, val & 1, 100, 5000);
2926 	if (ret) {
2927 		dev_err(dispc->dev, "failed to reset dispc\n");
2928 		return ret;
2929 	}
2930 
2931 	return 0;
2932 }
2933 
2934 static int dispc_init_hw(struct dispc_device *dispc)
2935 {
2936 	struct device *dev = dispc->dev;
2937 	int ret;
2938 
2939 	ret = pm_runtime_set_active(dev);
2940 	if (ret) {
2941 		dev_err(dev, "Failed to set DSS PM to active\n");
2942 		return ret;
2943 	}
2944 
2945 	ret = clk_prepare_enable(dispc->fclk);
2946 	if (ret) {
2947 		dev_err(dev, "Failed to enable DSS fclk\n");
2948 		goto err_runtime_suspend;
2949 	}
2950 
2951 	ret = dispc_softreset(dispc);
2952 	if (ret)
2953 		goto err_clk_disable;
2954 
2955 	clk_disable_unprepare(dispc->fclk);
2956 	ret = pm_runtime_set_suspended(dev);
2957 	if (ret) {
2958 		dev_err(dev, "Failed to set DSS PM to suspended\n");
2959 		return ret;
2960 	}
2961 
2962 	return 0;
2963 
2964 err_clk_disable:
2965 	clk_disable_unprepare(dispc->fclk);
2966 
2967 err_runtime_suspend:
2968 	ret = pm_runtime_set_suspended(dev);
2969 	if (ret) {
2970 		dev_err(dev, "Failed to set DSS PM to suspended\n");
2971 		return ret;
2972 	}
2973 
2974 	return ret;
2975 }
2976 
2977 int dispc_init(struct tidss_device *tidss)
2978 {
2979 	struct device *dev = tidss->dev;
2980 	struct platform_device *pdev = to_platform_device(dev);
2981 	struct dispc_device *dispc;
2982 	const struct dispc_features *feat;
2983 	unsigned int i, num_fourccs;
2984 	int r = 0;
2985 
2986 	feat = tidss->feat;
2987 
2988 	if (feat->subrev != DISPC_K2G) {
2989 		r = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
2990 		if (r)
2991 			dev_warn(dev, "cannot set DMA masks to 48-bit\n");
2992 	}
2993 
2994 	dma_set_max_seg_size(dev, UINT_MAX);
2995 
2996 	dispc = devm_kzalloc(dev, sizeof(*dispc), GFP_KERNEL);
2997 	if (!dispc)
2998 		return -ENOMEM;
2999 
3000 	dispc->tidss = tidss;
3001 	dispc->dev = dev;
3002 	dispc->feat = feat;
3003 
3004 	dispc_init_errata(dispc);
3005 
3006 	dispc->fourccs = devm_kcalloc(dev, ARRAY_SIZE(dispc_color_formats),
3007 				      sizeof(*dispc->fourccs), GFP_KERNEL);
3008 	if (!dispc->fourccs)
3009 		return -ENOMEM;
3010 
3011 	num_fourccs = 0;
3012 	for (i = 0; i < ARRAY_SIZE(dispc_color_formats); ++i) {
3013 		if (dispc->errata.i2000 &&
3014 		    dispc_fourcc_is_yuv(dispc_color_formats[i].fourcc)) {
3015 			continue;
3016 		}
3017 		dispc->fourccs[num_fourccs++] = dispc_color_formats[i].fourcc;
3018 	}
3019 
3020 	dispc->num_fourccs = num_fourccs;
3021 
3022 	dispc_common_regmap = dispc->feat->common_regs;
3023 
3024 	r = dispc_iomap_resource(pdev, dispc->feat->common,
3025 				 &dispc->base_common);
3026 	if (r)
3027 		return r;
3028 
3029 	for (i = 0; i < dispc->feat->num_vids; i++) {
3030 		r = dispc_iomap_resource(pdev, dispc->feat->vid_info[i].name,
3031 					 &dispc->base_vid[i]);
3032 		if (r)
3033 			return r;
3034 	}
3035 
3036 	for (i = 0; i < dispc->feat->num_vps; i++) {
3037 		u32 gamma_size = dispc->feat->vp_feat.color.gamma_size;
3038 		u32 *gamma_table;
3039 		struct clk *clk;
3040 
3041 		r = dispc_iomap_resource(pdev, dispc->feat->ovr_name[i],
3042 					 &dispc->base_ovr[i]);
3043 		if (r)
3044 			return r;
3045 
3046 		r = dispc_iomap_resource(pdev, dispc->feat->vp_name[i],
3047 					 &dispc->base_vp[i]);
3048 		if (r)
3049 			return r;
3050 
3051 		clk = devm_clk_get(dev, dispc->feat->vpclk_name[i]);
3052 		if (IS_ERR(clk)) {
3053 			dev_err(dev, "%s: Failed to get clk %s:%ld\n", __func__,
3054 				dispc->feat->vpclk_name[i], PTR_ERR(clk));
3055 			return PTR_ERR(clk);
3056 		}
3057 		dispc->vp_clk[i] = clk;
3058 
3059 		gamma_table = devm_kmalloc_array(dev, gamma_size,
3060 						 sizeof(*gamma_table),
3061 						 GFP_KERNEL);
3062 		if (!gamma_table)
3063 			return -ENOMEM;
3064 		dispc->vp_data[i].gamma_table = gamma_table;
3065 	}
3066 
3067 	if (feat->subrev == DISPC_AM65X) {
3068 		r = dispc_init_am65x_oldi_io_ctrl(dev, dispc);
3069 		if (r)
3070 			return r;
3071 	}
3072 
3073 	dispc->fclk = devm_clk_get(dev, "fck");
3074 	if (IS_ERR(dispc->fclk)) {
3075 		dev_err(dev, "%s: Failed to get fclk: %ld\n",
3076 			__func__, PTR_ERR(dispc->fclk));
3077 		return PTR_ERR(dispc->fclk);
3078 	}
3079 	dev_dbg(dev, "DSS fclk %lu Hz\n", clk_get_rate(dispc->fclk));
3080 
3081 	of_property_read_u32(dispc->dev->of_node, "max-memory-bandwidth",
3082 			     &dispc->memory_bandwidth_limit);
3083 
3084 	r = dispc_init_hw(dispc);
3085 	if (r)
3086 		return r;
3087 
3088 	tidss->dispc = dispc;
3089 
3090 	return 0;
3091 }
3092