xref: /linux/drivers/gpu/drm/ast/ast_vbios.c (revision 1260ed77798502de9c98020040d2995008de10cc)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright (c) 2005 ASPEED Technology Inc.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the authors not be used in
10  * advertising or publicity pertaining to distribution of the software without
11  * specific, written prior permission.  The authors makes no representations
12  * about the suitability of this software for any purpose.  It is provided
13  * "as is" without express or implied warranty.
14  *
15  * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17  * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21  * PERFORMANCE OF THIS SOFTWARE.
22  */
23 
24 #include "ast_drv.h"
25 #include "ast_vbios.h"
26 
27 /* 4:3 */
28 
29 static const struct ast_vbios_enhtable res_640x480[] = {
30 	{ 800, 640, 8, 96, 525, 480, 2, 2, VCLK25_175,		/* 60 Hz */
31 	  (SyncNN | HBorder | VBorder | Charx8Dot), 60, 1, 0x2e },
32 	{ 832, 640, 16, 40, 520, 480, 1, 3, VCLK31_5,		/* 72 Hz */
33 	  (SyncNN | HBorder | VBorder | Charx8Dot), 72, 2, 0x2e  },
34 	{ 840, 640, 16, 64, 500, 480, 1, 3, VCLK31_5,		/* 75 Hz */
35 	  (SyncNN | Charx8Dot), 75, 3, 0x2e },
36 	{ 832, 640, 56, 56, 509, 480, 1, 3, VCLK36,		/* 85 Hz */
37 	  (SyncNN | Charx8Dot), 85, 4, 0x2e },
38 	AST_VBIOS_INVALID_MODE,					/* end */
39 };
40 
41 static const struct ast_vbios_enhtable res_800x600[] = {
42 	{ 1024, 800, 24, 72, 625, 600, 1, 2, VCLK36,		/* 56 Hz */
43 	  (SyncPP | Charx8Dot), 56, 1, 0x30 },
44 	{ 1056, 800, 40, 128, 628, 600, 1, 4, VCLK40,		/* 60 Hz */
45 	  (SyncPP | Charx8Dot), 60, 2, 0x30 },
46 	{ 1040, 800, 56, 120, 666, 600, 37, 6, VCLK50,		/* 72 Hz */
47 	  (SyncPP | Charx8Dot), 72, 3, 0x30 },
48 	{ 1056, 800, 16, 80, 625, 600, 1, 3, VCLK49_5,		/* 75 Hz */
49 	  (SyncPP | Charx8Dot), 75, 4, 0x30 },
50 	{ 1048, 800, 32, 64, 631, 600, 1, 3, VCLK56_25,		/* 85 Hz */
51 	  (SyncPP | Charx8Dot), 84, 5, 0x30 },
52 	AST_VBIOS_INVALID_MODE,					/* end */
53 };
54 
55 static const struct ast_vbios_enhtable res_1024x768[] = {
56 	{ 1344, 1024, 24, 136, 806, 768, 3, 6, VCLK65,		/* 60 Hz */
57 	  (SyncNN | Charx8Dot), 60, 1, 0x31 },
58 	{ 1328, 1024, 24, 136, 806, 768, 3, 6, VCLK75,		/* 70 Hz */
59 	  (SyncNN | Charx8Dot), 70, 2, 0x31 },
60 	{ 1312, 1024, 16, 96, 800, 768, 1, 3, VCLK78_75,	/* 75 Hz */
61 	  (SyncPP | Charx8Dot), 75, 3, 0x31 },
62 	{ 1376, 1024, 48, 96, 808, 768, 1, 3, VCLK94_5,		/* 85 Hz */
63 	  (SyncPP | Charx8Dot), 84, 4, 0x31 },
64 	AST_VBIOS_INVALID_MODE,					/* end */
65 };
66 
67 static const struct ast_vbios_enhtable res_1152x864[] = {
68 	{ 1600, 1152, 64, 128,  900,  864, 1, 3, VCLK108,	/* 75 Hz */
69 	  (SyncPP | Charx8Dot | NewModeInfo), 75, 1, 0x3b },
70 	AST_VBIOS_INVALID_MODE,					/* end */
71 };
72 
73 static const struct ast_vbios_enhtable res_1280x1024[] = {
74 	{ 1688, 1280, 48, 112, 1066, 1024, 1, 3, VCLK108,	/* 60 Hz */
75 	  (SyncPP | Charx8Dot), 60, 1, 0x32 },
76 	{ 1688, 1280, 16, 144, 1066, 1024, 1, 3, VCLK135,	/* 75 Hz */
77 	  (SyncPP | Charx8Dot), 75, 2, 0x32 },
78 	{ 1728, 1280, 64, 160, 1072, 1024, 1, 3, VCLK157_5,	/* 85 Hz */
79 	  (SyncPP | Charx8Dot), 85, 3, 0x32 },
80 	AST_VBIOS_INVALID_MODE,					/* end */
81 };
82 
83 static const struct ast_vbios_enhtable res_1600x1200[] = {
84 	{ 2160, 1600, 64, 192, 1250, 1200, 1, 3, VCLK162,	/* 60 Hz */
85 	  (SyncPP | Charx8Dot), 60, 1, 0x33 },
86 	AST_VBIOS_INVALID_MODE,					/* end */
87 };
88 
89 /* 16:9 */
90 
91 static const struct ast_vbios_enhtable res_1360x768[] = {
92 	{ 1792, 1360, 64, 112, 795, 768, 3, 6, VCLK85_5,	/* 60 Hz */
93 	  (SyncPP | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo), 60, 1, 0x39 },
94 	AST_VBIOS_INVALID_MODE,					/* end */
95 };
96 
97 static const struct ast_vbios_enhtable res_1600x900[] = {
98 	{ 1760, 1600, 48, 32, 926, 900, 3, 5, VCLK97_75,	/* 60 Hz CVT RB */
99 	  (SyncNP | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo |
100 	  AST2500PreCatchCRT), 60, 1, 0x3a },
101 	{ 2112, 1600, 88, 168, 934, 900, 3, 5, VCLK118_25,	/* 60 Hz CVT */
102 	  (SyncPN | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo), 60, 2, 0x3a },
103 	AST_VBIOS_INVALID_MODE,					/* end */
104 };
105 
106 static const struct ast_vbios_enhtable res_1920x1080[] = {
107 	{ 2200, 1920, 88, 44, 1125, 1080, 4, 5, VCLK148_5,	/* 60 Hz */
108 	  (SyncPP | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo |
109 	  AST2500PreCatchCRT), 60, 1, 0x38 },
110 	AST_VBIOS_INVALID_MODE,					/* end */
111 };
112 
113 /* 16:10 */
114 
115 static const struct ast_vbios_enhtable res_1280x800[] = {
116 	{ 1440, 1280, 48, 32, 823, 800, 3, 6, VCLK71,		/* 60 Hz RB */
117 	  (SyncNP | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo |
118 	  AST2500PreCatchCRT), 60, 1, 0x35 },
119 	{ 1680, 1280, 72, 128, 831, 800, 3, 6, VCLK83_5,	/* 60 Hz */
120 	  (SyncPN | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo), 60, 2, 0x35 },
121 	AST_VBIOS_INVALID_MODE,					/* end */
122 };
123 
124 static const struct ast_vbios_enhtable res_1440x900[] = {
125 	{ 1600, 1440, 48, 32, 926, 900, 3, 6, VCLK88_75,	/* 60 Hz RB */
126 	  (SyncNP | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo |
127 	  AST2500PreCatchCRT), 60, 1, 0x36 },
128 	{ 1904, 1440, 80, 152, 934, 900, 3, 6, VCLK106_5,	/* 60 Hz */
129 	  (SyncPN | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo), 60, 2, 0x36 },
130 	AST_VBIOS_INVALID_MODE,					/* end */
131 };
132 
133 static const struct ast_vbios_enhtable res_1680x1050[] = {
134 	{ 1840, 1680, 48, 32, 1080, 1050, 3, 6, VCLK119,	/* 60 Hz RB */
135 	  (SyncNP | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo |
136 	  AST2500PreCatchCRT), 60, 1, 0x37 },
137 	{ 2240, 1680, 104, 176, 1089, 1050, 3, 6, VCLK146_25,	/* 60 Hz */
138 	  (SyncPN | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo), 60, 2, 0x37 },
139 	AST_VBIOS_INVALID_MODE,					/* end */
140 };
141 
142 static const struct ast_vbios_enhtable res_1920x1200[] = {
143 	{ 2080, 1920, 48, 32, 1235, 1200, 3, 6, VCLK154,	/* 60 Hz RB*/
144 	  (SyncNP | Charx8Dot | LineCompareOff | WideScreenMode | NewModeInfo |
145 	  AST2500PreCatchCRT), 60, 1, 0x34 },
146 	AST_VBIOS_INVALID_MODE,					/* end */
147 };
148 
149 /*
150  * VBIOS mode tables
151  */
152 
153 static const struct ast_vbios_enhtable *res_table_wuxga[] = {
154 	&res_1920x1200[0],
155 	NULL,
156 };
157 
158 static const struct ast_vbios_enhtable *res_table_fullhd[] = {
159 	&res_1920x1080[0],
160 	NULL,
161 };
162 
163 static const struct ast_vbios_enhtable *res_table_wsxga_p[] = {
164 	&res_1280x800[0],
165 	&res_1360x768[0],
166 	&res_1440x900[0],
167 	&res_1600x900[0],
168 	&res_1680x1050[0],
169 	NULL,
170 };
171 
172 static const struct ast_vbios_enhtable *res_table[] = {
173 	&res_640x480[0],
174 	&res_800x600[0],
175 	&res_1024x768[0],
176 	&res_1152x864[0],
177 	&res_1280x1024[0],
178 	&res_1600x1200[0],
179 	NULL,
180 };
181 
182 static const struct ast_vbios_enhtable *
183 __ast_vbios_find_mode_table(const struct ast_vbios_enhtable **vmode_tables,
184 			    unsigned int hdisplay,
185 			    unsigned int vdisplay)
186 {
187 	while (*vmode_tables) {
188 		if ((*vmode_tables)->hde == hdisplay && (*vmode_tables)->vde == vdisplay)
189 			return *vmode_tables;
190 		++vmode_tables;
191 	}
192 
193 	return NULL;
194 }
195 
196 static const struct ast_vbios_enhtable *ast_vbios_find_mode_table(const struct ast_device *ast,
197 								  unsigned int hdisplay,
198 								  unsigned int vdisplay)
199 {
200 	const struct ast_vbios_enhtable *vmode_table = NULL;
201 
202 	if (ast->support_wuxga)
203 		vmode_table = __ast_vbios_find_mode_table(res_table_wuxga, hdisplay, vdisplay);
204 	if (!vmode_table && ast->support_fullhd)
205 		vmode_table = __ast_vbios_find_mode_table(res_table_fullhd, hdisplay, vdisplay);
206 	if (!vmode_table && ast->support_wsxga_p)
207 		vmode_table = __ast_vbios_find_mode_table(res_table_wsxga_p, hdisplay, vdisplay);
208 	if (!vmode_table)
209 		vmode_table = __ast_vbios_find_mode_table(res_table, hdisplay, vdisplay);
210 
211 	return vmode_table;
212 }
213 
214 const struct ast_vbios_enhtable *ast_vbios_find_mode(const struct ast_device *ast,
215 						     const struct drm_display_mode *mode)
216 {
217 	const struct ast_vbios_enhtable *best_vmode = NULL;
218 	const struct ast_vbios_enhtable *vmode_table;
219 	const struct ast_vbios_enhtable *vmode;
220 	u32 refresh_rate;
221 
222 	vmode_table = ast_vbios_find_mode_table(ast, mode->hdisplay, mode->vdisplay);
223 	if (!vmode_table)
224 		return NULL;
225 
226 	refresh_rate = drm_mode_vrefresh(mode);
227 
228 	for (vmode = vmode_table; ast_vbios_mode_is_valid(vmode); ++vmode) {
229 		if (((mode->flags & DRM_MODE_FLAG_NVSYNC) && (vmode->flags & PVSync)) ||
230 		    ((mode->flags & DRM_MODE_FLAG_PVSYNC) && (vmode->flags & NVSync)) ||
231 		    ((mode->flags & DRM_MODE_FLAG_NHSYNC) && (vmode->flags & PHSync)) ||
232 		    ((mode->flags & DRM_MODE_FLAG_PHSYNC) && (vmode->flags & NHSync))) {
233 			continue;
234 		}
235 		if (vmode->refresh_rate <= refresh_rate &&
236 		    (!best_vmode || vmode->refresh_rate > best_vmode->refresh_rate))
237 			best_vmode = vmode;
238 	}
239 
240 	return best_vmode;
241 }
242