drm/amd: drop dependencies on drm_os_linux.h
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / display / dc / core / dc_hw_sequencer.c
1 /*
2  * Copyright 2015 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 #include <linux/delay.h>
27
28 #include "dm_services.h"
29 #include "core_types.h"
30 #include "timing_generator.h"
31 #include "hw_sequencer.h"
32
33 #define NUM_ELEMENTS(a) (sizeof(a) / sizeof((a)[0]))
34
35 /* used as index in array of black_color_format */
36 enum black_color_format {
37         BLACK_COLOR_FORMAT_RGB_FULLRANGE = 0,
38         BLACK_COLOR_FORMAT_RGB_LIMITED,
39         BLACK_COLOR_FORMAT_YUV_TV,
40         BLACK_COLOR_FORMAT_YUV_CV,
41         BLACK_COLOR_FORMAT_YUV_SUPER_AA,
42         BLACK_COLOR_FORMAT_DEBUG,
43 };
44
45 enum dc_color_space_type {
46         COLOR_SPACE_RGB_TYPE,
47         COLOR_SPACE_RGB_LIMITED_TYPE,
48         COLOR_SPACE_YCBCR601_TYPE,
49         COLOR_SPACE_YCBCR709_TYPE,
50         COLOR_SPACE_YCBCR601_LIMITED_TYPE,
51         COLOR_SPACE_YCBCR709_LIMITED_TYPE
52 };
53
54 static const struct tg_color black_color_format[] = {
55         /* BlackColorFormat_RGB_FullRange */
56         {0, 0, 0},
57         /* BlackColorFormat_RGB_Limited */
58         {0x40, 0x40, 0x40},
59         /* BlackColorFormat_YUV_TV */
60         {0x200, 0x40, 0x200},
61         /* BlackColorFormat_YUV_CV */
62         {0x1f4, 0x40, 0x1f4},
63         /* BlackColorFormat_YUV_SuperAA */
64         {0x1a2, 0x20, 0x1a2},
65         /* visual confirm debug */
66         {0xff, 0xff, 0},
67 };
68
69 struct out_csc_color_matrix_type {
70         enum dc_color_space_type color_space_type;
71         uint16_t regval[12];
72 };
73
74 static const struct out_csc_color_matrix_type output_csc_matrix[] = {
75         { COLOR_SPACE_RGB_TYPE,
76                 { 0x2000, 0, 0, 0, 0, 0x2000, 0, 0, 0, 0, 0x2000, 0} },
77         { COLOR_SPACE_RGB_LIMITED_TYPE,
78                 { 0x1B67, 0, 0, 0x201, 0, 0x1B67, 0, 0x201, 0, 0, 0x1B67, 0x201} },
79         { COLOR_SPACE_YCBCR601_TYPE,
80                 { 0xE04, 0xF444, 0xFDB9, 0x1004, 0x831, 0x1016, 0x320, 0x201, 0xFB45,
81                                 0xF6B7, 0xE04, 0x1004} },
82         { COLOR_SPACE_YCBCR709_TYPE,
83                 { 0xE04, 0xF345, 0xFEB7, 0x1004, 0x5D3, 0x1399, 0x1FA,
84                                 0x201, 0xFCCA, 0xF533, 0xE04, 0x1004} },
85
86         /* TODO: correct values below */
87         { COLOR_SPACE_YCBCR601_LIMITED_TYPE,
88                 { 0xE00, 0xF447, 0xFDB9, 0x1000, 0x991,
89                                 0x12C9, 0x3A6, 0x200, 0xFB47, 0xF6B9, 0xE00, 0x1000} },
90         { COLOR_SPACE_YCBCR709_LIMITED_TYPE,
91                 { 0xE00, 0xF349, 0xFEB7, 0x1000, 0x6CE, 0x16E3,
92                                 0x24F, 0x200, 0xFCCB, 0xF535, 0xE00, 0x1000} },
93 };
94
95 static bool is_rgb_type(
96                 enum dc_color_space color_space)
97 {
98         bool ret = false;
99
100         if (color_space == COLOR_SPACE_SRGB                     ||
101                 color_space == COLOR_SPACE_XR_RGB               ||
102                 color_space == COLOR_SPACE_MSREF_SCRGB          ||
103                 color_space == COLOR_SPACE_2020_RGB_FULLRANGE   ||
104                 color_space == COLOR_SPACE_ADOBERGB             ||
105                 color_space == COLOR_SPACE_DCIP3        ||
106                 color_space == COLOR_SPACE_DOLBYVISION)
107                 ret = true;
108         return ret;
109 }
110
111 static bool is_rgb_limited_type(
112                 enum dc_color_space color_space)
113 {
114         bool ret = false;
115
116         if (color_space == COLOR_SPACE_SRGB_LIMITED             ||
117                 color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE)
118                 ret = true;
119         return ret;
120 }
121
122 static bool is_ycbcr601_type(
123                 enum dc_color_space color_space)
124 {
125         bool ret = false;
126
127         if (color_space == COLOR_SPACE_YCBCR601 ||
128                 color_space == COLOR_SPACE_XV_YCC_601)
129                 ret = true;
130         return ret;
131 }
132
133 static bool is_ycbcr601_limited_type(
134                 enum dc_color_space color_space)
135 {
136         bool ret = false;
137
138         if (color_space == COLOR_SPACE_YCBCR601_LIMITED)
139                 ret = true;
140         return ret;
141 }
142
143 static bool is_ycbcr709_type(
144                 enum dc_color_space color_space)
145 {
146         bool ret = false;
147
148         if (color_space == COLOR_SPACE_YCBCR709 ||
149                 color_space == COLOR_SPACE_XV_YCC_709)
150                 ret = true;
151         return ret;
152 }
153
154 static bool is_ycbcr709_limited_type(
155                 enum dc_color_space color_space)
156 {
157         bool ret = false;
158
159         if (color_space == COLOR_SPACE_YCBCR709_LIMITED)
160                 ret = true;
161         return ret;
162 }
163 enum dc_color_space_type get_color_space_type(enum dc_color_space color_space)
164 {
165         enum dc_color_space_type type = COLOR_SPACE_RGB_TYPE;
166
167         if (is_rgb_type(color_space))
168                 type = COLOR_SPACE_RGB_TYPE;
169         else if (is_rgb_limited_type(color_space))
170                 type = COLOR_SPACE_RGB_LIMITED_TYPE;
171         else if (is_ycbcr601_type(color_space))
172                 type = COLOR_SPACE_YCBCR601_TYPE;
173         else if (is_ycbcr709_type(color_space))
174                 type = COLOR_SPACE_YCBCR709_TYPE;
175         else if (is_ycbcr601_limited_type(color_space))
176                 type = COLOR_SPACE_YCBCR601_LIMITED_TYPE;
177         else if (is_ycbcr709_limited_type(color_space))
178                 type = COLOR_SPACE_YCBCR709_LIMITED_TYPE;
179
180         return type;
181 }
182
183 const uint16_t *find_color_matrix(enum dc_color_space color_space,
184                                                         uint32_t *array_size)
185 {
186         int i;
187         enum dc_color_space_type type;
188         const uint16_t *val = NULL;
189         int arr_size = NUM_ELEMENTS(output_csc_matrix);
190
191         type = get_color_space_type(color_space);
192         for (i = 0; i < arr_size; i++)
193                 if (output_csc_matrix[i].color_space_type == type) {
194                         val = output_csc_matrix[i].regval;
195                         *array_size = 12;
196                         break;
197                 }
198
199         return val;
200 }
201
202
203 void color_space_to_black_color(
204         const struct dc *dc,
205         enum dc_color_space colorspace,
206         struct tg_color *black_color)
207 {
208         switch (colorspace) {
209         case COLOR_SPACE_YCBCR601:
210         case COLOR_SPACE_YCBCR709:
211         case COLOR_SPACE_YCBCR601_LIMITED:
212         case COLOR_SPACE_YCBCR709_LIMITED:
213         case COLOR_SPACE_2020_YCBCR:
214                 *black_color = black_color_format[BLACK_COLOR_FORMAT_YUV_CV];
215                 break;
216
217         case COLOR_SPACE_SRGB_LIMITED:
218                 *black_color =
219                         black_color_format[BLACK_COLOR_FORMAT_RGB_LIMITED];
220                 break;
221
222         /**
223          * Remove default and add case for all color space
224          * so when we forget to add new color space
225          * compiler will give a warning
226          */
227         case COLOR_SPACE_UNKNOWN:
228         case COLOR_SPACE_SRGB:
229         case COLOR_SPACE_XR_RGB:
230         case COLOR_SPACE_MSREF_SCRGB:
231         case COLOR_SPACE_XV_YCC_709:
232         case COLOR_SPACE_XV_YCC_601:
233         case COLOR_SPACE_2020_RGB_FULLRANGE:
234         case COLOR_SPACE_2020_RGB_LIMITEDRANGE:
235         case COLOR_SPACE_ADOBERGB:
236         case COLOR_SPACE_DCIP3:
237         case COLOR_SPACE_DISPLAYNATIVE:
238         case COLOR_SPACE_DOLBYVISION:
239         case COLOR_SPACE_APPCTRL:
240         case COLOR_SPACE_CUSTOMPOINTS:
241                 /* fefault is sRGB black (full range). */
242                 *black_color =
243                         black_color_format[BLACK_COLOR_FORMAT_RGB_FULLRANGE];
244                 /* default is sRGB black 0. */
245                 break;
246         }
247 }
248
249 bool hwss_wait_for_blank_complete(
250                 struct timing_generator *tg)
251 {
252         int counter;
253
254         /* Not applicable if the pipe is not primary, save 300ms of boot time */
255         if (!tg->funcs->is_blanked)
256                 return true;
257         for (counter = 0; counter < 100; counter++) {
258                 if (tg->funcs->is_blanked(tg))
259                         break;
260
261                 msleep(1);
262         }
263
264         if (counter == 100) {
265                 dm_error("DC: failed to blank crtc!\n");
266                 return false;
267         }
268
269         return true;
270 }