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 
935 	// Setup manual flow control for EOF via TRIG_A
936 	optc->funcs->setup_manual_trigger(optc);
937 }
938 
939 void optc1_set_vtotal_min_max(struct timing_generator *optc, int vtotal_min, int vtotal_max)
940 {
941 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
942 
943 	REG_SET(OTG_V_TOTAL_MAX, 0,
944 		OTG_V_TOTAL_MAX, vtotal_max);
945 
946 	REG_SET(OTG_V_TOTAL_MIN, 0,
947 		OTG_V_TOTAL_MIN, vtotal_min);
948 }
949 
950 static void optc1_set_test_pattern(
951 	struct timing_generator *optc,
952 	/* TODO: replace 'controller_dp_test_pattern' by 'test_pattern_mode'
953 	 * because this is not DP-specific (which is probably somewhere in DP
954 	 * encoder) */
955 	enum controller_dp_test_pattern test_pattern,
956 	enum dc_color_depth color_depth)
957 {
958 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
959 	enum test_pattern_color_format bit_depth;
960 	enum test_pattern_dyn_range dyn_range;
961 	enum test_pattern_mode mode;
962 	uint32_t pattern_mask;
963 	uint32_t pattern_data;
964 	/* color ramp generator mixes 16-bits color */
965 	uint32_t src_bpc = 16;
966 	/* requested bpc */
967 	uint32_t dst_bpc;
968 	uint32_t index;
969 	/* RGB values of the color bars.
970 	 * Produce two RGB colors: RGB0 - white (all Fs)
971 	 * and RGB1 - black (all 0s)
972 	 * (three RGB components for two colors)
973 	 */
974 	uint16_t src_color[6] = {0xFFFF, 0xFFFF, 0xFFFF, 0x0000,
975 						0x0000, 0x0000};
976 	/* dest color (converted to the specified color format) */
977 	uint16_t dst_color[6];
978 	uint32_t inc_base;
979 
980 	/* translate to bit depth */
981 	switch (color_depth) {
982 	case COLOR_DEPTH_666:
983 		bit_depth = TEST_PATTERN_COLOR_FORMAT_BPC_6;
984 	break;
985 	case COLOR_DEPTH_888:
986 		bit_depth = TEST_PATTERN_COLOR_FORMAT_BPC_8;
987 	break;
988 	case COLOR_DEPTH_101010:
989 		bit_depth = TEST_PATTERN_COLOR_FORMAT_BPC_10;
990 	break;
991 	case COLOR_DEPTH_121212:
992 		bit_depth = TEST_PATTERN_COLOR_FORMAT_BPC_12;
993 	break;
994 	default:
995 		bit_depth = TEST_PATTERN_COLOR_FORMAT_BPC_8;
996 	break;
997 	}
998 
999 	switch (test_pattern) {
1000 	case CONTROLLER_DP_TEST_PATTERN_COLORSQUARES:
1001 	case CONTROLLER_DP_TEST_PATTERN_COLORSQUARES_CEA:
1002 	{
1003 		dyn_range = (test_pattern ==
1004 				CONTROLLER_DP_TEST_PATTERN_COLORSQUARES_CEA ?
1005 				TEST_PATTERN_DYN_RANGE_CEA :
1006 				TEST_PATTERN_DYN_RANGE_VESA);
1007 		mode = TEST_PATTERN_MODE_COLORSQUARES_RGB;
1008 
1009 		REG_UPDATE_2(OTG_TEST_PATTERN_PARAMETERS,
1010 				OTG_TEST_PATTERN_VRES, 6,
1011 				OTG_TEST_PATTERN_HRES, 6);
1012 
1013 		REG_UPDATE_4(OTG_TEST_PATTERN_CONTROL,
1014 				OTG_TEST_PATTERN_EN, 1,
1015 				OTG_TEST_PATTERN_MODE, mode,
1016 				OTG_TEST_PATTERN_DYNAMIC_RANGE, dyn_range,
1017 				OTG_TEST_PATTERN_COLOR_FORMAT, bit_depth);
1018 	}
1019 	break;
1020 
1021 	case CONTROLLER_DP_TEST_PATTERN_VERTICALBARS:
1022 	case CONTROLLER_DP_TEST_PATTERN_HORIZONTALBARS:
1023 	{
1024 		mode = (test_pattern ==
1025 			CONTROLLER_DP_TEST_PATTERN_VERTICALBARS ?
1026 			TEST_PATTERN_MODE_VERTICALBARS :
1027 			TEST_PATTERN_MODE_HORIZONTALBARS);
1028 
1029 		switch (bit_depth) {
1030 		case TEST_PATTERN_COLOR_FORMAT_BPC_6:
1031 			dst_bpc = 6;
1032 		break;
1033 		case TEST_PATTERN_COLOR_FORMAT_BPC_8:
1034 			dst_bpc = 8;
1035 		break;
1036 		case TEST_PATTERN_COLOR_FORMAT_BPC_10:
1037 			dst_bpc = 10;
1038 		break;
1039 		default:
1040 			dst_bpc = 8;
1041 		break;
1042 		}
1043 
1044 		/* adjust color to the required colorFormat */
1045 		for (index = 0; index < 6; index++) {
1046 			/* dst = 2^dstBpc * src / 2^srcBpc = src >>
1047 			 * (srcBpc - dstBpc);
1048 			 */
1049 			dst_color[index] =
1050 				src_color[index] >> (src_bpc - dst_bpc);
1051 		/* CRTC_TEST_PATTERN_DATA has 16 bits,
1052 		 * lowest 6 are hardwired to ZERO
1053 		 * color bits should be left aligned to MSB
1054 		 * XXXXXXXXXX000000 for 10 bit,
1055 		 * XXXXXXXX00000000 for 8 bit and XXXXXX0000000000 for 6
1056 		 */
1057 			dst_color[index] <<= (16 - dst_bpc);
1058 		}
1059 
1060 		REG_WRITE(OTG_TEST_PATTERN_PARAMETERS, 0);
1061 
1062 		/* We have to write the mask before data, similar to pipeline.
1063 		 * For example, for 8 bpc, if we want RGB0 to be magenta,
1064 		 * and RGB1 to be cyan,
1065 		 * we need to make 7 writes:
1066 		 * MASK   DATA
1067 		 * 000001 00000000 00000000                     set mask to R0
1068 		 * 000010 11111111 00000000     R0 255, 0xFF00, set mask to G0
1069 		 * 000100 00000000 00000000     G0 0,   0x0000, set mask to B0
1070 		 * 001000 11111111 00000000     B0 255, 0xFF00, set mask to R1
1071 		 * 010000 00000000 00000000     R1 0,   0x0000, set mask to G1
1072 		 * 100000 11111111 00000000     G1 255, 0xFF00, set mask to B1
1073 		 * 100000 11111111 00000000     B1 255, 0xFF00
1074 		 *
1075 		 * we will make a loop of 6 in which we prepare the mask,
1076 		 * then write, then prepare the color for next write.
1077 		 * first iteration will write mask only,
1078 		 * but each next iteration color prepared in
1079 		 * previous iteration will be written within new mask,
1080 		 * the last component will written separately,
1081 		 * mask is not changing between 6th and 7th write
1082 		 * and color will be prepared by last iteration
1083 		 */
1084 
1085 		/* write color, color values mask in CRTC_TEST_PATTERN_MASK
1086 		 * is B1, G1, R1, B0, G0, R0
1087 		 */
1088 		pattern_data = 0;
1089 		for (index = 0; index < 6; index++) {
1090 			/* prepare color mask, first write PATTERN_DATA
1091 			 * will have all zeros
1092 			 */
1093 			pattern_mask = (1 << index);
1094 
1095 			/* write color component */
1096 			REG_SET_2(OTG_TEST_PATTERN_COLOR, 0,
1097 					OTG_TEST_PATTERN_MASK, pattern_mask,
1098 					OTG_TEST_PATTERN_DATA, pattern_data);
1099 
1100 			/* prepare next color component,
1101 			 * will be written in the next iteration
1102 			 */
1103 			pattern_data = dst_color[index];
1104 		}
1105 		/* write last color component,
1106 		 * it's been already prepared in the loop
1107 		 */
1108 		REG_SET_2(OTG_TEST_PATTERN_COLOR, 0,
1109 				OTG_TEST_PATTERN_MASK, pattern_mask,
1110 				OTG_TEST_PATTERN_DATA, pattern_data);
1111 
1112 		/* enable test pattern */
1113 		REG_UPDATE_4(OTG_TEST_PATTERN_CONTROL,
1114 				OTG_TEST_PATTERN_EN, 1,
1115 				OTG_TEST_PATTERN_MODE, mode,
1116 				OTG_TEST_PATTERN_DYNAMIC_RANGE, 0,
1117 				OTG_TEST_PATTERN_COLOR_FORMAT, bit_depth);
1118 	}
1119 	break;
1120 
1121 	case CONTROLLER_DP_TEST_PATTERN_COLORRAMP:
1122 	{
1123 		mode = (bit_depth ==
1124 			TEST_PATTERN_COLOR_FORMAT_BPC_10 ?
1125 			TEST_PATTERN_MODE_DUALRAMP_RGB :
1126 			TEST_PATTERN_MODE_SINGLERAMP_RGB);
1127 
1128 		switch (bit_depth) {
1129 		case TEST_PATTERN_COLOR_FORMAT_BPC_6:
1130 			dst_bpc = 6;
1131 		break;
1132 		case TEST_PATTERN_COLOR_FORMAT_BPC_8:
1133 			dst_bpc = 8;
1134 		break;
1135 		case TEST_PATTERN_COLOR_FORMAT_BPC_10:
1136 			dst_bpc = 10;
1137 		break;
1138 		default:
1139 			dst_bpc = 8;
1140 		break;
1141 		}
1142 
1143 		/* increment for the first ramp for one color gradation
1144 		 * 1 gradation for 6-bit color is 2^10
1145 		 * gradations in 16-bit color
1146 		 */
1147 		inc_base = (src_bpc - dst_bpc);
1148 
1149 		switch (bit_depth) {
1150 		case TEST_PATTERN_COLOR_FORMAT_BPC_6:
1151 		{
1152 			REG_UPDATE_5(OTG_TEST_PATTERN_PARAMETERS,
1153 					OTG_TEST_PATTERN_INC0, inc_base,
1154 					OTG_TEST_PATTERN_INC1, 0,
1155 					OTG_TEST_PATTERN_HRES, 6,
1156 					OTG_TEST_PATTERN_VRES, 6,
1157 					OTG_TEST_PATTERN_RAMP0_OFFSET, 0);
1158 		}
1159 		break;
1160 		case TEST_PATTERN_COLOR_FORMAT_BPC_8:
1161 		{
1162 			REG_UPDATE_5(OTG_TEST_PATTERN_PARAMETERS,
1163 					OTG_TEST_PATTERN_INC0, inc_base,
1164 					OTG_TEST_PATTERN_INC1, 0,
1165 					OTG_TEST_PATTERN_HRES, 8,
1166 					OTG_TEST_PATTERN_VRES, 6,
1167 					OTG_TEST_PATTERN_RAMP0_OFFSET, 0);
1168 		}
1169 		break;
1170 		case TEST_PATTERN_COLOR_FORMAT_BPC_10:
1171 		{
1172 			REG_UPDATE_5(OTG_TEST_PATTERN_PARAMETERS,
1173 					OTG_TEST_PATTERN_INC0, inc_base,
1174 					OTG_TEST_PATTERN_INC1, inc_base + 2,
1175 					OTG_TEST_PATTERN_HRES, 8,
1176 					OTG_TEST_PATTERN_VRES, 5,
1177 					OTG_TEST_PATTERN_RAMP0_OFFSET, 384 << 6);
1178 		}
1179 		break;
1180 		default:
1181 		break;
1182 		}
1183 
1184 		REG_WRITE(OTG_TEST_PATTERN_COLOR, 0);
1185 
1186 		/* enable test pattern */
1187 		REG_WRITE(OTG_TEST_PATTERN_CONTROL, 0);
1188 
1189 		REG_SET_4(OTG_TEST_PATTERN_CONTROL, 0,
1190 				OTG_TEST_PATTERN_EN, 1,
1191 				OTG_TEST_PATTERN_MODE, mode,
1192 				OTG_TEST_PATTERN_DYNAMIC_RANGE, 0,
1193 				OTG_TEST_PATTERN_COLOR_FORMAT, bit_depth);
1194 	}
1195 	break;
1196 	case CONTROLLER_DP_TEST_PATTERN_VIDEOMODE:
1197 	{
1198 		REG_WRITE(OTG_TEST_PATTERN_CONTROL, 0);
1199 		REG_WRITE(OTG_TEST_PATTERN_COLOR, 0);
1200 		REG_WRITE(OTG_TEST_PATTERN_PARAMETERS, 0);
1201 	}
1202 	break;
1203 	default:
1204 		break;
1205 
1206 	}
1207 }
1208 
1209 void optc1_get_crtc_scanoutpos(
1210 	struct timing_generator *optc,
1211 	uint32_t *v_blank_start,
1212 	uint32_t *v_blank_end,
1213 	uint32_t *h_position,
1214 	uint32_t *v_position)
1215 {
1216 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1217 	struct crtc_position position;
1218 
1219 	REG_GET_2(OTG_V_BLANK_START_END,
1220 			OTG_V_BLANK_START, v_blank_start,
1221 			OTG_V_BLANK_END, v_blank_end);
1222 
1223 	optc1_get_position(optc, &position);
1224 
1225 	*h_position = position.horizontal_count;
1226 	*v_position = position.vertical_count;
1227 }
1228 
1229 static void optc1_enable_stereo(struct timing_generator *optc,
1230 	const struct dc_crtc_timing *timing, struct crtc_stereo_flags *flags)
1231 {
1232 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1233 
1234 	if (flags) {
1235 		uint32_t stereo_en;
1236 		stereo_en = flags->FRAME_PACKED == 0 ? 1 : 0;
1237 
1238 		if (flags->PROGRAM_STEREO)
1239 			REG_UPDATE_3(OTG_STEREO_CONTROL,
1240 				OTG_STEREO_EN, stereo_en,
1241 				OTG_STEREO_SYNC_OUTPUT_LINE_NUM, 0,
1242 				OTG_STEREO_SYNC_OUTPUT_POLARITY, flags->RIGHT_EYE_POLARITY == 0 ? 0 : 1);
1243 
1244 		if (flags->PROGRAM_POLARITY)
1245 			REG_UPDATE(OTG_STEREO_CONTROL,
1246 				OTG_STEREO_EYE_FLAG_POLARITY,
1247 				flags->RIGHT_EYE_POLARITY == 0 ? 0 : 1);
1248 
1249 		if (flags->DISABLE_STEREO_DP_SYNC)
1250 			REG_UPDATE(OTG_STEREO_CONTROL,
1251 				OTG_DISABLE_STEREOSYNC_OUTPUT_FOR_DP, 1);
1252 
1253 		if (flags->PROGRAM_STEREO)
1254 			REG_UPDATE_2(OTG_3D_STRUCTURE_CONTROL,
1255 				OTG_3D_STRUCTURE_EN, flags->FRAME_PACKED,
1256 				OTG_3D_STRUCTURE_STEREO_SEL_OVR, flags->FRAME_PACKED);
1257 
1258 	}
1259 }
1260 
1261 void optc1_program_stereo(struct timing_generator *optc,
1262 	const struct dc_crtc_timing *timing, struct crtc_stereo_flags *flags)
1263 {
1264 	if (flags->PROGRAM_STEREO)
1265 		optc1_enable_stereo(optc, timing, flags);
1266 	else
1267 		optc1_disable_stereo(optc);
1268 }
1269 
1270 
1271 bool optc1_is_stereo_left_eye(struct timing_generator *optc)
1272 {
1273 	bool ret = false;
1274 	uint32_t left_eye = 0;
1275 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1276 
1277 	REG_GET(OTG_STEREO_STATUS,
1278 		OTG_STEREO_CURRENT_EYE, &left_eye);
1279 	if (left_eye == 1)
1280 		ret = true;
1281 	else
1282 		ret = false;
1283 
1284 	return ret;
1285 }
1286 
1287 bool optc1_get_hw_timing(struct timing_generator *tg,
1288 		struct dc_crtc_timing *hw_crtc_timing)
1289 {
1290 	struct dcn_otg_state s = {0};
1291 
1292 	if (tg == NULL || hw_crtc_timing == NULL)
1293 		return false;
1294 
1295 	optc1_read_otg_state(DCN10TG_FROM_TG(tg), &s);
1296 
1297 	hw_crtc_timing->h_total = s.h_total + 1;
1298 	hw_crtc_timing->h_addressable = s.h_total - ((s.h_total - s.h_blank_start) + s.h_blank_end);
1299 	hw_crtc_timing->h_front_porch = s.h_total + 1 - s.h_blank_start;
1300 	hw_crtc_timing->h_sync_width = s.h_sync_a_end - s.h_sync_a_start;
1301 
1302 	hw_crtc_timing->v_total = s.v_total + 1;
1303 	hw_crtc_timing->v_addressable = s.v_total - ((s.v_total - s.v_blank_start) + s.v_blank_end);
1304 	hw_crtc_timing->v_front_porch = s.v_total + 1 - s.v_blank_start;
1305 	hw_crtc_timing->v_sync_width = s.v_sync_a_end - s.v_sync_a_start;
1306 
1307 	return true;
1308 }
1309 
1310 
1311 void optc1_read_otg_state(struct optc *optc1,
1312 		struct dcn_otg_state *s)
1313 {
1314 	REG_GET(OTG_CONTROL,
1315 			OTG_MASTER_EN, &s->otg_enabled);
1316 
1317 	REG_GET_2(OTG_V_BLANK_START_END,
1318 			OTG_V_BLANK_START, &s->v_blank_start,
1319 			OTG_V_BLANK_END, &s->v_blank_end);
1320 
1321 	REG_GET(OTG_V_SYNC_A_CNTL,
1322 			OTG_V_SYNC_A_POL, &s->v_sync_a_pol);
1323 
1324 	REG_GET(OTG_V_TOTAL,
1325 			OTG_V_TOTAL, &s->v_total);
1326 
1327 	REG_GET(OTG_V_TOTAL_MAX,
1328 			OTG_V_TOTAL_MAX, &s->v_total_max);
1329 
1330 	REG_GET(OTG_V_TOTAL_MIN,
1331 			OTG_V_TOTAL_MIN, &s->v_total_min);
1332 
1333 	REG_GET(OTG_V_TOTAL_CONTROL,
1334 			OTG_V_TOTAL_MAX_SEL, &s->v_total_max_sel);
1335 
1336 	REG_GET(OTG_V_TOTAL_CONTROL,
1337 			OTG_V_TOTAL_MIN_SEL, &s->v_total_min_sel);
1338 
1339 	REG_GET_2(OTG_V_SYNC_A,
1340 			OTG_V_SYNC_A_START, &s->v_sync_a_start,
1341 			OTG_V_SYNC_A_END, &s->v_sync_a_end);
1342 
1343 	REG_GET_2(OTG_H_BLANK_START_END,
1344 			OTG_H_BLANK_START, &s->h_blank_start,
1345 			OTG_H_BLANK_END, &s->h_blank_end);
1346 
1347 	REG_GET_2(OTG_H_SYNC_A,
1348 			OTG_H_SYNC_A_START, &s->h_sync_a_start,
1349 			OTG_H_SYNC_A_END, &s->h_sync_a_end);
1350 
1351 	REG_GET(OTG_H_SYNC_A_CNTL,
1352 			OTG_H_SYNC_A_POL, &s->h_sync_a_pol);
1353 
1354 	REG_GET(OTG_H_TOTAL,
1355 			OTG_H_TOTAL, &s->h_total);
1356 
1357 	REG_GET(OPTC_INPUT_GLOBAL_CONTROL,
1358 			OPTC_UNDERFLOW_OCCURRED_STATUS, &s->underflow_occurred_status);
1359 
1360 	REG_GET(OTG_VERTICAL_INTERRUPT1_CONTROL,
1361 			OTG_VERTICAL_INTERRUPT1_INT_ENABLE, &s->vertical_interrupt1_en);
1362 
1363 	REG_GET(OTG_VERTICAL_INTERRUPT1_POSITION,
1364 				OTG_VERTICAL_INTERRUPT1_LINE_START, &s->vertical_interrupt1_line);
1365 
1366 	REG_GET(OTG_VERTICAL_INTERRUPT2_CONTROL,
1367 			OTG_VERTICAL_INTERRUPT2_INT_ENABLE, &s->vertical_interrupt2_en);
1368 
1369 	REG_GET(OTG_VERTICAL_INTERRUPT2_POSITION,
1370 			OTG_VERTICAL_INTERRUPT2_LINE_START, &s->vertical_interrupt2_line);
1371 }
1372 
1373 bool optc1_get_otg_active_size(struct timing_generator *optc,
1374 		uint32_t *otg_active_width,
1375 		uint32_t *otg_active_height)
1376 {
1377 	uint32_t otg_enabled;
1378 	uint32_t v_blank_start;
1379 	uint32_t v_blank_end;
1380 	uint32_t h_blank_start;
1381 	uint32_t h_blank_end;
1382 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1383 
1384 
1385 	REG_GET(OTG_CONTROL,
1386 			OTG_MASTER_EN, &otg_enabled);
1387 
1388 	if (otg_enabled == 0)
1389 		return false;
1390 
1391 	REG_GET_2(OTG_V_BLANK_START_END,
1392 			OTG_V_BLANK_START, &v_blank_start,
1393 			OTG_V_BLANK_END, &v_blank_end);
1394 
1395 	REG_GET_2(OTG_H_BLANK_START_END,
1396 			OTG_H_BLANK_START, &h_blank_start,
1397 			OTG_H_BLANK_END, &h_blank_end);
1398 
1399 	*otg_active_width = v_blank_start - v_blank_end;
1400 	*otg_active_height = h_blank_start - h_blank_end;
1401 	return true;
1402 }
1403 
1404 void optc1_clear_optc_underflow(struct timing_generator *optc)
1405 {
1406 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1407 
1408 	REG_UPDATE(OPTC_INPUT_GLOBAL_CONTROL, OPTC_UNDERFLOW_CLEAR, 1);
1409 }
1410 
1411 void optc1_tg_init(struct timing_generator *optc)
1412 {
1413 	optc1_set_blank_data_double_buffer(optc, true);
1414 	optc1_set_timing_double_buffer(optc, true);
1415 	optc1_clear_optc_underflow(optc);
1416 }
1417 
1418 bool optc1_is_tg_enabled(struct timing_generator *optc)
1419 {
1420 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1421 	uint32_t otg_enabled = 0;
1422 
1423 	REG_GET(OTG_CONTROL, OTG_MASTER_EN, &otg_enabled);
1424 
1425 	return (otg_enabled != 0);
1426 
1427 }
1428 
1429 bool optc1_is_optc_underflow_occurred(struct timing_generator *optc)
1430 {
1431 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1432 	uint32_t underflow_occurred = 0;
1433 
1434 	REG_GET(OPTC_INPUT_GLOBAL_CONTROL,
1435 			OPTC_UNDERFLOW_OCCURRED_STATUS,
1436 			&underflow_occurred);
1437 
1438 	return (underflow_occurred == 1);
1439 }
1440 
1441 bool optc1_configure_crc(struct timing_generator *optc,
1442 			  const struct crc_params *params)
1443 {
1444 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1445 
1446 	/* Cannot configure crc on a CRTC that is disabled */
1447 	if (!optc1_is_tg_enabled(optc))
1448 		return false;
1449 
1450 	REG_WRITE(OTG_CRC_CNTL, 0);
1451 
1452 	if (!params->enable)
1453 		return true;
1454 
1455 	/* Program frame boundaries */
1456 	/* Window A x axis start and end. */
1457 	REG_UPDATE_2(OTG_CRC0_WINDOWA_X_CONTROL,
1458 			OTG_CRC0_WINDOWA_X_START, params->windowa_x_start,
1459 			OTG_CRC0_WINDOWA_X_END, params->windowa_x_end);
1460 
1461 	/* Window A y axis start and end. */
1462 	REG_UPDATE_2(OTG_CRC0_WINDOWA_Y_CONTROL,
1463 			OTG_CRC0_WINDOWA_Y_START, params->windowa_y_start,
1464 			OTG_CRC0_WINDOWA_Y_END, params->windowa_y_end);
1465 
1466 	/* Window B x axis start and end. */
1467 	REG_UPDATE_2(OTG_CRC0_WINDOWB_X_CONTROL,
1468 			OTG_CRC0_WINDOWB_X_START, params->windowb_x_start,
1469 			OTG_CRC0_WINDOWB_X_END, params->windowb_x_end);
1470 
1471 	/* Window B y axis start and end. */
1472 	REG_UPDATE_2(OTG_CRC0_WINDOWB_Y_CONTROL,
1473 			OTG_CRC0_WINDOWB_Y_START, params->windowb_y_start,
1474 			OTG_CRC0_WINDOWB_Y_END, params->windowb_y_end);
1475 
1476 	/* Set crc mode and selection, and enable. Only using CRC0*/
1477 	REG_UPDATE_3(OTG_CRC_CNTL,
1478 			OTG_CRC_CONT_EN, params->continuous_mode ? 1 : 0,
1479 			OTG_CRC0_SELECT, params->selection,
1480 			OTG_CRC_EN, 1);
1481 
1482 	return true;
1483 }
1484 
1485 /**
1486  * optc1_get_crc - Capture CRC result per component
1487  *
1488  * @optc: timing_generator instance.
1489  * @r_cr: 16-bit primary CRC signature for red data.
1490  * @g_y: 16-bit primary CRC signature for green data.
1491  * @b_cb: 16-bit primary CRC signature for blue data.
1492  *
1493  * This function reads the CRC signature from the OPTC registers. Notice that
1494  * we have three registers to keep the CRC result per color component (RGB).
1495  *
1496  * Returns:
1497  * If CRC is disabled, return false; otherwise, return true, and the CRC
1498  * results in the parameters.
1499  */
1500 bool optc1_get_crc(struct timing_generator *optc,
1501 		   uint32_t *r_cr, uint32_t *g_y, uint32_t *b_cb)
1502 {
1503 	uint32_t field = 0;
1504 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
1505 
1506 	REG_GET(OTG_CRC_CNTL, OTG_CRC_EN, &field);
1507 
1508 	/* Early return if CRC is not enabled for this CRTC */
1509 	if (!field)
1510 		return false;
1511 
1512 	/* OTG_CRC0_DATA_RG has the CRC16 results for the red and green component */
1513 	REG_GET_2(OTG_CRC0_DATA_RG,
1514 		  CRC0_R_CR, r_cr,
1515 		  CRC0_G_Y, g_y);
1516 
1517 	/* OTG_CRC0_DATA_B has the CRC16 results for the blue component */
1518 	REG_GET(OTG_CRC0_DATA_B,
1519 		CRC0_B_CB, b_cb);
1520 
1521 	return true;
1522 }
1523 
1524 static const struct timing_generator_funcs dcn10_tg_funcs = {
1525 		.validate_timing = optc1_validate_timing,
1526 		.program_timing = optc1_program_timing,
1527 		.setup_vertical_interrupt0 = optc1_setup_vertical_interrupt0,
1528 		.setup_vertical_interrupt1 = optc1_setup_vertical_interrupt1,
1529 		.setup_vertical_interrupt2 = optc1_setup_vertical_interrupt2,
1530 		.program_global_sync = optc1_program_global_sync,
1531 		.enable_crtc = optc1_enable_crtc,
1532 		.disable_crtc = optc1_disable_crtc,
1533 		/* used by enable_timing_synchronization. Not need for FPGA */
1534 		.is_counter_moving = optc1_is_counter_moving,
1535 		.get_position = optc1_get_position,
1536 		.get_frame_count = optc1_get_vblank_counter,
1537 		.get_scanoutpos = optc1_get_crtc_scanoutpos,
1538 		.get_otg_active_size = optc1_get_otg_active_size,
1539 		.set_early_control = optc1_set_early_control,
1540 		/* used by enable_timing_synchronization. Not need for FPGA */
1541 		.wait_for_state = optc1_wait_for_state,
1542 		.set_blank = optc1_set_blank,
1543 		.is_blanked = optc1_is_blanked,
1544 		.set_blank_color = optc1_program_blank_color,
1545 		.did_triggered_reset_occur = optc1_did_triggered_reset_occur,
1546 		.enable_reset_trigger = optc1_enable_reset_trigger,
1547 		.enable_crtc_reset = optc1_enable_crtc_reset,
1548 		.disable_reset_trigger = optc1_disable_reset_trigger,
1549 		.lock = optc1_lock,
1550 		.unlock = optc1_unlock,
1551 		.enable_optc_clock = optc1_enable_optc_clock,
1552 		.set_drr = optc1_set_drr,
1553 		.get_last_used_drr_vtotal = NULL,
1554 		.set_vtotal_min_max = optc1_set_vtotal_min_max,
1555 		.set_static_screen_control = optc1_set_static_screen_control,
1556 		.set_test_pattern = optc1_set_test_pattern,
1557 		.program_stereo = optc1_program_stereo,
1558 		.is_stereo_left_eye = optc1_is_stereo_left_eye,
1559 		.set_blank_data_double_buffer = optc1_set_blank_data_double_buffer,
1560 		.tg_init = optc1_tg_init,
1561 		.is_tg_enabled = optc1_is_tg_enabled,
1562 		.is_optc_underflow_occurred = optc1_is_optc_underflow_occurred,
1563 		.clear_optc_underflow = optc1_clear_optc_underflow,
1564 		.get_crc = optc1_get_crc,
1565 		.configure_crc = optc1_configure_crc,
1566 		.set_vtg_params = optc1_set_vtg_params,
1567 		.program_manual_trigger = optc1_program_manual_trigger,
1568 		.setup_manual_trigger = optc1_setup_manual_trigger,
1569 		.get_hw_timing = optc1_get_hw_timing,
1570 };
1571 
1572 void dcn10_timing_generator_init(struct optc *optc1)
1573 {
1574 	optc1->base.funcs = &dcn10_tg_funcs;
1575 
1576 	optc1->max_h_total = optc1->tg_mask->OTG_H_TOTAL + 1;
1577 	optc1->max_v_total = optc1->tg_mask->OTG_V_TOTAL + 1;
1578 
1579 	optc1->min_h_blank = 32;
1580 	optc1->min_v_blank = 3;
1581 	optc1->min_v_blank_interlace = 5;
1582 	optc1->min_h_sync_width = 4;
1583 	optc1->min_v_sync_width = 1;
1584 }
1585 
1586 /* "Containter" vs. "pixel" is a concept within HW blocks, mostly those closer to the back-end. It works like this:
1587  *
1588  * - 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
1589  *   containter rate.
1590  *
1591  * - In 4:2:0 (DSC or uncompressed) there are two pixels per container, hence the target container rate has to be
1592  *   halved to maintain the correct pixel rate.
1593  *
1594  * - Unlike 4:2:2 uncompressed, DSC 4:2:2 Native also has two pixels per container (this happens when DSC is applied
1595  *   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.
1596  *
1597  */
1598 bool optc1_is_two_pixels_per_containter(const struct dc_crtc_timing *timing)
1599 {
1600 	bool two_pix = timing->pixel_encoding == PIXEL_ENCODING_YCBCR420;
1601 
1602 	two_pix = two_pix || (timing->flags.DSC && timing->pixel_encoding == PIXEL_ENCODING_YCBCR422
1603 			&& !timing->dsc_cfg.ycbcr422_simple);
1604 	return two_pix;
1605 }
1606 
1607