xref: /openbmc/linux/drivers/gpu/drm/amd/display/dc/dcn10/dcn10_optc.c (revision 25879d7b4986beba3f0d84762fe40d09fdc8b219)
1 /*
2  * Copyright 2012-15 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 
27 #include "reg_helper.h"
28 #include "dcn10_optc.h"
29 #include "dc.h"
30 #include "dc_trace.h"
31 
32 #define REG(reg)\
33 	optc1->tg_regs->reg
34 
35 #define CTX \
36 	optc1->base.ctx
37 
38 #undef FN
39 #define FN(reg_name, field_name) \
40 	optc1->tg_shift->field_name, optc1->tg_mask->field_name
41 
42 #define STATIC_SCREEN_EVENT_MASK_RANGETIMING_DOUBLE_BUFFER_UPDATE_EN 0x100
43 
44 /**
45 * apply_front_porch_workaround  TODO FPGA still need?
46 *
47 * This is a workaround for a bug that has existed since R5xx and has not been
48 * fixed keep Front porch at minimum 2 for Interlaced mode or 1 for progressive.
49 */
50 static void apply_front_porch_workaround(struct dc_crtc_timing *timing)
51 {
52 	if (timing->flags.INTERLACE == 1) {
53 		if (timing->v_front_porch < 2)
54 			timing->v_front_porch = 2;
55 	} else {
56 		if (timing->v_front_porch < 1)
57 			timing->v_front_porch = 1;
58 	}
59 }
60 
61 void optc1_program_global_sync(
62 		struct timing_generator *optc,
63 		int vready_offset,
64 		int vstartup_start,
65 		int vupdate_offset,
66 		int vupdate_width)
67 {
68 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
69 
70 	optc1->vready_offset = vready_offset;
71 	optc1->vstartup_start = vstartup_start;
72 	optc1->vupdate_offset = vupdate_offset;
73 	optc1->vupdate_width = vupdate_width;
74 
75 	if (optc1->vstartup_start == 0) {
76 		BREAK_TO_DEBUGGER();
77 		return;
78 	}
79 
80 	REG_SET(OTG_VSTARTUP_PARAM, 0,
81 		VSTARTUP_START, optc1->vstartup_start);
82 
83 	REG_SET_2(OTG_VUPDATE_PARAM, 0,
84 			VUPDATE_OFFSET, optc1->vupdate_offset,
85 			VUPDATE_WIDTH, optc1->vupdate_width);
86 
87 	REG_SET(OTG_VREADY_PARAM, 0,
88 			VREADY_OFFSET, optc1->vready_offset);
89 }
90 
91 static void optc1_disable_stereo(struct timing_generator *optc)
92 {
93 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
94 
95 	REG_SET(OTG_STEREO_CONTROL, 0,
96 		OTG_STEREO_EN, 0);
97 
98 	REG_SET_2(OTG_3D_STRUCTURE_CONTROL, 0,
99 		OTG_3D_STRUCTURE_EN, 0,
100 		OTG_3D_STRUCTURE_STEREO_SEL_OVR, 0);
101 }
102 
103 void optc1_setup_vertical_interrupt0(
104 		struct timing_generator *optc,
105 		uint32_t start_line,
106 		uint32_t end_line)
107 {
108 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
109 
110 	REG_SET_2(OTG_VERTICAL_INTERRUPT0_POSITION, 0,
111 			OTG_VERTICAL_INTERRUPT0_LINE_START, start_line,
112 			OTG_VERTICAL_INTERRUPT0_LINE_END, end_line);
113 }
114 
115 void optc1_setup_vertical_interrupt1(
116 		struct timing_generator *optc,
117 		uint32_t start_line)
118 {
119 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
120 
121 	REG_SET(OTG_VERTICAL_INTERRUPT1_POSITION, 0,
122 				OTG_VERTICAL_INTERRUPT1_LINE_START, start_line);
123 }
124 
125 void optc1_setup_vertical_interrupt2(
126 		struct timing_generator *optc,
127 		uint32_t start_line)
128 {
129 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
130 
131 	REG_SET(OTG_VERTICAL_INTERRUPT2_POSITION, 0,
132 			OTG_VERTICAL_INTERRUPT2_LINE_START, start_line);
133 }
134 
135 /**
136  * program_timing_generator   used by mode timing set
137  * Program CRTC Timing Registers - OTG_H_*, OTG_V_*, Pixel repetition.
138  * Including SYNC. Call BIOS command table to program Timings.
139  */
140 void optc1_program_timing(
141 	struct timing_generator *optc,
142 	const struct dc_crtc_timing *dc_crtc_timing,
143 	int vready_offset,
144 	int vstartup_start,
145 	int vupdate_offset,
146 	int vupdate_width,
147 	const enum signal_type signal,
148 	bool use_vbios)
149 {
150 	struct dc_crtc_timing patched_crtc_timing;
151 	uint32_t asic_blank_end;
152 	uint32_t asic_blank_start;
153 	uint32_t v_total;
154 	uint32_t v_sync_end;
155 	uint32_t h_sync_polarity, v_sync_polarity;
156 	uint32_t start_point = 0;
157 	uint32_t field_num = 0;
158 	enum h_timing_div_mode h_div = H_TIMING_NO_DIV;
159 
160 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
161 
162 	optc1->signal = signal;
163 	optc1->vready_offset = vready_offset;
164 	optc1->vstartup_start = vstartup_start;
165 	optc1->vupdate_offset = vupdate_offset;
166 	optc1->vupdate_width = vupdate_width;
167 	patched_crtc_timing = *dc_crtc_timing;
168 	apply_front_porch_workaround(&patched_crtc_timing);
169 	optc1->orginal_patched_timing = patched_crtc_timing;
170 
171 	/* Load horizontal timing */
172 
173 	/* CRTC_H_TOTAL = vesa.h_total - 1 */
174 	REG_SET(OTG_H_TOTAL, 0,
175 			OTG_H_TOTAL,  patched_crtc_timing.h_total - 1);
176 
177 	/* h_sync_start = 0, h_sync_end = vesa.h_sync_width */
178 	REG_UPDATE_2(OTG_H_SYNC_A,
179 			OTG_H_SYNC_A_START, 0,
180 			OTG_H_SYNC_A_END, patched_crtc_timing.h_sync_width);
181 
182 	/* blank_start = line end - front porch */
183 	asic_blank_start = patched_crtc_timing.h_total -
184 			patched_crtc_timing.h_front_porch;
185 
186 	/* blank_end = blank_start - active */
187 	asic_blank_end = asic_blank_start -
188 			patched_crtc_timing.h_border_right -
189 			patched_crtc_timing.h_addressable -
190 			patched_crtc_timing.h_border_left;
191 
192 	REG_UPDATE_2(OTG_H_BLANK_START_END,
193 			OTG_H_BLANK_START, asic_blank_start,
194 			OTG_H_BLANK_END, asic_blank_end);
195 
196 	/* h_sync polarity */
197 	h_sync_polarity = patched_crtc_timing.flags.HSYNC_POSITIVE_POLARITY ?
198 			0 : 1;
199 
200 	REG_UPDATE(OTG_H_SYNC_A_CNTL,
201 			OTG_H_SYNC_A_POL, h_sync_polarity);
202 
203 	v_total = patched_crtc_timing.v_total - 1;
204 
205 	REG_SET(OTG_V_TOTAL, 0,
206 			OTG_V_TOTAL, v_total);
207 
208 	/* In case of V_TOTAL_CONTROL is on, make sure OTG_V_TOTAL_MAX and
209 	 * OTG_V_TOTAL_MIN are equal to V_TOTAL.
210 	 */
211 	optc->funcs->set_vtotal_min_max(optc, v_total, v_total);
212 
213 	/* v_sync_start = 0, v_sync_end = v_sync_width */
214 	v_sync_end = patched_crtc_timing.v_sync_width;
215 
216 	REG_UPDATE_2(OTG_V_SYNC_A,
217 			OTG_V_SYNC_A_START, 0,
218 			OTG_V_SYNC_A_END, v_sync_end);
219 
220 	/* blank_start = frame end - front porch */
221 	asic_blank_start = patched_crtc_timing.v_total -
222 			patched_crtc_timing.v_front_porch;
223 
224 	/* blank_end = blank_start - active */
225 	asic_blank_end = asic_blank_start -
226 			patched_crtc_timing.v_border_bottom -
227 			patched_crtc_timing.v_addressable -
228 			patched_crtc_timing.v_border_top;
229 
230 	REG_UPDATE_2(OTG_V_BLANK_START_END,
231 			OTG_V_BLANK_START, asic_blank_start,
232 			OTG_V_BLANK_END, asic_blank_end);
233 
234 	/* v_sync polarity */
235 	v_sync_polarity = patched_crtc_timing.flags.VSYNC_POSITIVE_POLARITY ?
236 			0 : 1;
237 
238 	REG_UPDATE(OTG_V_SYNC_A_CNTL,
239 		OTG_V_SYNC_A_POL, v_sync_polarity);
240 
241 	if (optc1->signal == SIGNAL_TYPE_DISPLAY_PORT ||
242 			optc1->signal == SIGNAL_TYPE_DISPLAY_PORT_MST ||
243 			optc1->signal == SIGNAL_TYPE_EDP) {
244 		start_point = 1;
245 		if (patched_crtc_timing.flags.INTERLACE == 1)
246 			field_num = 1;
247 	}
248 
249 	/* Interlace */
250 	if (REG(OTG_INTERLACE_CONTROL)) {
251 		if (patched_crtc_timing.flags.INTERLACE == 1)
252 			REG_UPDATE(OTG_INTERLACE_CONTROL,
253 					OTG_INTERLACE_ENABLE, 1);
254 		else
255 			REG_UPDATE(OTG_INTERLACE_CONTROL,
256 					OTG_INTERLACE_ENABLE, 0);
257 	}
258 
259 	/* VTG enable set to 0 first VInit */
260 	REG_UPDATE(CONTROL,
261 			VTG0_ENABLE, 0);
262 
263 	/* original code is using VTG offset to address OTG reg, seems wrong */
264 	REG_UPDATE_2(OTG_CONTROL,
265 			OTG_START_POINT_CNTL, start_point,
266 			OTG_FIELD_NUMBER_CNTL, field_num);
267 
268 	optc->funcs->program_global_sync(optc,
269 			vready_offset,
270 			vstartup_start,
271 			vupdate_offset,
272 			vupdate_width);
273 
274 	optc->funcs->set_vtg_params(optc, dc_crtc_timing, true);
275 
276 	/* TODO
277 	 * patched_crtc_timing.flags.HORZ_COUNT_BY_TWO == 1
278 	 * program_horz_count_by_2
279 	 * for DVI 30bpp mode, 0 otherwise
280 	 * program_horz_count_by_2(optc, &patched_crtc_timing);
281 	 */
282 
283 	/* Enable stereo - only when we need to pack 3D frame. Other types
284 	 * of stereo handled in explicit call
285 	 */
286 
287 	if (optc1_is_two_pixels_per_containter(&patched_crtc_timing) || optc1->opp_count == 2)
288 		h_div = H_TIMING_DIV_BY2;
289 
290 	if (REG(OPTC_DATA_FORMAT_CONTROL) && optc1->tg_mask->OPTC_DATA_FORMAT != 0) {
291 		uint32_t data_fmt = 0;
292 
293 		if (patched_crtc_timing.pixel_encoding == PIXEL_ENCODING_YCBCR422)
294 			data_fmt = 1;
295 		else if (patched_crtc_timing.pixel_encoding == PIXEL_ENCODING_YCBCR420)
296 			data_fmt = 2;
297 
298 		REG_UPDATE(OPTC_DATA_FORMAT_CONTROL, OPTC_DATA_FORMAT, data_fmt);
299 	}
300 
301 	if (optc1->tg_mask->OTG_H_TIMING_DIV_MODE != 0) {
302 		if (optc1->opp_count == 4)
303 			h_div = H_TIMING_DIV_BY4;
304 
305 		REG_UPDATE(OTG_H_TIMING_CNTL,
306 		OTG_H_TIMING_DIV_MODE, h_div);
307 	} else {
308 		REG_UPDATE(OTG_H_TIMING_CNTL,
309 		OTG_H_TIMING_DIV_BY2, h_div);
310 	}
311 }
312 
313 /**
314  * optc1_set_vtg_params - Set Vertical Timing Generator (VTG) parameters
315  *
316  * @optc: timing_generator struct used to extract the optc parameters
317  * @dc_crtc_timing: Timing parameters configured
318  * @program_fp2: Boolean value indicating if FP2 will be programmed or not
319  *
320  * OTG is responsible for generating the global sync signals, including
321  * vertical timing information for each HUBP in the dcfclk domain. Each VTG is
322  * associated with one OTG that provides HUBP with vertical timing information
323  * (i.e., there is 1:1 correspondence between OTG and VTG). This function is
324  * responsible for setting the OTG parameters to the VTG during the pipe
325  * programming.
326  */
327 void optc1_set_vtg_params(struct timing_generator *optc,
328 		const struct dc_crtc_timing *dc_crtc_timing, bool program_fp2)
329 {
330 	struct dc_crtc_timing patched_crtc_timing;
331 	uint32_t asic_blank_end;
332 	uint32_t v_init;
333 	uint32_t v_fp2 = 0;
334 	int32_t vertical_line_start;
335 
336 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
337 
338 	patched_crtc_timing = *dc_crtc_timing;
339 	apply_front_porch_workaround(&patched_crtc_timing);
340 
341 	/* VCOUNT_INIT is the start of blank */
342 	v_init = patched_crtc_timing.v_total - patched_crtc_timing.v_front_porch;
343 
344 	/* end of blank = v_init - active */
345 	asic_blank_end = v_init -
346 			patched_crtc_timing.v_border_bottom -
347 			patched_crtc_timing.v_addressable -
348 			patched_crtc_timing.v_border_top;
349 
350 	/* if VSTARTUP is before VSYNC, FP2 is the offset, otherwise 0 */
351 	vertical_line_start = asic_blank_end - optc1->vstartup_start + 1;
352 	if (vertical_line_start < 0)
353 		v_fp2 = -vertical_line_start;
354 
355 	/* Interlace */
356 	if (REG(OTG_INTERLACE_CONTROL)) {
357 		if (patched_crtc_timing.flags.INTERLACE == 1) {
358 			v_init = v_init / 2;
359 			if ((optc1->vstartup_start/2)*2 > asic_blank_end)
360 				v_fp2 = v_fp2 / 2;
361 		}
362 	}
363 
364 	if (program_fp2)
365 		REG_UPDATE_2(CONTROL,
366 				VTG0_FP2, v_fp2,
367 				VTG0_VCOUNT_INIT, v_init);
368 	else
369 		REG_UPDATE(CONTROL, VTG0_VCOUNT_INIT, v_init);
370 }
371 
372 void optc1_set_blank_data_double_buffer(struct timing_generator *optc, bool enable)
373 {
374 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
375 
376 	uint32_t blank_data_double_buffer_enable = enable ? 1 : 0;
377 
378 	REG_UPDATE(OTG_DOUBLE_BUFFER_CONTROL,
379 			OTG_BLANK_DATA_DOUBLE_BUFFER_EN, blank_data_double_buffer_enable);
380 }
381 
382 /**
383  * optc1_set_timing_double_buffer() - DRR double buffering control
384  *
385  * Sets double buffer point for V_TOTAL, H_TOTAL, VTOTAL_MIN,
386  * VTOTAL_MAX, VTOTAL_MIN_SEL and VTOTAL_MAX_SEL registers.
387  *
388  * Options: any time,  start of frame, dp start of frame (range timing)
389  */
390 void optc1_set_timing_double_buffer(struct timing_generator *optc, bool enable)
391 {
392 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
393 	uint32_t mode = enable ? 2 : 0;
394 
395 	REG_UPDATE(OTG_DOUBLE_BUFFER_CONTROL,
396 		   OTG_RANGE_TIMING_DBUF_UPDATE_MODE, mode);
397 }
398 
399 /**
400  * unblank_crtc
401  * Call ASIC Control Object to UnBlank CRTC.
402  */
403 static void optc1_unblank_crtc(struct timing_generator *optc)
404 {
405 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
406 
407 	REG_UPDATE_2(OTG_BLANK_CONTROL,
408 			OTG_BLANK_DATA_EN, 0,
409 			OTG_BLANK_DE_MODE, 0);
410 
411 	/* W/A for automated testing
412 	 * Automated testing will fail underflow test as there
413 	 * sporadic underflows which occur during the optc blank
414 	 * sequence.  As a w/a, clear underflow on unblank.
415 	 * This prevents the failure, but will not mask actual
416 	 * underflow that affect real use cases.
417 	 */
418 	optc1_clear_optc_underflow(optc);
419 }
420 
421 /**
422  * blank_crtc
423  * Call ASIC Control Object to Blank CRTC.
424  */
425 
426 static void optc1_blank_crtc(struct timing_generator *optc)
427 {
428 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
429 
430 	REG_UPDATE_2(OTG_BLANK_CONTROL,
431 			OTG_BLANK_DATA_EN, 1,
432 			OTG_BLANK_DE_MODE, 0);
433 
434 	optc1_set_blank_data_double_buffer(optc, false);
435 }
436 
437 void optc1_set_blank(struct timing_generator *optc,
438 		bool enable_blanking)
439 {
440 	if (enable_blanking)
441 		optc1_blank_crtc(optc);
442 	else
443 		optc1_unblank_crtc(optc);
444 }
445 
446 bool optc1_is_blanked(struct timing_generator *optc)
447 {
448 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
449 	uint32_t blank_en;
450 	uint32_t blank_state;
451 
452 	REG_GET_2(OTG_BLANK_CONTROL,
453 			OTG_BLANK_DATA_EN, &blank_en,
454 			OTG_CURRENT_BLANK_STATE, &blank_state);
455 
456 	return blank_en && blank_state;
457 }
458 
459 void optc1_enable_optc_clock(struct timing_generator *optc, bool enable)
460 {
461 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
462 
463 	if (enable) {
464 		REG_UPDATE_2(OPTC_INPUT_CLOCK_CONTROL,
465 				OPTC_INPUT_CLK_EN, 1,
466 				OPTC_INPUT_CLK_GATE_DIS, 1);
467 
468 		REG_WAIT(OPTC_INPUT_CLOCK_CONTROL,
469 				OPTC_INPUT_CLK_ON, 1,
470 				1, 1000);
471 
472 		/* Enable clock */
473 		REG_UPDATE_2(OTG_CLOCK_CONTROL,
474 				OTG_CLOCK_EN, 1,
475 				OTG_CLOCK_GATE_DIS, 1);
476 		REG_WAIT(OTG_CLOCK_CONTROL,
477 				OTG_CLOCK_ON, 1,
478 				1, 1000);
479 	} else  {
480 
481 		//last chance to clear underflow, otherwise, it will always there due to clock is off.
482 		if (optc->funcs->is_optc_underflow_occurred(optc) == true)
483 			optc->funcs->clear_optc_underflow(optc);
484 
485 		REG_UPDATE_2(OTG_CLOCK_CONTROL,
486 				OTG_CLOCK_GATE_DIS, 0,
487 				OTG_CLOCK_EN, 0);
488 
489 		REG_UPDATE_2(OPTC_INPUT_CLOCK_CONTROL,
490 				OPTC_INPUT_CLK_GATE_DIS, 0,
491 				OPTC_INPUT_CLK_EN, 0);
492 	}
493 }
494 
495 /**
496  * Enable CRTC
497  * Enable CRTC - call ASIC Control Object to enable Timing generator.
498  */
499 static bool optc1_enable_crtc(struct timing_generator *optc)
500 {
501 	/* TODO FPGA wait for answer
502 	 * OTG_MASTER_UPDATE_MODE != CRTC_MASTER_UPDATE_MODE
503 	 * OTG_MASTER_UPDATE_LOCK != CRTC_MASTER_UPDATE_LOCK
504 	 */
505 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
506 
507 	/* opp instance for OTG. For DCN1.0, ODM is remoed.
508 	 * OPP and OPTC should 1:1 mapping
509 	 */
510 	REG_UPDATE(OPTC_DATA_SOURCE_SELECT,
511 			OPTC_SRC_SEL, optc->inst);
512 
513 	/* VTG enable first is for HW workaround */
514 	REG_UPDATE(CONTROL,
515 			VTG0_ENABLE, 1);
516 
517 	REG_SEQ_START();
518 
519 	/* Enable CRTC */
520 	REG_UPDATE_2(OTG_CONTROL,
521 			OTG_DISABLE_POINT_CNTL, 3,
522 			OTG_MASTER_EN, 1);
523 
524 	REG_SEQ_SUBMIT();
525 	REG_SEQ_WAIT_DONE();
526 
527 	return true;
528 }
529 
530 /* disable_crtc - call ASIC Control Object to disable Timing generator. */
531 bool optc1_disable_crtc(struct timing_generator *optc)
532 {
533 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
534 
535 	/* disable otg request until end of the first line
536 	 * in the vertical blank region
537 	 */
538 	REG_UPDATE_2(OTG_CONTROL,
539 			OTG_DISABLE_POINT_CNTL, 3,
540 			OTG_MASTER_EN, 0);
541 
542 	REG_UPDATE(CONTROL,
543 			VTG0_ENABLE, 0);
544 
545 	/* CRTC disabled, so disable  clock. */
546 	REG_WAIT(OTG_CLOCK_CONTROL,
547 			OTG_BUSY, 0,
548 			1, 100000);
549 
550 	return true;
551 }
552 
553 
554 void optc1_program_blank_color(
555 		struct timing_generator *optc,
556 		const struct tg_color *black_color)
557 {
558 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
559 
560 	REG_SET_3(OTG_BLACK_COLOR, 0,
561 			OTG_BLACK_COLOR_B_CB, black_color->color_b_cb,
562 			OTG_BLACK_COLOR_G_Y, black_color->color_g_y,
563 			OTG_BLACK_COLOR_R_CR, black_color->color_r_cr);
564 }
565 
566 bool optc1_validate_timing(
567 	struct timing_generator *optc,
568 	const struct dc_crtc_timing *timing)
569 {
570 	uint32_t v_blank;
571 	uint32_t h_blank;
572 	uint32_t min_v_blank;
573 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
574 
575 	ASSERT(timing != NULL);
576 
577 	v_blank = (timing->v_total - timing->v_addressable -
578 					timing->v_border_top - timing->v_border_bottom);
579 
580 	h_blank = (timing->h_total - timing->h_addressable -
581 		timing->h_border_right -
582 		timing->h_border_left);
583 
584 	if (timing->timing_3d_format != TIMING_3D_FORMAT_NONE &&
585 		timing->timing_3d_format != TIMING_3D_FORMAT_HW_FRAME_PACKING &&
586 		timing->timing_3d_format != TIMING_3D_FORMAT_TOP_AND_BOTTOM &&
587 		timing->timing_3d_format != TIMING_3D_FORMAT_SIDE_BY_SIDE &&
588 		timing->timing_3d_format != TIMING_3D_FORMAT_FRAME_ALTERNATE &&
589 		timing->timing_3d_format != TIMING_3D_FORMAT_INBAND_FA)
590 		return false;
591 
592 	/* Temporarily blocking interlacing mode until it's supported */
593 	if (timing->flags.INTERLACE == 1)
594 		return false;
595 
596 	/* Check maximum number of pixels supported by Timing Generator
597 	 * (Currently will never fail, in order to fail needs display which
598 	 * needs more than 8192 horizontal and
599 	 * more than 8192 vertical total pixels)
600 	 */
601 	if (timing->h_total > optc1->max_h_total ||
602 		timing->v_total > optc1->max_v_total)
603 		return false;
604 
605 
606 	if (h_blank < optc1->min_h_blank)
607 		return false;
608 
609 	if (timing->h_sync_width  < optc1->min_h_sync_width ||
610 		 timing->v_sync_width  < optc1->min_v_sync_width)
611 		return false;
612 
613 	min_v_blank = timing->flags.INTERLACE?optc1->min_v_blank_interlace:optc1->min_v_blank;
614 
615 	if (v_blank < min_v_blank)
616 		return false;
617 
618 	return true;
619 
620 }
621 
622 /*
623  * get_vblank_counter
624  *
625  * @brief
626  * Get counter for vertical blanks. use register CRTC_STATUS_FRAME_COUNT which
627  * holds the counter of frames.
628  *
629  * @param
630  * struct timing_generator *optc - [in] timing generator which controls the
631  * desired CRTC
632  *
633  * @return
634  * Counter of frames, which should equal to number of vblanks.
635  */
636 uint32_t optc1_get_vblank_counter(struct timing_generator *optc)
637 {
638 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
639 	uint32_t frame_count;
640 
641 	REG_GET(OTG_STATUS_FRAME_COUNT,
642 		OTG_FRAME_COUNT, &frame_count);
643 
644 	return frame_count;
645 }
646 
647 void optc1_lock(struct timing_generator *optc)
648 {
649 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
650 
651 	REG_SET(OTG_GLOBAL_CONTROL0, 0,
652 			OTG_MASTER_UPDATE_LOCK_SEL, optc->inst);
653 	REG_SET(OTG_MASTER_UPDATE_LOCK, 0,
654 			OTG_MASTER_UPDATE_LOCK, 1);
655 
656 	REG_WAIT(OTG_MASTER_UPDATE_LOCK,
657 			UPDATE_LOCK_STATUS, 1,
658 			1, 10);
659 
660 	TRACE_OPTC_LOCK_UNLOCK_STATE(optc1, optc->inst, true);
661 }
662 
663 void optc1_unlock(struct timing_generator *optc)
664 {
665 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
666 
667 	REG_SET(OTG_MASTER_UPDATE_LOCK, 0,
668 			OTG_MASTER_UPDATE_LOCK, 0);
669 
670 	TRACE_OPTC_LOCK_UNLOCK_STATE(optc1, optc->inst, false);
671 }
672 
673 void optc1_get_position(struct timing_generator *optc,
674 		struct crtc_position *position)
675 {
676 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
677 
678 	REG_GET_2(OTG_STATUS_POSITION,
679 			OTG_HORZ_COUNT, &position->horizontal_count,
680 			OTG_VERT_COUNT, &position->vertical_count);
681 
682 	REG_GET(OTG_NOM_VERT_POSITION,
683 			OTG_VERT_COUNT_NOM, &position->nominal_vcount);
684 }
685 
686 bool optc1_is_counter_moving(struct timing_generator *optc)
687 {
688 	struct crtc_position position1, position2;
689 
690 	optc->funcs->get_position(optc, &position1);
691 	optc->funcs->get_position(optc, &position2);
692 
693 	if (position1.horizontal_count == position2.horizontal_count &&
694 		position1.vertical_count == position2.vertical_count)
695 		return false;
696 	else
697 		return true;
698 }
699 
700 bool optc1_did_triggered_reset_occur(
701 	struct timing_generator *optc)
702 {
703 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
704 	uint32_t occurred_force, occurred_vsync;
705 
706 	REG_GET(OTG_FORCE_COUNT_NOW_CNTL,
707 		OTG_FORCE_COUNT_NOW_OCCURRED, &occurred_force);
708 
709 	REG_GET(OTG_VERT_SYNC_CONTROL,
710 		OTG_FORCE_VSYNC_NEXT_LINE_OCCURRED, &occurred_vsync);
711 
712 	return occurred_vsync != 0 || occurred_force != 0;
713 }
714 
715 void optc1_disable_reset_trigger(struct timing_generator *optc)
716 {
717 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
718 
719 	REG_WRITE(OTG_TRIGA_CNTL, 0);
720 
721 	REG_SET(OTG_FORCE_COUNT_NOW_CNTL, 0,
722 		OTG_FORCE_COUNT_NOW_CLEAR, 1);
723 
724 	REG_SET(OTG_VERT_SYNC_CONTROL, 0,
725 		OTG_FORCE_VSYNC_NEXT_LINE_CLEAR, 1);
726 }
727 
728 void optc1_enable_reset_trigger(struct timing_generator *optc, int source_tg_inst)
729 {
730 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
731 	uint32_t falling_edge;
732 
733 	REG_GET(OTG_V_SYNC_A_CNTL,
734 			OTG_V_SYNC_A_POL, &falling_edge);
735 
736 	if (falling_edge)
737 		REG_SET_3(OTG_TRIGA_CNTL, 0,
738 				/* vsync signal from selected OTG pipe based
739 				 * on OTG_TRIG_SOURCE_PIPE_SELECT setting
740 				 */
741 				OTG_TRIGA_SOURCE_SELECT, 20,
742 				OTG_TRIGA_SOURCE_PIPE_SELECT, source_tg_inst,
743 				/* always detect falling edge */
744 				OTG_TRIGA_FALLING_EDGE_DETECT_CNTL, 1);
745 	else
746 		REG_SET_3(OTG_TRIGA_CNTL, 0,
747 				/* vsync signal from selected OTG pipe based
748 				 * on OTG_TRIG_SOURCE_PIPE_SELECT setting
749 				 */
750 				OTG_TRIGA_SOURCE_SELECT, 20,
751 				OTG_TRIGA_SOURCE_PIPE_SELECT, source_tg_inst,
752 				/* always detect rising edge */
753 				OTG_TRIGA_RISING_EDGE_DETECT_CNTL, 1);
754 
755 	REG_SET(OTG_FORCE_COUNT_NOW_CNTL, 0,
756 			/* force H count to H_TOTAL and V count to V_TOTAL in
757 			 * progressive mode and V_TOTAL-1 in interlaced mode
758 			 */
759 			OTG_FORCE_COUNT_NOW_MODE, 2);
760 }
761 
762 void optc1_enable_crtc_reset(
763 		struct timing_generator *optc,
764 		int source_tg_inst,
765 		struct crtc_trigger_info *crtc_tp)
766 {
767 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
768 	uint32_t falling_edge = 0;
769 	uint32_t rising_edge = 0;
770 
771 	switch (crtc_tp->event) {
772 
773 	case CRTC_EVENT_VSYNC_RISING:
774 		rising_edge = 1;
775 		break;
776 
777 	case CRTC_EVENT_VSYNC_FALLING:
778 		falling_edge = 1;
779 		break;
780 	}
781 
782 	REG_SET_4(OTG_TRIGA_CNTL, 0,
783 		 /* vsync signal from selected OTG pipe based
784 		  * on OTG_TRIG_SOURCE_PIPE_SELECT setting
785 		  */
786 		  OTG_TRIGA_SOURCE_SELECT, 20,
787 		  OTG_TRIGA_SOURCE_PIPE_SELECT, source_tg_inst,
788 		  /* always detect falling edge */
789 		  OTG_TRIGA_RISING_EDGE_DETECT_CNTL, rising_edge,
790 		  OTG_TRIGA_FALLING_EDGE_DETECT_CNTL, falling_edge);
791 
792 	switch (crtc_tp->delay) {
793 	case TRIGGER_DELAY_NEXT_LINE:
794 		REG_SET(OTG_VERT_SYNC_CONTROL, 0,
795 				OTG_AUTO_FORCE_VSYNC_MODE, 1);
796 		break;
797 	case TRIGGER_DELAY_NEXT_PIXEL:
798 		REG_SET(OTG_FORCE_COUNT_NOW_CNTL, 0,
799 			/* force H count to H_TOTAL and V count to V_TOTAL in
800 			 * progressive mode and V_TOTAL-1 in interlaced mode
801 			 */
802 			OTG_FORCE_COUNT_NOW_MODE, 2);
803 		break;
804 	}
805 }
806 
807 void optc1_wait_for_state(struct timing_generator *optc,
808 		enum crtc_state state)
809 {
810 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
811 
812 	switch (state) {
813 	case CRTC_STATE_VBLANK:
814 		REG_WAIT(OTG_STATUS,
815 				OTG_V_BLANK, 1,
816 				1, 100000); /* 1 vupdate at 10hz */
817 		break;
818 
819 	case CRTC_STATE_VACTIVE:
820 		REG_WAIT(OTG_STATUS,
821 				OTG_V_ACTIVE_DISP, 1,
822 				1, 100000); /* 1 vupdate at 10hz */
823 		break;
824 
825 	default:
826 		break;
827 	}
828 }
829 
830 void optc1_set_early_control(
831 	struct timing_generator *optc,
832 	uint32_t early_cntl)
833 {
834 	/* asic design change, do not need this control
835 	 * empty for share caller logic
836 	 */
837 }
838 
839 
840 void optc1_set_static_screen_control(
841 	struct timing_generator *optc,
842 	uint32_t event_triggers,
843 	uint32_t num_frames)
844 {
845 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
846 
847 	// By register spec, it only takes 8 bit value
848 	if (num_frames > 0xFF)
849 		num_frames = 0xFF;
850 
851 	/* Bit 8 is no longer applicable in RV for PSR case,
852 	 * set bit 8 to 0 if given
853 	 */
854 	if ((event_triggers & STATIC_SCREEN_EVENT_MASK_RANGETIMING_DOUBLE_BUFFER_UPDATE_EN)
855 			!= 0)
856 		event_triggers = event_triggers &
857 		~STATIC_SCREEN_EVENT_MASK_RANGETIMING_DOUBLE_BUFFER_UPDATE_EN;
858 
859 	REG_SET_2(OTG_STATIC_SCREEN_CONTROL, 0,
860 			OTG_STATIC_SCREEN_EVENT_MASK, event_triggers,
861 			OTG_STATIC_SCREEN_FRAME_COUNT, num_frames);
862 }
863 
864 static void optc1_setup_manual_trigger(struct timing_generator *optc)
865 {
866 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
867 
868 	REG_SET(OTG_GLOBAL_CONTROL2, 0,
869 			MANUAL_FLOW_CONTROL_SEL, optc->inst);
870 
871 	REG_SET_8(OTG_TRIGA_CNTL, 0,
872 			OTG_TRIGA_SOURCE_SELECT, 22,
873 			OTG_TRIGA_SOURCE_PIPE_SELECT, optc->inst,
874 			OTG_TRIGA_RISING_EDGE_DETECT_CNTL, 1,
875 			OTG_TRIGA_FALLING_EDGE_DETECT_CNTL, 0,
876 			OTG_TRIGA_POLARITY_SELECT, 0,
877 			OTG_TRIGA_FREQUENCY_SELECT, 0,
878 			OTG_TRIGA_DELAY, 0,
879 			OTG_TRIGA_CLEAR, 1);
880 }
881 
882 static void optc1_program_manual_trigger(struct timing_generator *optc)
883 {
884 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
885 
886 	REG_SET(OTG_MANUAL_FLOW_CONTROL, 0,
887 			MANUAL_FLOW_CONTROL, 1);
888 
889 	REG_SET(OTG_MANUAL_FLOW_CONTROL, 0,
890 			MANUAL_FLOW_CONTROL, 0);
891 }
892 
893 
894 /**
895  *****************************************************************************
896  *  Function: set_drr
897  *
898  *  @brief
899  *     Program dynamic refresh rate registers m_OTGx_OTG_V_TOTAL_*.
900  *
901  *****************************************************************************
902  */
903 void optc1_set_drr(
904 	struct timing_generator *optc,
905 	const struct drr_params *params)
906 {
907 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
908 
909 	if (params != NULL &&
910 		params->vertical_total_max > 0 &&
911 		params->vertical_total_min > 0) {
912 
913 		if (params->vertical_total_mid != 0) {
914 
915 			REG_SET(OTG_V_TOTAL_MID, 0,
916 				OTG_V_TOTAL_MID, params->vertical_total_mid - 1);
917 
918 			REG_UPDATE_2(OTG_V_TOTAL_CONTROL,
919 					OTG_VTOTAL_MID_REPLACING_MAX_EN, 1,
920 					OTG_VTOTAL_MID_FRAME_NUM,
921 					(uint8_t)params->vertical_total_mid_frame_num);
922 
923 		}
924 
925 		optc->funcs->set_vtotal_min_max(optc, params->vertical_total_min - 1, params->vertical_total_max - 1);
926 
927 		REG_UPDATE_5(OTG_V_TOTAL_CONTROL,
928 				OTG_V_TOTAL_MIN_SEL, 1,
929 				OTG_V_TOTAL_MAX_SEL, 1,
930 				OTG_FORCE_LOCK_ON_EVENT, 0,
931 				OTG_SET_V_TOTAL_MIN_MASK_EN, 0,
932 				OTG_SET_V_TOTAL_MIN_MASK, 0);
933 
934 		// Setup manual flow control for EOF via TRIG_A
935 		optc->funcs->setup_manual_trigger(optc);
936 
937 	} else {
938 		REG_UPDATE_4(OTG_V_TOTAL_CONTROL,
939 				OTG_SET_V_TOTAL_MIN_MASK, 0,
940 				OTG_V_TOTAL_MIN_SEL, 0,
941 				OTG_V_TOTAL_MAX_SEL, 0,
942 				OTG_FORCE_LOCK_ON_EVENT, 0);
943 
944 		optc->funcs->set_vtotal_min_max(optc, 0, 0);
945 	}
946 }
947 
948 void optc1_set_vtotal_min_max(struct timing_generator *optc, int vtotal_min, int vtotal_max)
949 {
950 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
951 
952 	REG_SET(OTG_V_TOTAL_MAX, 0,
953 		OTG_V_TOTAL_MAX, vtotal_max);
954 
955 	REG_SET(OTG_V_TOTAL_MIN, 0,
956 		OTG_V_TOTAL_MIN, vtotal_min);
957 }
958 
959 static void optc1_set_test_pattern(
960 	struct timing_generator *optc,
961 	/* TODO: replace 'controller_dp_test_pattern' by 'test_pattern_mode'
962 	 * because this is not DP-specific (which is probably somewhere in DP
963 	 * encoder) */
964 	enum controller_dp_test_pattern test_pattern,
965 	enum dc_color_depth color_depth)
966 {
967 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
968 	enum test_pattern_color_format bit_depth;
969 	enum test_pattern_dyn_range dyn_range;
970 	enum test_pattern_mode mode;
971 	uint32_t pattern_mask;
972 	uint32_t pattern_data;
973 	/* color ramp generator mixes 16-bits color */
974 	uint32_t src_bpc = 16;
975 	/* requested bpc */
976 	uint32_t dst_bpc;
977 	uint32_t index;
978 	/* RGB values of the color bars.
979 	 * Produce two RGB colors: RGB0 - white (all Fs)
980 	 * and RGB1 - black (all 0s)
981 	 * (three RGB components for two colors)
982 	 */
983 	uint16_t src_color[6] = {0xFFFF, 0xFFFF, 0xFFFF, 0x0000,
984 						0x0000, 0x0000};
985 	/* dest color (converted to the specified color format) */
986 	uint16_t dst_color[6];
987 	uint32_t inc_base;
988 
989 	/* translate to bit depth */
990 	switch (color_depth) {
991 	case COLOR_DEPTH_666:
992 		bit_depth = TEST_PATTERN_COLOR_FORMAT_BPC_6;
993 	break;
994 	case COLOR_DEPTH_888:
995 		bit_depth = TEST_PATTERN_COLOR_FORMAT_BPC_8;
996 	break;
997 	case COLOR_DEPTH_101010:
998 		bit_depth = TEST_PATTERN_COLOR_FORMAT_BPC_10;
999 	break;
1000 	case COLOR_DEPTH_121212:
1001 		bit_depth = TEST_PATTERN_COLOR_FORMAT_BPC_12;
1002 	break;
1003 	default:
1004 		bit_depth = TEST_PATTERN_COLOR_FORMAT_BPC_8;
1005 	break;
1006 	}
1007 
1008 	switch (test_pattern) {
1009 	case CONTROLLER_DP_TEST_PATTERN_COLORSQUARES:
1010 	case CONTROLLER_DP_TEST_PATTERN_COLORSQUARES_CEA:
1011 	{
1012 		dyn_range = (test_pattern ==
1013 				CONTROLLER_DP_TEST_PATTERN_COLORSQUARES_CEA ?
1014 				TEST_PATTERN_DYN_RANGE_CEA :
1015 				TEST_PATTERN_DYN_RANGE_VESA);
1016 		mode = TEST_PATTERN_MODE_COLORSQUARES_RGB;
1017 
1018 		REG_UPDATE_2(OTG_TEST_PATTERN_PARAMETERS,
1019 				OTG_TEST_PATTERN_VRES, 6,
1020 				OTG_TEST_PATTERN_HRES, 6);
1021 
1022 		REG_UPDATE_4(OTG_TEST_PATTERN_CONTROL,
1023 				OTG_TEST_PATTERN_EN, 1,
1024 				OTG_TEST_PATTERN_MODE, mode,
1025 				OTG_TEST_PATTERN_DYNAMIC_RANGE, dyn_range,
1026 				OTG_TEST_PATTERN_COLOR_FORMAT, bit_depth);
1027 	}
1028 	break;
1029 
1030 	case CONTROLLER_DP_TEST_PATTERN_VERTICALBARS:
1031 	case CONTROLLER_DP_TEST_PATTERN_HORIZONTALBARS:
1032 	{
1033 		mode = (test_pattern ==
1034 			CONTROLLER_DP_TEST_PATTERN_VERTICALBARS ?
1035 			TEST_PATTERN_MODE_VERTICALBARS :
1036 			TEST_PATTERN_MODE_HORIZONTALBARS);
1037 
1038 		switch (bit_depth) {
1039 		case TEST_PATTERN_COLOR_FORMAT_BPC_6:
1040 			dst_bpc = 6;
1041 		break;
1042 		case TEST_PATTERN_COLOR_FORMAT_BPC_8:
1043 			dst_bpc = 8;
1044 		break;
1045 		case TEST_PATTERN_COLOR_FORMAT_BPC_10:
1046 			dst_bpc = 10;
1047 		break;
1048 		default:
1049 			dst_bpc = 8;
1050 		break;
1051 		}
1052 
1053 		/* adjust color to the required colorFormat */
1054 		for (index = 0; index < 6; index++) {
1055 			/* dst = 2^dstBpc * src / 2^srcBpc = src >>
1056 			 * (srcBpc - dstBpc);
1057 			 */
1058 			dst_color[index] =
1059 				src_color[index] >> (src_bpc - dst_bpc);
1060 		/* CRTC_TEST_PATTERN_DATA has 16 bits,
1061 		 * lowest 6 are hardwired to ZERO
1062 		 * color bits should be left aligned to MSB
1063 		 * XXXXXXXXXX000000 for 10 bit,
1064 		 * XXXXXXXX00000000 for 8 bit and XXXXXX0000000000 for 6
1065 		 */
1066 			dst_color[index] <<= (16 - dst_bpc);
1067 		}
1068 
1069 		REG_WRITE(OTG_TEST_PATTERN_PARAMETERS, 0);
1070 
1071 		/* We have to write the mask before data, similar to pipeline.
1072 		 * For example, for 8 bpc, if we want RGB0 to be magenta,
1073 		 * and RGB1 to be cyan,
1074 		 * we need to make 7 writes:
1075 		 * MASK   DATA
1076 		 * 000001 00000000 00000000                     set mask to R0
1077 		 * 000010 11111111 00000000     R0 255, 0xFF00, set mask to G0
1078 		 * 000100 00000000 00000000     G0 0,   0x0000, set mask to B0
1079 		 * 001000 11111111 00000000     B0 255, 0xFF00, set mask to R1
1080 		 * 010000 00000000 00000000     R1 0,   0x0000, set mask to G1
1081 		 * 100000 11111111 00000000     G1 255, 0xFF00, set mask to B1
1082 		 * 100000 11111111 00000000     B1 255, 0xFF00
1083 		 *
1084 		 * we will make a loop of 6 in which we prepare the mask,
1085 		 * then write, then prepare the color for next write.
1086 		 * first iteration will write mask only,
1087 		 * but each next iteration color prepared in
1088 		 * previous iteration will be written within new mask,
1089 		 * the last component will written separately,
1090 		 * mask is not changing between 6th and 7th write
1091 		 * and color will be prepared by last iteration
1092 		 */
1093 
1094 		/* write color, color values mask in CRTC_TEST_PATTERN_MASK
1095 		 * is B1, G1, R1, B0, G0, R0
1096 		 */
1097 		pattern_data = 0;
1098 		for (index = 0; index < 6; index++) {
1099 			/* prepare color mask, first write PATTERN_DATA
1100 			 * will have all zeros
1101 			 */
1102 			pattern_mask = (1 << index);
1103 
1104 			/* write color component */
1105 			REG_SET_2(OTG_TEST_PATTERN_COLOR, 0,
1106 					OTG_TEST_PATTERN_MASK, pattern_mask,
1107 					OTG_TEST_PATTERN_DATA, pattern_data);
1108 
1109 			/* prepare next color component,
1110 			 * will be written in the next iteration
1111 			 */
1112 			pattern_data = dst_color[index];
1113 		}
1114 		/* write last color component,
1115 		 * it's been already prepared in the loop
1116 		 */
1117 		REG_SET_2(OTG_TEST_PATTERN_COLOR, 0,
1118 				OTG_TEST_PATTERN_MASK, pattern_mask,
1119 				OTG_TEST_PATTERN_DATA, pattern_data);
1120 
1121 		/* enable test pattern */
1122 		REG_UPDATE_4(OTG_TEST_PATTERN_CONTROL,
1123 				OTG_TEST_PATTERN_EN, 1,
1124 				OTG_TEST_PATTERN_MODE, mode,
1125 				OTG_TEST_PATTERN_DYNAMIC_RANGE, 0,
1126 				OTG_TEST_PATTERN_COLOR_FORMAT, bit_depth);
1127 	}
1128 	break;
1129 
1130 	case CONTROLLER_DP_TEST_PATTERN_COLORRAMP:
1131 	{
1132 		mode = (bit_depth ==
1133 			TEST_PATTERN_COLOR_FORMAT_BPC_10 ?
1134 			TEST_PATTERN_MODE_DUALRAMP_RGB :
1135 			TEST_PATTERN_MODE_SINGLERAMP_RGB);
1136 
1137 		switch (bit_depth) {
1138 		case TEST_PATTERN_COLOR_FORMAT_BPC_6:
1139 			dst_bpc = 6;
1140 		break;
1141 		case TEST_PATTERN_COLOR_FORMAT_BPC_8:
1142 			dst_bpc = 8;
1143 		break;
1144 		case TEST_PATTERN_COLOR_FORMAT_BPC_10:
1145 			dst_bpc = 10;
1146 		break;
1147 		default:
1148 			dst_bpc = 8;
1149 		break;
1150 		}
1151 
1152 		/* increment for the first ramp for one color gradation
1153 		 * 1 gradation for 6-bit color is 2^10
1154 		 * gradations in 16-bit color
1155 		 */
1156 		inc_base = (src_bpc - dst_bpc);
1157 
1158 		switch (bit_depth) {
1159 		case TEST_PATTERN_COLOR_FORMAT_BPC_6:
1160 		{
1161 			REG_UPDATE_5(OTG_TEST_PATTERN_PARAMETERS,
1162 					OTG_TEST_PATTERN_INC0, inc_base,
1163 					OTG_TEST_PATTERN_INC1, 0,
1164 					OTG_TEST_PATTERN_HRES, 6,
1165 					OTG_TEST_PATTERN_VRES, 6,
1166 					OTG_TEST_PATTERN_RAMP0_OFFSET, 0);
1167 		}
1168 		break;
1169 		case TEST_PATTERN_COLOR_FORMAT_BPC_8:
1170 		{
1171 			REG_UPDATE_5(OTG_TEST_PATTERN_PARAMETERS,
1172 					OTG_TEST_PATTERN_INC0, inc_base,
1173 					OTG_TEST_PATTERN_INC1, 0,
1174 					OTG_TEST_PATTERN_HRES, 8,
1175 					OTG_TEST_PATTERN_VRES, 6,
1176 					OTG_TEST_PATTERN_RAMP0_OFFSET, 0);
1177 		}
1178 		break;
1179 		case TEST_PATTERN_COLOR_FORMAT_BPC_10:
1180 		{
1181 			REG_UPDATE_5(OTG_TEST_PATTERN_PARAMETERS,
1182 					OTG_TEST_PATTERN_INC0, inc_base,
1183 					OTG_TEST_PATTERN_INC1, inc_base + 2,
1184 					OTG_TEST_PATTERN_HRES, 8,
1185 					OTG_TEST_PATTERN_VRES, 5,
1186 					OTG_TEST_PATTERN_RAMP0_OFFSET, 384 << 6);
1187 		}
1188 		break;
1189 		default:
1190 		break;
1191 		}
1192 
1193 		REG_WRITE(OTG_TEST_PATTERN_COLOR, 0);
1194 
1195 		/* enable test pattern */
1196 		REG_WRITE(OTG_TEST_PATTERN_CONTROL, 0);
1197 
1198 		REG_SET_4(OTG_TEST_PATTERN_CONTROL, 0,
1199 				OTG_TEST_PATTERN_EN, 1,
1200 				OTG_TEST_PATTERN_MODE, mode,
1201 				OTG_TEST_PATTERN_DYNAMIC_RANGE, 0,
1202 				OTG_TEST_PATTERN_COLOR_FORMAT, bit_depth);
1203 	}
1204 	break;
1205 	case CONTROLLER_DP_TEST_PATTERN_VIDEOMODE:
1206 	{
1207 		REG_WRITE(OTG_TEST_PATTERN_CONTROL, 0);
1208 		REG_WRITE(OTG_TEST_PATTERN_COLOR, 0);
1209 		REG_WRITE(OTG_TEST_PATTERN_PARAMETERS, 0);
1210 	}
1211 	break;
1212 	default:
1213 		break;
1214 
1215 	}
1216 }
1217 
1218 void optc1_get_crtc_scanoutpos(
1219 	struct timing_generator *optc,
1220 	uint32_t *v_blank_start,
1221 	uint32_t *v_blank_end,
1222 	uint32_t *h_position,
1223 	uint32_t *v_position)
1224 {
1225 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1226 	struct crtc_position position;
1227 
1228 	REG_GET_2(OTG_V_BLANK_START_END,
1229 			OTG_V_BLANK_START, v_blank_start,
1230 			OTG_V_BLANK_END, v_blank_end);
1231 
1232 	optc1_get_position(optc, &position);
1233 
1234 	*h_position = position.horizontal_count;
1235 	*v_position = position.vertical_count;
1236 }
1237 
1238 static void optc1_enable_stereo(struct timing_generator *optc,
1239 	const struct dc_crtc_timing *timing, struct crtc_stereo_flags *flags)
1240 {
1241 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1242 
1243 	if (flags) {
1244 		uint32_t stereo_en;
1245 		stereo_en = flags->FRAME_PACKED == 0 ? 1 : 0;
1246 
1247 		if (flags->PROGRAM_STEREO)
1248 			REG_UPDATE_3(OTG_STEREO_CONTROL,
1249 				OTG_STEREO_EN, stereo_en,
1250 				OTG_STEREO_SYNC_OUTPUT_LINE_NUM, 0,
1251 				OTG_STEREO_SYNC_OUTPUT_POLARITY, flags->RIGHT_EYE_POLARITY == 0 ? 0 : 1);
1252 
1253 		if (flags->PROGRAM_POLARITY)
1254 			REG_UPDATE(OTG_STEREO_CONTROL,
1255 				OTG_STEREO_EYE_FLAG_POLARITY,
1256 				flags->RIGHT_EYE_POLARITY == 0 ? 0 : 1);
1257 
1258 		if (flags->DISABLE_STEREO_DP_SYNC)
1259 			REG_UPDATE(OTG_STEREO_CONTROL,
1260 				OTG_DISABLE_STEREOSYNC_OUTPUT_FOR_DP, 1);
1261 
1262 		if (flags->PROGRAM_STEREO)
1263 			REG_UPDATE_2(OTG_3D_STRUCTURE_CONTROL,
1264 				OTG_3D_STRUCTURE_EN, flags->FRAME_PACKED,
1265 				OTG_3D_STRUCTURE_STEREO_SEL_OVR, flags->FRAME_PACKED);
1266 
1267 	}
1268 }
1269 
1270 void optc1_program_stereo(struct timing_generator *optc,
1271 	const struct dc_crtc_timing *timing, struct crtc_stereo_flags *flags)
1272 {
1273 	if (flags->PROGRAM_STEREO)
1274 		optc1_enable_stereo(optc, timing, flags);
1275 	else
1276 		optc1_disable_stereo(optc);
1277 }
1278 
1279 
1280 bool optc1_is_stereo_left_eye(struct timing_generator *optc)
1281 {
1282 	bool ret = false;
1283 	uint32_t left_eye = 0;
1284 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1285 
1286 	REG_GET(OTG_STEREO_STATUS,
1287 		OTG_STEREO_CURRENT_EYE, &left_eye);
1288 	if (left_eye == 1)
1289 		ret = true;
1290 	else
1291 		ret = false;
1292 
1293 	return ret;
1294 }
1295 
1296 bool optc1_get_hw_timing(struct timing_generator *tg,
1297 		struct dc_crtc_timing *hw_crtc_timing)
1298 {
1299 	struct dcn_otg_state s = {0};
1300 
1301 	if (tg == NULL || hw_crtc_timing == NULL)
1302 		return false;
1303 
1304 	optc1_read_otg_state(DCN10TG_FROM_TG(tg), &s);
1305 
1306 	hw_crtc_timing->h_total = s.h_total + 1;
1307 	hw_crtc_timing->h_addressable = s.h_total - ((s.h_total - s.h_blank_start) + s.h_blank_end);
1308 	hw_crtc_timing->h_front_porch = s.h_total + 1 - s.h_blank_start;
1309 	hw_crtc_timing->h_sync_width = s.h_sync_a_end - s.h_sync_a_start;
1310 
1311 	hw_crtc_timing->v_total = s.v_total + 1;
1312 	hw_crtc_timing->v_addressable = s.v_total - ((s.v_total - s.v_blank_start) + s.v_blank_end);
1313 	hw_crtc_timing->v_front_porch = s.v_total + 1 - s.v_blank_start;
1314 	hw_crtc_timing->v_sync_width = s.v_sync_a_end - s.v_sync_a_start;
1315 
1316 	return true;
1317 }
1318 
1319 
1320 void optc1_read_otg_state(struct optc *optc1,
1321 		struct dcn_otg_state *s)
1322 {
1323 	REG_GET(OTG_CONTROL,
1324 			OTG_MASTER_EN, &s->otg_enabled);
1325 
1326 	REG_GET_2(OTG_V_BLANK_START_END,
1327 			OTG_V_BLANK_START, &s->v_blank_start,
1328 			OTG_V_BLANK_END, &s->v_blank_end);
1329 
1330 	REG_GET(OTG_V_SYNC_A_CNTL,
1331 			OTG_V_SYNC_A_POL, &s->v_sync_a_pol);
1332 
1333 	REG_GET(OTG_V_TOTAL,
1334 			OTG_V_TOTAL, &s->v_total);
1335 
1336 	REG_GET(OTG_V_TOTAL_MAX,
1337 			OTG_V_TOTAL_MAX, &s->v_total_max);
1338 
1339 	REG_GET(OTG_V_TOTAL_MIN,
1340 			OTG_V_TOTAL_MIN, &s->v_total_min);
1341 
1342 	REG_GET(OTG_V_TOTAL_CONTROL,
1343 			OTG_V_TOTAL_MAX_SEL, &s->v_total_max_sel);
1344 
1345 	REG_GET(OTG_V_TOTAL_CONTROL,
1346 			OTG_V_TOTAL_MIN_SEL, &s->v_total_min_sel);
1347 
1348 	REG_GET_2(OTG_V_SYNC_A,
1349 			OTG_V_SYNC_A_START, &s->v_sync_a_start,
1350 			OTG_V_SYNC_A_END, &s->v_sync_a_end);
1351 
1352 	REG_GET_2(OTG_H_BLANK_START_END,
1353 			OTG_H_BLANK_START, &s->h_blank_start,
1354 			OTG_H_BLANK_END, &s->h_blank_end);
1355 
1356 	REG_GET_2(OTG_H_SYNC_A,
1357 			OTG_H_SYNC_A_START, &s->h_sync_a_start,
1358 			OTG_H_SYNC_A_END, &s->h_sync_a_end);
1359 
1360 	REG_GET(OTG_H_SYNC_A_CNTL,
1361 			OTG_H_SYNC_A_POL, &s->h_sync_a_pol);
1362 
1363 	REG_GET(OTG_H_TOTAL,
1364 			OTG_H_TOTAL, &s->h_total);
1365 
1366 	REG_GET(OPTC_INPUT_GLOBAL_CONTROL,
1367 			OPTC_UNDERFLOW_OCCURRED_STATUS, &s->underflow_occurred_status);
1368 
1369 	REG_GET(OTG_VERTICAL_INTERRUPT1_CONTROL,
1370 			OTG_VERTICAL_INTERRUPT1_INT_ENABLE, &s->vertical_interrupt1_en);
1371 
1372 	REG_GET(OTG_VERTICAL_INTERRUPT1_POSITION,
1373 				OTG_VERTICAL_INTERRUPT1_LINE_START, &s->vertical_interrupt1_line);
1374 
1375 	REG_GET(OTG_VERTICAL_INTERRUPT2_CONTROL,
1376 			OTG_VERTICAL_INTERRUPT2_INT_ENABLE, &s->vertical_interrupt2_en);
1377 
1378 	REG_GET(OTG_VERTICAL_INTERRUPT2_POSITION,
1379 			OTG_VERTICAL_INTERRUPT2_LINE_START, &s->vertical_interrupt2_line);
1380 }
1381 
1382 bool optc1_get_otg_active_size(struct timing_generator *optc,
1383 		uint32_t *otg_active_width,
1384 		uint32_t *otg_active_height)
1385 {
1386 	uint32_t otg_enabled;
1387 	uint32_t v_blank_start;
1388 	uint32_t v_blank_end;
1389 	uint32_t h_blank_start;
1390 	uint32_t h_blank_end;
1391 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1392 
1393 
1394 	REG_GET(OTG_CONTROL,
1395 			OTG_MASTER_EN, &otg_enabled);
1396 
1397 	if (otg_enabled == 0)
1398 		return false;
1399 
1400 	REG_GET_2(OTG_V_BLANK_START_END,
1401 			OTG_V_BLANK_START, &v_blank_start,
1402 			OTG_V_BLANK_END, &v_blank_end);
1403 
1404 	REG_GET_2(OTG_H_BLANK_START_END,
1405 			OTG_H_BLANK_START, &h_blank_start,
1406 			OTG_H_BLANK_END, &h_blank_end);
1407 
1408 	*otg_active_width = v_blank_start - v_blank_end;
1409 	*otg_active_height = h_blank_start - h_blank_end;
1410 	return true;
1411 }
1412 
1413 void optc1_clear_optc_underflow(struct timing_generator *optc)
1414 {
1415 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1416 
1417 	REG_UPDATE(OPTC_INPUT_GLOBAL_CONTROL, OPTC_UNDERFLOW_CLEAR, 1);
1418 }
1419 
1420 void optc1_tg_init(struct timing_generator *optc)
1421 {
1422 	optc1_set_blank_data_double_buffer(optc, true);
1423 	optc1_set_timing_double_buffer(optc, true);
1424 	optc1_clear_optc_underflow(optc);
1425 }
1426 
1427 bool optc1_is_tg_enabled(struct timing_generator *optc)
1428 {
1429 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1430 	uint32_t otg_enabled = 0;
1431 
1432 	REG_GET(OTG_CONTROL, OTG_MASTER_EN, &otg_enabled);
1433 
1434 	return (otg_enabled != 0);
1435 
1436 }
1437 
1438 bool optc1_is_optc_underflow_occurred(struct timing_generator *optc)
1439 {
1440 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1441 	uint32_t underflow_occurred = 0;
1442 
1443 	REG_GET(OPTC_INPUT_GLOBAL_CONTROL,
1444 			OPTC_UNDERFLOW_OCCURRED_STATUS,
1445 			&underflow_occurred);
1446 
1447 	return (underflow_occurred == 1);
1448 }
1449 
1450 bool optc1_configure_crc(struct timing_generator *optc,
1451 			  const struct crc_params *params)
1452 {
1453 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1454 
1455 	/* Cannot configure crc on a CRTC that is disabled */
1456 	if (!optc1_is_tg_enabled(optc))
1457 		return false;
1458 
1459 	REG_WRITE(OTG_CRC_CNTL, 0);
1460 
1461 	if (!params->enable)
1462 		return true;
1463 
1464 	/* Program frame boundaries */
1465 	/* Window A x axis start and end. */
1466 	REG_UPDATE_2(OTG_CRC0_WINDOWA_X_CONTROL,
1467 			OTG_CRC0_WINDOWA_X_START, params->windowa_x_start,
1468 			OTG_CRC0_WINDOWA_X_END, params->windowa_x_end);
1469 
1470 	/* Window A y axis start and end. */
1471 	REG_UPDATE_2(OTG_CRC0_WINDOWA_Y_CONTROL,
1472 			OTG_CRC0_WINDOWA_Y_START, params->windowa_y_start,
1473 			OTG_CRC0_WINDOWA_Y_END, params->windowa_y_end);
1474 
1475 	/* Window B x axis start and end. */
1476 	REG_UPDATE_2(OTG_CRC0_WINDOWB_X_CONTROL,
1477 			OTG_CRC0_WINDOWB_X_START, params->windowb_x_start,
1478 			OTG_CRC0_WINDOWB_X_END, params->windowb_x_end);
1479 
1480 	/* Window B y axis start and end. */
1481 	REG_UPDATE_2(OTG_CRC0_WINDOWB_Y_CONTROL,
1482 			OTG_CRC0_WINDOWB_Y_START, params->windowb_y_start,
1483 			OTG_CRC0_WINDOWB_Y_END, params->windowb_y_end);
1484 
1485 	/* Set crc mode and selection, and enable. Only using CRC0*/
1486 	REG_UPDATE_3(OTG_CRC_CNTL,
1487 			OTG_CRC_CONT_EN, params->continuous_mode ? 1 : 0,
1488 			OTG_CRC0_SELECT, params->selection,
1489 			OTG_CRC_EN, 1);
1490 
1491 	return true;
1492 }
1493 
1494 /**
1495  * optc1_get_crc - Capture CRC result per component
1496  *
1497  * @optc: timing_generator instance.
1498  * @r_cr: 16-bit primary CRC signature for red data.
1499  * @g_y: 16-bit primary CRC signature for green data.
1500  * @b_cb: 16-bit primary CRC signature for blue data.
1501  *
1502  * This function reads the CRC signature from the OPTC registers. Notice that
1503  * we have three registers to keep the CRC result per color component (RGB).
1504  *
1505  * Returns:
1506  * If CRC is disabled, return false; otherwise, return true, and the CRC
1507  * results in the parameters.
1508  */
1509 bool optc1_get_crc(struct timing_generator *optc,
1510 		   uint32_t *r_cr, uint32_t *g_y, uint32_t *b_cb)
1511 {
1512 	uint32_t field = 0;
1513 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1514 
1515 	REG_GET(OTG_CRC_CNTL, OTG_CRC_EN, &field);
1516 
1517 	/* Early return if CRC is not enabled for this CRTC */
1518 	if (!field)
1519 		return false;
1520 
1521 	/* OTG_CRC0_DATA_RG has the CRC16 results for the red and green component */
1522 	REG_GET_2(OTG_CRC0_DATA_RG,
1523 		  CRC0_R_CR, r_cr,
1524 		  CRC0_G_Y, g_y);
1525 
1526 	/* OTG_CRC0_DATA_B has the CRC16 results for the blue component */
1527 	REG_GET(OTG_CRC0_DATA_B,
1528 		CRC0_B_CB, b_cb);
1529 
1530 	return true;
1531 }
1532 
1533 static const struct timing_generator_funcs dcn10_tg_funcs = {
1534 		.validate_timing = optc1_validate_timing,
1535 		.program_timing = optc1_program_timing,
1536 		.setup_vertical_interrupt0 = optc1_setup_vertical_interrupt0,
1537 		.setup_vertical_interrupt1 = optc1_setup_vertical_interrupt1,
1538 		.setup_vertical_interrupt2 = optc1_setup_vertical_interrupt2,
1539 		.program_global_sync = optc1_program_global_sync,
1540 		.enable_crtc = optc1_enable_crtc,
1541 		.disable_crtc = optc1_disable_crtc,
1542 		/* used by enable_timing_synchronization. Not need for FPGA */
1543 		.is_counter_moving = optc1_is_counter_moving,
1544 		.get_position = optc1_get_position,
1545 		.get_frame_count = optc1_get_vblank_counter,
1546 		.get_scanoutpos = optc1_get_crtc_scanoutpos,
1547 		.get_otg_active_size = optc1_get_otg_active_size,
1548 		.set_early_control = optc1_set_early_control,
1549 		/* used by enable_timing_synchronization. Not need for FPGA */
1550 		.wait_for_state = optc1_wait_for_state,
1551 		.set_blank = optc1_set_blank,
1552 		.is_blanked = optc1_is_blanked,
1553 		.set_blank_color = optc1_program_blank_color,
1554 		.did_triggered_reset_occur = optc1_did_triggered_reset_occur,
1555 		.enable_reset_trigger = optc1_enable_reset_trigger,
1556 		.enable_crtc_reset = optc1_enable_crtc_reset,
1557 		.disable_reset_trigger = optc1_disable_reset_trigger,
1558 		.lock = optc1_lock,
1559 		.unlock = optc1_unlock,
1560 		.enable_optc_clock = optc1_enable_optc_clock,
1561 		.set_drr = optc1_set_drr,
1562 		.get_last_used_drr_vtotal = NULL,
1563 		.set_vtotal_min_max = optc1_set_vtotal_min_max,
1564 		.set_static_screen_control = optc1_set_static_screen_control,
1565 		.set_test_pattern = optc1_set_test_pattern,
1566 		.program_stereo = optc1_program_stereo,
1567 		.is_stereo_left_eye = optc1_is_stereo_left_eye,
1568 		.set_blank_data_double_buffer = optc1_set_blank_data_double_buffer,
1569 		.tg_init = optc1_tg_init,
1570 		.is_tg_enabled = optc1_is_tg_enabled,
1571 		.is_optc_underflow_occurred = optc1_is_optc_underflow_occurred,
1572 		.clear_optc_underflow = optc1_clear_optc_underflow,
1573 		.get_crc = optc1_get_crc,
1574 		.configure_crc = optc1_configure_crc,
1575 		.set_vtg_params = optc1_set_vtg_params,
1576 		.program_manual_trigger = optc1_program_manual_trigger,
1577 		.setup_manual_trigger = optc1_setup_manual_trigger,
1578 		.get_hw_timing = optc1_get_hw_timing,
1579 };
1580 
1581 void dcn10_timing_generator_init(struct optc *optc1)
1582 {
1583 	optc1->base.funcs = &dcn10_tg_funcs;
1584 
1585 	optc1->max_h_total = optc1->tg_mask->OTG_H_TOTAL + 1;
1586 	optc1->max_v_total = optc1->tg_mask->OTG_V_TOTAL + 1;
1587 
1588 	optc1->min_h_blank = 32;
1589 	optc1->min_v_blank = 3;
1590 	optc1->min_v_blank_interlace = 5;
1591 	optc1->min_h_sync_width = 4;
1592 	optc1->min_v_sync_width = 1;
1593 }
1594 
1595 /* "Containter" vs. "pixel" is a concept within HW blocks, mostly those closer to the back-end. It works like this:
1596  *
1597  * - In most of the formats (RGB or YCbCr 4:4:4, 4:2:2 uncompressed and DSC 4:2:2 Simple) pixel rate is the same as
1598  *   containter rate.
1599  *
1600  * - In 4:2:0 (DSC or uncompressed) there are two pixels per container, hence the target container rate has to be
1601  *   halved to maintain the correct pixel rate.
1602  *
1603  * - Unlike 4:2:2 uncompressed, DSC 4:2:2 Native also has two pixels per container (this happens when DSC is applied
1604  *   to it) and has to be treated the same as 4:2:0, i.e. target containter rate has to be halved in this case as well.
1605  *
1606  */
1607 bool optc1_is_two_pixels_per_containter(const struct dc_crtc_timing *timing)
1608 {
1609 	bool two_pix = timing->pixel_encoding == PIXEL_ENCODING_YCBCR420;
1610 
1611 	two_pix = two_pix || (timing->flags.DSC && timing->pixel_encoding == PIXEL_ENCODING_YCBCR422
1612 			&& !timing->dsc_cfg.ycbcr422_simple);
1613 	return two_pix;
1614 }
1615 
1616