1*3f68c01bSZhan Liu /*
2*3f68c01bSZhan Liu  * Copyright 2012-15 Advanced Micro Devices, Inc.
3*3f68c01bSZhan Liu  *
4*3f68c01bSZhan Liu  * Permission is hereby granted, free of charge, to any person obtaining a
5*3f68c01bSZhan Liu  * copy of this software and associated documentation files (the "Software"),
6*3f68c01bSZhan Liu  * to deal in the Software without restriction, including without limitation
7*3f68c01bSZhan Liu  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*3f68c01bSZhan Liu  * and/or sell copies of the Software, and to permit persons to whom the
9*3f68c01bSZhan Liu  * Software is furnished to do so, subject to the following conditions:
10*3f68c01bSZhan Liu  *
11*3f68c01bSZhan Liu  * The above copyright notice and this permission notice shall be included in
12*3f68c01bSZhan Liu  * all copies or substantial portions of the Software.
13*3f68c01bSZhan Liu  *
14*3f68c01bSZhan Liu  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*3f68c01bSZhan Liu  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*3f68c01bSZhan Liu  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*3f68c01bSZhan Liu  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*3f68c01bSZhan Liu  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*3f68c01bSZhan Liu  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*3f68c01bSZhan Liu  * OTHER DEALINGS IN THE SOFTWARE.
21*3f68c01bSZhan Liu  *
22*3f68c01bSZhan Liu  * Authors: AMD
23*3f68c01bSZhan Liu  *
24*3f68c01bSZhan Liu  */
25*3f68c01bSZhan Liu 
26*3f68c01bSZhan Liu #include "reg_helper.h"
27*3f68c01bSZhan Liu #include "dcn201_optc.h"
28*3f68c01bSZhan Liu #include "dcn10/dcn10_optc.h"
29*3f68c01bSZhan Liu #include "dc.h"
30*3f68c01bSZhan Liu 
31*3f68c01bSZhan Liu #define REG(reg)\
32*3f68c01bSZhan Liu 	optc1->tg_regs->reg
33*3f68c01bSZhan Liu 
34*3f68c01bSZhan Liu #define CTX \
35*3f68c01bSZhan Liu 	optc1->base.ctx
36*3f68c01bSZhan Liu 
37*3f68c01bSZhan Liu #undef FN
38*3f68c01bSZhan Liu #define FN(reg_name, field_name) \
39*3f68c01bSZhan Liu 	optc1->tg_shift->field_name, optc1->tg_mask->field_name
40*3f68c01bSZhan Liu 
41*3f68c01bSZhan Liu /*TEMP: Need to figure out inheritance model here.*/
optc201_is_two_pixels_per_containter(const struct dc_crtc_timing * timing)42*3f68c01bSZhan Liu bool optc201_is_two_pixels_per_containter(const struct dc_crtc_timing *timing)
43*3f68c01bSZhan Liu {
44*3f68c01bSZhan Liu 	return optc1_is_two_pixels_per_containter(timing);
45*3f68c01bSZhan Liu }
46*3f68c01bSZhan Liu 
optc201_triplebuffer_lock(struct timing_generator * optc)47*3f68c01bSZhan Liu static void optc201_triplebuffer_lock(struct timing_generator *optc)
48*3f68c01bSZhan Liu {
49*3f68c01bSZhan Liu 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
50*3f68c01bSZhan Liu 
51*3f68c01bSZhan Liu 	REG_SET(OTG_GLOBAL_CONTROL0, 0,
52*3f68c01bSZhan Liu 		OTG_MASTER_UPDATE_LOCK_SEL, optc->inst);
53*3f68c01bSZhan Liu 	REG_SET(OTG_VUPDATE_KEEPOUT, 0,
54*3f68c01bSZhan Liu 		OTG_MASTER_UPDATE_LOCK_VUPDATE_KEEPOUT_EN, 1);
55*3f68c01bSZhan Liu 	REG_SET(OTG_MASTER_UPDATE_LOCK, 0,
56*3f68c01bSZhan Liu 		OTG_MASTER_UPDATE_LOCK, 1);
57*3f68c01bSZhan Liu 
58*3f68c01bSZhan Liu 	REG_WAIT(OTG_MASTER_UPDATE_LOCK,
59*3f68c01bSZhan Liu 			UPDATE_LOCK_STATUS, 1,
60*3f68c01bSZhan Liu 			1, 10);
61*3f68c01bSZhan Liu }
62*3f68c01bSZhan Liu 
optc201_triplebuffer_unlock(struct timing_generator * optc)63*3f68c01bSZhan Liu static void optc201_triplebuffer_unlock(struct timing_generator *optc)
64*3f68c01bSZhan Liu {
65*3f68c01bSZhan Liu 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
66*3f68c01bSZhan Liu 
67*3f68c01bSZhan Liu 	REG_SET(OTG_MASTER_UPDATE_LOCK, 0,
68*3f68c01bSZhan Liu 		OTG_MASTER_UPDATE_LOCK, 0);
69*3f68c01bSZhan Liu 	REG_SET(OTG_VUPDATE_KEEPOUT, 0,
70*3f68c01bSZhan Liu 		OTG_MASTER_UPDATE_LOCK_VUPDATE_KEEPOUT_EN, 0);
71*3f68c01bSZhan Liu 
72*3f68c01bSZhan Liu }
73*3f68c01bSZhan Liu 
optc201_validate_timing(struct timing_generator * optc,const struct dc_crtc_timing * timing)74*3f68c01bSZhan Liu static bool optc201_validate_timing(
75*3f68c01bSZhan Liu 	struct timing_generator *optc,
76*3f68c01bSZhan Liu 	const struct dc_crtc_timing *timing)
77*3f68c01bSZhan Liu {
78*3f68c01bSZhan Liu 	uint32_t v_blank;
79*3f68c01bSZhan Liu 	uint32_t h_blank;
80*3f68c01bSZhan Liu 	uint32_t min_v_blank;
81*3f68c01bSZhan Liu 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
82*3f68c01bSZhan Liu 
83*3f68c01bSZhan Liu 	ASSERT(timing != NULL);
84*3f68c01bSZhan Liu 
85*3f68c01bSZhan Liu 	v_blank = (timing->v_total - timing->v_addressable -
86*3f68c01bSZhan Liu 					timing->v_border_top - timing->v_border_bottom);
87*3f68c01bSZhan Liu 
88*3f68c01bSZhan Liu 	h_blank = (timing->h_total - timing->h_addressable -
89*3f68c01bSZhan Liu 		timing->h_border_right -
90*3f68c01bSZhan Liu 		timing->h_border_left);
91*3f68c01bSZhan Liu 
92*3f68c01bSZhan Liu 	if (timing->timing_3d_format != TIMING_3D_FORMAT_NONE &&
93*3f68c01bSZhan Liu 		timing->timing_3d_format != TIMING_3D_FORMAT_HW_FRAME_PACKING &&
94*3f68c01bSZhan Liu 		timing->timing_3d_format != TIMING_3D_FORMAT_TOP_AND_BOTTOM &&
95*3f68c01bSZhan Liu 		timing->timing_3d_format != TIMING_3D_FORMAT_SIDE_BY_SIDE &&
96*3f68c01bSZhan Liu 		timing->timing_3d_format != TIMING_3D_FORMAT_FRAME_ALTERNATE &&
97*3f68c01bSZhan Liu 		timing->timing_3d_format != TIMING_3D_FORMAT_INBAND_FA)
98*3f68c01bSZhan Liu 		return false;
99*3f68c01bSZhan Liu 
100*3f68c01bSZhan Liu 	/* Check maximum number of pixels supported by Timing Generator
101*3f68c01bSZhan Liu 	 * (Currently will never fail, in order to fail needs display which
102*3f68c01bSZhan Liu 	 * needs more than 8192 horizontal and
103*3f68c01bSZhan Liu 	 * more than 8192 vertical total pixels)
104*3f68c01bSZhan Liu 	 */
105*3f68c01bSZhan Liu 	if (timing->h_total > optc1->max_h_total ||
106*3f68c01bSZhan Liu 		timing->v_total > optc1->max_v_total)
107*3f68c01bSZhan Liu 		return false;
108*3f68c01bSZhan Liu 
109*3f68c01bSZhan Liu 	if (h_blank < optc1->min_h_blank)
110*3f68c01bSZhan Liu 		return false;
111*3f68c01bSZhan Liu 
112*3f68c01bSZhan Liu 	if (timing->h_sync_width  < optc1->min_h_sync_width ||
113*3f68c01bSZhan Liu 		 timing->v_sync_width  < optc1->min_v_sync_width)
114*3f68c01bSZhan Liu 		return false;
115*3f68c01bSZhan Liu 
116*3f68c01bSZhan Liu 	min_v_blank = timing->flags.INTERLACE?optc1->min_v_blank_interlace:optc1->min_v_blank;
117*3f68c01bSZhan Liu 
118*3f68c01bSZhan Liu 	if (v_blank < min_v_blank)
119*3f68c01bSZhan Liu 		return false;
120*3f68c01bSZhan Liu 
121*3f68c01bSZhan Liu 	return true;
122*3f68c01bSZhan Liu 
123*3f68c01bSZhan Liu }
124*3f68c01bSZhan Liu 
optc201_get_optc_source(struct timing_generator * optc,uint32_t * num_of_src_opp,uint32_t * src_opp_id_0,uint32_t * src_opp_id_1)125*3f68c01bSZhan Liu static void optc201_get_optc_source(struct timing_generator *optc,
126*3f68c01bSZhan Liu 		uint32_t *num_of_src_opp,
127*3f68c01bSZhan Liu 		uint32_t *src_opp_id_0,
128*3f68c01bSZhan Liu 		uint32_t *src_opp_id_1)
129*3f68c01bSZhan Liu {
130*3f68c01bSZhan Liu 	struct optc *optc1 = DCN10TG_FROM_TG(optc);
131*3f68c01bSZhan Liu 
132*3f68c01bSZhan Liu 	REG_GET(OPTC_DATA_SOURCE_SELECT,
133*3f68c01bSZhan Liu 			OPTC_SEG0_SRC_SEL, src_opp_id_0);
134*3f68c01bSZhan Liu 
135*3f68c01bSZhan Liu 	*num_of_src_opp = 1;
136*3f68c01bSZhan Liu }
137*3f68c01bSZhan Liu 
138*3f68c01bSZhan Liu static struct timing_generator_funcs dcn201_tg_funcs = {
139*3f68c01bSZhan Liu 		.validate_timing = optc201_validate_timing,
140*3f68c01bSZhan Liu 		.program_timing = optc1_program_timing,
141*3f68c01bSZhan Liu 		.setup_vertical_interrupt0 = optc1_setup_vertical_interrupt0,
142*3f68c01bSZhan Liu 		.setup_vertical_interrupt1 = optc1_setup_vertical_interrupt1,
143*3f68c01bSZhan Liu 		.setup_vertical_interrupt2 = optc1_setup_vertical_interrupt2,
144*3f68c01bSZhan Liu 		.program_global_sync = optc1_program_global_sync,
145*3f68c01bSZhan Liu 		.enable_crtc = optc2_enable_crtc,
146*3f68c01bSZhan Liu 		.disable_crtc = optc1_disable_crtc,
147*3f68c01bSZhan Liu 		/* used by enable_timing_synchronization. Not need for FPGA */
148*3f68c01bSZhan Liu 		.is_counter_moving = optc1_is_counter_moving,
149*3f68c01bSZhan Liu 		.get_position = optc1_get_position,
150*3f68c01bSZhan Liu 		.get_frame_count = optc1_get_vblank_counter,
151*3f68c01bSZhan Liu 		.get_scanoutpos = optc1_get_crtc_scanoutpos,
152*3f68c01bSZhan Liu 		.get_otg_active_size = optc1_get_otg_active_size,
153*3f68c01bSZhan Liu 		.set_early_control = optc1_set_early_control,
154*3f68c01bSZhan Liu 		/* used by enable_timing_synchronization. Not need for FPGA */
155*3f68c01bSZhan Liu 		.wait_for_state = optc1_wait_for_state,
156*3f68c01bSZhan Liu 		.set_blank = optc1_set_blank,
157*3f68c01bSZhan Liu 		.is_blanked = optc1_is_blanked,
158*3f68c01bSZhan Liu 		.set_blank_color = optc1_program_blank_color,
159*3f68c01bSZhan Liu 		.did_triggered_reset_occur = optc1_did_triggered_reset_occur,
160*3f68c01bSZhan Liu 		.enable_reset_trigger = optc1_enable_reset_trigger,
161*3f68c01bSZhan Liu 		.enable_crtc_reset = optc1_enable_crtc_reset,
162*3f68c01bSZhan Liu 		.disable_reset_trigger = optc1_disable_reset_trigger,
163*3f68c01bSZhan Liu 		.triplebuffer_lock = optc201_triplebuffer_lock,
164*3f68c01bSZhan Liu 		.triplebuffer_unlock = optc201_triplebuffer_unlock,
165*3f68c01bSZhan Liu 		.lock = optc1_lock,
166*3f68c01bSZhan Liu 		.unlock = optc1_unlock,
167*3f68c01bSZhan Liu 		.enable_optc_clock = optc1_enable_optc_clock,
168*3f68c01bSZhan Liu 		.set_drr = optc1_set_drr,
169*3f68c01bSZhan Liu 		.get_last_used_drr_vtotal = NULL,
170*3f68c01bSZhan Liu 		.set_vtotal_min_max = optc1_set_vtotal_min_max,
171*3f68c01bSZhan Liu 		.set_static_screen_control = optc1_set_static_screen_control,
172*3f68c01bSZhan Liu 		.program_stereo = optc1_program_stereo,
173*3f68c01bSZhan Liu 		.is_stereo_left_eye = optc1_is_stereo_left_eye,
174*3f68c01bSZhan Liu 		.set_blank_data_double_buffer = optc1_set_blank_data_double_buffer,
175*3f68c01bSZhan Liu 		.tg_init = optc1_tg_init,
176*3f68c01bSZhan Liu 		.is_tg_enabled = optc1_is_tg_enabled,
177*3f68c01bSZhan Liu 		.is_optc_underflow_occurred = optc1_is_optc_underflow_occurred,
178*3f68c01bSZhan Liu 		.clear_optc_underflow = optc1_clear_optc_underflow,
179*3f68c01bSZhan Liu 		.get_crc = optc1_get_crc,
180*3f68c01bSZhan Liu 		.configure_crc = optc2_configure_crc,
181*3f68c01bSZhan Liu 		.set_dsc_config = optc2_set_dsc_config,
182*3f68c01bSZhan Liu 		.set_dwb_source = NULL,
183*3f68c01bSZhan Liu 		.get_optc_source = optc201_get_optc_source,
184*3f68c01bSZhan Liu 		.set_vtg_params = optc1_set_vtg_params,
185*3f68c01bSZhan Liu 		.program_manual_trigger = optc2_program_manual_trigger,
186*3f68c01bSZhan Liu 		.setup_manual_trigger = optc2_setup_manual_trigger,
187*3f68c01bSZhan Liu 		.get_hw_timing = optc1_get_hw_timing,
188*3f68c01bSZhan Liu };
189*3f68c01bSZhan Liu 
dcn201_timing_generator_init(struct optc * optc1)190*3f68c01bSZhan Liu void dcn201_timing_generator_init(struct optc *optc1)
191*3f68c01bSZhan Liu {
192*3f68c01bSZhan Liu 	optc1->base.funcs = &dcn201_tg_funcs;
193*3f68c01bSZhan Liu 
194*3f68c01bSZhan Liu 	optc1->max_h_total = optc1->tg_mask->OTG_H_TOTAL + 1;
195*3f68c01bSZhan Liu 	optc1->max_v_total = optc1->tg_mask->OTG_V_TOTAL + 1;
196*3f68c01bSZhan Liu 
197*3f68c01bSZhan Liu 	optc1->min_h_blank = 32;
198*3f68c01bSZhan Liu 	optc1->min_v_blank = 3;
199*3f68c01bSZhan Liu 	optc1->min_v_blank_interlace = 5;
200*3f68c01bSZhan Liu 	optc1->min_h_sync_width = 8;
201*3f68c01bSZhan Liu 	optc1->min_v_sync_width = 1;
202*3f68c01bSZhan Liu }
203