1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2015 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  * Authors: AMD
23b843c749SSergey Zigachev  *
24b843c749SSergey Zigachev  */
25b843c749SSergey Zigachev 
26b843c749SSergey Zigachev #include "dm_services.h"
27b843c749SSergey Zigachev #include "core_types.h"
28b843c749SSergey Zigachev #include "timing_generator.h"
29b843c749SSergey Zigachev #include "hw_sequencer.h"
30b843c749SSergey Zigachev 
31b843c749SSergey Zigachev #define NUM_ELEMENTS(a) (sizeof(a) / sizeof((a)[0]))
32b843c749SSergey Zigachev 
33b843c749SSergey Zigachev /* used as index in array of black_color_format */
34b843c749SSergey Zigachev enum black_color_format {
35b843c749SSergey Zigachev 	BLACK_COLOR_FORMAT_RGB_FULLRANGE = 0,
36b843c749SSergey Zigachev 	BLACK_COLOR_FORMAT_RGB_LIMITED,
37b843c749SSergey Zigachev 	BLACK_COLOR_FORMAT_YUV_TV,
38b843c749SSergey Zigachev 	BLACK_COLOR_FORMAT_YUV_CV,
39b843c749SSergey Zigachev 	BLACK_COLOR_FORMAT_YUV_SUPER_AA,
40b843c749SSergey Zigachev 	BLACK_COLOR_FORMAT_DEBUG,
41b843c749SSergey Zigachev };
42b843c749SSergey Zigachev 
43b843c749SSergey Zigachev enum dc_color_space_type {
44b843c749SSergey Zigachev 	COLOR_SPACE_RGB_TYPE,
45b843c749SSergey Zigachev 	COLOR_SPACE_RGB_LIMITED_TYPE,
46b843c749SSergey Zigachev 	COLOR_SPACE_YCBCR601_TYPE,
47b843c749SSergey Zigachev 	COLOR_SPACE_YCBCR709_TYPE,
48b843c749SSergey Zigachev 	COLOR_SPACE_YCBCR601_LIMITED_TYPE,
49b843c749SSergey Zigachev 	COLOR_SPACE_YCBCR709_LIMITED_TYPE
50b843c749SSergey Zigachev };
51b843c749SSergey Zigachev 
52b843c749SSergey Zigachev static const struct tg_color black_color_format[] = {
53b843c749SSergey Zigachev 	/* BlackColorFormat_RGB_FullRange */
54b843c749SSergey Zigachev 	{0, 0, 0},
55b843c749SSergey Zigachev 	/* BlackColorFormat_RGB_Limited */
56b843c749SSergey Zigachev 	{0x40, 0x40, 0x40},
57b843c749SSergey Zigachev 	/* BlackColorFormat_YUV_TV */
58b843c749SSergey Zigachev 	{0x200, 0x40, 0x200},
59b843c749SSergey Zigachev 	/* BlackColorFormat_YUV_CV */
60b843c749SSergey Zigachev 	{0x1f4, 0x40, 0x1f4},
61b843c749SSergey Zigachev 	/* BlackColorFormat_YUV_SuperAA */
62b843c749SSergey Zigachev 	{0x1a2, 0x20, 0x1a2},
63b843c749SSergey Zigachev 	/* visual confirm debug */
64b843c749SSergey Zigachev 	{0xff, 0xff, 0},
65b843c749SSergey Zigachev };
66b843c749SSergey Zigachev 
67b843c749SSergey Zigachev struct out_csc_color_matrix_type {
68b843c749SSergey Zigachev 	enum dc_color_space_type color_space_type;
69b843c749SSergey Zigachev 	uint16_t regval[12];
70b843c749SSergey Zigachev };
71b843c749SSergey Zigachev 
72b843c749SSergey Zigachev static const struct out_csc_color_matrix_type output_csc_matrix[] = {
73b843c749SSergey Zigachev 	{ COLOR_SPACE_RGB_TYPE,
74b843c749SSergey Zigachev 		{ 0x2000, 0, 0, 0, 0, 0x2000, 0, 0, 0, 0, 0x2000, 0} },
75b843c749SSergey Zigachev 	{ COLOR_SPACE_RGB_LIMITED_TYPE,
76b843c749SSergey Zigachev 		{ 0x1B67, 0, 0, 0x201, 0, 0x1B67, 0, 0x201, 0, 0, 0x1B67, 0x201} },
77b843c749SSergey Zigachev 	{ COLOR_SPACE_YCBCR601_TYPE,
78b843c749SSergey Zigachev 		{ 0xE04, 0xF444, 0xFDB9, 0x1004, 0x831, 0x1016, 0x320, 0x201, 0xFB45,
79b843c749SSergey Zigachev 				0xF6B7, 0xE04, 0x1004} },
80b843c749SSergey Zigachev 	{ COLOR_SPACE_YCBCR709_TYPE,
81b843c749SSergey Zigachev 		{ 0xE04, 0xF345, 0xFEB7, 0x1004, 0x5D3, 0x1399, 0x1FA,
82b843c749SSergey Zigachev 				0x201, 0xFCCA, 0xF533, 0xE04, 0x1004} },
83b843c749SSergey Zigachev 
84b843c749SSergey Zigachev 	/* TODO: correct values below */
85b843c749SSergey Zigachev 	{ COLOR_SPACE_YCBCR601_LIMITED_TYPE,
86b843c749SSergey Zigachev 		{ 0xE00, 0xF447, 0xFDB9, 0x1000, 0x991,
87b843c749SSergey Zigachev 				0x12C9, 0x3A6, 0x200, 0xFB47, 0xF6B9, 0xE00, 0x1000} },
88b843c749SSergey Zigachev 	{ COLOR_SPACE_YCBCR709_LIMITED_TYPE,
89b843c749SSergey Zigachev 		{ 0xE00, 0xF349, 0xFEB7, 0x1000, 0x6CE, 0x16E3,
90b843c749SSergey Zigachev 				0x24F, 0x200, 0xFCCB, 0xF535, 0xE00, 0x1000} },
91b843c749SSergey Zigachev };
92b843c749SSergey Zigachev 
is_rgb_type(enum dc_color_space color_space)93b843c749SSergey Zigachev static bool is_rgb_type(
94b843c749SSergey Zigachev 		enum dc_color_space color_space)
95b843c749SSergey Zigachev {
96b843c749SSergey Zigachev 	bool ret = false;
97b843c749SSergey Zigachev 
98b843c749SSergey Zigachev 	if (color_space == COLOR_SPACE_SRGB			||
99b843c749SSergey Zigachev 		color_space == COLOR_SPACE_XR_RGB		||
100b843c749SSergey Zigachev 		color_space == COLOR_SPACE_MSREF_SCRGB		||
101b843c749SSergey Zigachev 		color_space == COLOR_SPACE_2020_RGB_FULLRANGE	||
102b843c749SSergey Zigachev 		color_space == COLOR_SPACE_ADOBERGB		||
103b843c749SSergey Zigachev 		color_space == COLOR_SPACE_DCIP3	||
104b843c749SSergey Zigachev 		color_space == COLOR_SPACE_DOLBYVISION)
105b843c749SSergey Zigachev 		ret = true;
106b843c749SSergey Zigachev 	return ret;
107b843c749SSergey Zigachev }
108b843c749SSergey Zigachev 
is_rgb_limited_type(enum dc_color_space color_space)109b843c749SSergey Zigachev static bool is_rgb_limited_type(
110b843c749SSergey Zigachev 		enum dc_color_space color_space)
111b843c749SSergey Zigachev {
112b843c749SSergey Zigachev 	bool ret = false;
113b843c749SSergey Zigachev 
114b843c749SSergey Zigachev 	if (color_space == COLOR_SPACE_SRGB_LIMITED		||
115b843c749SSergey Zigachev 		color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE)
116b843c749SSergey Zigachev 		ret = true;
117b843c749SSergey Zigachev 	return ret;
118b843c749SSergey Zigachev }
119b843c749SSergey Zigachev 
is_ycbcr601_type(enum dc_color_space color_space)120b843c749SSergey Zigachev static bool is_ycbcr601_type(
121b843c749SSergey Zigachev 		enum dc_color_space color_space)
122b843c749SSergey Zigachev {
123b843c749SSergey Zigachev 	bool ret = false;
124b843c749SSergey Zigachev 
125b843c749SSergey Zigachev 	if (color_space == COLOR_SPACE_YCBCR601	||
126b843c749SSergey Zigachev 		color_space == COLOR_SPACE_XV_YCC_601)
127b843c749SSergey Zigachev 		ret = true;
128b843c749SSergey Zigachev 	return ret;
129b843c749SSergey Zigachev }
130b843c749SSergey Zigachev 
is_ycbcr601_limited_type(enum dc_color_space color_space)131b843c749SSergey Zigachev static bool is_ycbcr601_limited_type(
132b843c749SSergey Zigachev 		enum dc_color_space color_space)
133b843c749SSergey Zigachev {
134b843c749SSergey Zigachev 	bool ret = false;
135b843c749SSergey Zigachev 
136b843c749SSergey Zigachev 	if (color_space == COLOR_SPACE_YCBCR601_LIMITED)
137b843c749SSergey Zigachev 		ret = true;
138b843c749SSergey Zigachev 	return ret;
139b843c749SSergey Zigachev }
140b843c749SSergey Zigachev 
is_ycbcr709_type(enum dc_color_space color_space)141b843c749SSergey Zigachev static bool is_ycbcr709_type(
142b843c749SSergey Zigachev 		enum dc_color_space color_space)
143b843c749SSergey Zigachev {
144b843c749SSergey Zigachev 	bool ret = false;
145b843c749SSergey Zigachev 
146b843c749SSergey Zigachev 	if (color_space == COLOR_SPACE_YCBCR709	||
147b843c749SSergey Zigachev 		color_space == COLOR_SPACE_XV_YCC_709)
148b843c749SSergey Zigachev 		ret = true;
149b843c749SSergey Zigachev 	return ret;
150b843c749SSergey Zigachev }
151b843c749SSergey Zigachev 
is_ycbcr709_limited_type(enum dc_color_space color_space)152b843c749SSergey Zigachev static bool is_ycbcr709_limited_type(
153b843c749SSergey Zigachev 		enum dc_color_space color_space)
154b843c749SSergey Zigachev {
155b843c749SSergey Zigachev 	bool ret = false;
156b843c749SSergey Zigachev 
157b843c749SSergey Zigachev 	if (color_space == COLOR_SPACE_YCBCR709_LIMITED)
158b843c749SSergey Zigachev 		ret = true;
159b843c749SSergey Zigachev 	return ret;
160b843c749SSergey Zigachev }
161*78973132SSergey Zigachev 
162*78973132SSergey Zigachev static
get_color_space_type(enum dc_color_space color_space)163b843c749SSergey Zigachev enum dc_color_space_type get_color_space_type(enum dc_color_space color_space)
164b843c749SSergey Zigachev {
165b843c749SSergey Zigachev 	enum dc_color_space_type type = COLOR_SPACE_RGB_TYPE;
166b843c749SSergey Zigachev 
167b843c749SSergey Zigachev 	if (is_rgb_type(color_space))
168b843c749SSergey Zigachev 		type = COLOR_SPACE_RGB_TYPE;
169b843c749SSergey Zigachev 	else if (is_rgb_limited_type(color_space))
170b843c749SSergey Zigachev 		type = COLOR_SPACE_RGB_LIMITED_TYPE;
171b843c749SSergey Zigachev 	else if (is_ycbcr601_type(color_space))
172b843c749SSergey Zigachev 		type = COLOR_SPACE_YCBCR601_TYPE;
173b843c749SSergey Zigachev 	else if (is_ycbcr709_type(color_space))
174b843c749SSergey Zigachev 		type = COLOR_SPACE_YCBCR709_TYPE;
175b843c749SSergey Zigachev 	else if (is_ycbcr601_limited_type(color_space))
176b843c749SSergey Zigachev 		type = COLOR_SPACE_YCBCR601_LIMITED_TYPE;
177b843c749SSergey Zigachev 	else if (is_ycbcr709_limited_type(color_space))
178b843c749SSergey Zigachev 		type = COLOR_SPACE_YCBCR709_LIMITED_TYPE;
179b843c749SSergey Zigachev 
180b843c749SSergey Zigachev 	return type;
181b843c749SSergey Zigachev }
182b843c749SSergey Zigachev 
find_color_matrix(enum dc_color_space color_space,uint32_t * array_size)183b843c749SSergey Zigachev const uint16_t *find_color_matrix(enum dc_color_space color_space,
184b843c749SSergey Zigachev 							uint32_t *array_size)
185b843c749SSergey Zigachev {
186b843c749SSergey Zigachev 	int i;
187b843c749SSergey Zigachev 	enum dc_color_space_type type;
188b843c749SSergey Zigachev 	const uint16_t *val = NULL;
189b843c749SSergey Zigachev 	int arr_size = NUM_ELEMENTS(output_csc_matrix);
190b843c749SSergey Zigachev 
191b843c749SSergey Zigachev 	type = get_color_space_type(color_space);
192b843c749SSergey Zigachev 	for (i = 0; i < arr_size; i++)
193b843c749SSergey Zigachev 		if (output_csc_matrix[i].color_space_type == type) {
194b843c749SSergey Zigachev 			val = output_csc_matrix[i].regval;
195b843c749SSergey Zigachev 			*array_size = 12;
196b843c749SSergey Zigachev 			break;
197b843c749SSergey Zigachev 		}
198b843c749SSergey Zigachev 
199b843c749SSergey Zigachev 	return val;
200b843c749SSergey Zigachev }
201b843c749SSergey Zigachev 
202b843c749SSergey Zigachev 
color_space_to_black_color(const struct dc * dc,enum dc_color_space colorspace,struct tg_color * black_color)203b843c749SSergey Zigachev void color_space_to_black_color(
204b843c749SSergey Zigachev 	const struct dc *dc,
205b843c749SSergey Zigachev 	enum dc_color_space colorspace,
206b843c749SSergey Zigachev 	struct tg_color *black_color)
207b843c749SSergey Zigachev {
208b843c749SSergey Zigachev 	switch (colorspace) {
209b843c749SSergey Zigachev 	case COLOR_SPACE_YCBCR601:
210b843c749SSergey Zigachev 	case COLOR_SPACE_YCBCR709:
211b843c749SSergey Zigachev 	case COLOR_SPACE_YCBCR601_LIMITED:
212b843c749SSergey Zigachev 	case COLOR_SPACE_YCBCR709_LIMITED:
213b843c749SSergey Zigachev 	case COLOR_SPACE_2020_YCBCR:
214b843c749SSergey Zigachev 		*black_color = black_color_format[BLACK_COLOR_FORMAT_YUV_CV];
215b843c749SSergey Zigachev 		break;
216b843c749SSergey Zigachev 
217b843c749SSergey Zigachev 	case COLOR_SPACE_SRGB_LIMITED:
218b843c749SSergey Zigachev 		*black_color =
219b843c749SSergey Zigachev 			black_color_format[BLACK_COLOR_FORMAT_RGB_LIMITED];
220b843c749SSergey Zigachev 		break;
221b843c749SSergey Zigachev 
222b843c749SSergey Zigachev 	/**
223b843c749SSergey Zigachev 	 * Remove default and add case for all color space
224b843c749SSergey Zigachev 	 * so when we forget to add new color space
225b843c749SSergey Zigachev 	 * compiler will give a warning
226b843c749SSergey Zigachev 	 */
227b843c749SSergey Zigachev 	case COLOR_SPACE_UNKNOWN:
228b843c749SSergey Zigachev 	case COLOR_SPACE_SRGB:
229b843c749SSergey Zigachev 	case COLOR_SPACE_XR_RGB:
230b843c749SSergey Zigachev 	case COLOR_SPACE_MSREF_SCRGB:
231b843c749SSergey Zigachev 	case COLOR_SPACE_XV_YCC_709:
232b843c749SSergey Zigachev 	case COLOR_SPACE_XV_YCC_601:
233b843c749SSergey Zigachev 	case COLOR_SPACE_2020_RGB_FULLRANGE:
234b843c749SSergey Zigachev 	case COLOR_SPACE_2020_RGB_LIMITEDRANGE:
235b843c749SSergey Zigachev 	case COLOR_SPACE_ADOBERGB:
236b843c749SSergey Zigachev 	case COLOR_SPACE_DCIP3:
237b843c749SSergey Zigachev 	case COLOR_SPACE_DISPLAYNATIVE:
238b843c749SSergey Zigachev 	case COLOR_SPACE_DOLBYVISION:
239b843c749SSergey Zigachev 	case COLOR_SPACE_APPCTRL:
240b843c749SSergey Zigachev 	case COLOR_SPACE_CUSTOMPOINTS:
241b843c749SSergey Zigachev 		/* fefault is sRGB black (full range). */
242b843c749SSergey Zigachev 		*black_color =
243b843c749SSergey Zigachev 			black_color_format[BLACK_COLOR_FORMAT_RGB_FULLRANGE];
244b843c749SSergey Zigachev 		/* default is sRGB black 0. */
245b843c749SSergey Zigachev 		break;
246b843c749SSergey Zigachev 	}
247b843c749SSergey Zigachev }
248b843c749SSergey Zigachev 
hwss_wait_for_blank_complete(struct timing_generator * tg)249b843c749SSergey Zigachev bool hwss_wait_for_blank_complete(
250b843c749SSergey Zigachev 		struct timing_generator *tg)
251b843c749SSergey Zigachev {
252b843c749SSergey Zigachev 	int counter;
253b843c749SSergey Zigachev 
254b843c749SSergey Zigachev 	/* Not applicable if the pipe is not primary, save 300ms of boot time */
255b843c749SSergey Zigachev 	if (!tg->funcs->is_blanked)
256b843c749SSergey Zigachev 		return true;
257b843c749SSergey Zigachev 	for (counter = 0; counter < 100; counter++) {
258b843c749SSergey Zigachev 		if (tg->funcs->is_blanked(tg))
259b843c749SSergey Zigachev 			break;
260b843c749SSergey Zigachev 
261b843c749SSergey Zigachev 		msleep(1);
262b843c749SSergey Zigachev 	}
263b843c749SSergey Zigachev 
264b843c749SSergey Zigachev 	if (counter == 100) {
265b843c749SSergey Zigachev 		dm_error("DC: failed to blank crtc!\n");
266b843c749SSergey Zigachev 		return false;
267b843c749SSergey Zigachev 	}
268b843c749SSergey Zigachev 
269b843c749SSergey Zigachev 	return true;
270b843c749SSergey Zigachev }
271