xref: /linux/drivers/gpu/drm/ast/ast_mode.c (revision 42d37fc0c819b81f6f6afd108b55d04ba9d32d0f)
1 /*
2  * Copyright 2012 Red Hat Inc.
3  * Parts based on xf86-video-ast
4  * Copyright (c) 2005 ASPEED Technology Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
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 NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  */
27 /*
28  * Authors: Dave Airlie <airlied@redhat.com>
29  */
30 
31 #include <linux/export.h>
32 #include <linux/pci.h>
33 
34 #include <drm/drm_atomic.h>
35 #include <drm/drm_atomic_helper.h>
36 #include <drm/drm_atomic_state_helper.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_damage_helper.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_format_helper.h>
41 #include <drm/drm_fourcc.h>
42 #include <drm/drm_gem_atomic_helper.h>
43 #include <drm/drm_gem_framebuffer_helper.h>
44 #include <drm/drm_gem_shmem_helper.h>
45 #include <drm/drm_managed.h>
46 #include <drm/drm_panic.h>
47 #include <drm/drm_probe_helper.h>
48 
49 #include "ast_ddc.h"
50 #include "ast_drv.h"
51 #include "ast_tables.h"
52 
53 #define AST_LUT_SIZE 256
54 
55 static inline void ast_load_palette_index(struct ast_device *ast,
56 				     u8 index, u8 red, u8 green,
57 				     u8 blue)
58 {
59 	ast_io_write8(ast, AST_IO_VGADWR, index);
60 	ast_io_read8(ast, AST_IO_VGASRI);
61 	ast_io_write8(ast, AST_IO_VGAPDR, red);
62 	ast_io_read8(ast, AST_IO_VGASRI);
63 	ast_io_write8(ast, AST_IO_VGAPDR, green);
64 	ast_io_read8(ast, AST_IO_VGASRI);
65 	ast_io_write8(ast, AST_IO_VGAPDR, blue);
66 	ast_io_read8(ast, AST_IO_VGASRI);
67 }
68 
69 static void ast_crtc_set_gamma_linear(struct ast_device *ast,
70 				      const struct drm_format_info *format)
71 {
72 	int i;
73 
74 	switch (format->format) {
75 	case DRM_FORMAT_C8: /* In this case, gamma table is used as color palette */
76 	case DRM_FORMAT_RGB565:
77 	case DRM_FORMAT_XRGB8888:
78 		for (i = 0; i < AST_LUT_SIZE; i++)
79 			ast_load_palette_index(ast, i, i, i, i);
80 		break;
81 	default:
82 		drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n",
83 			      &format->format);
84 		break;
85 	}
86 }
87 
88 static void ast_crtc_set_gamma(struct ast_device *ast,
89 			       const struct drm_format_info *format,
90 			       struct drm_color_lut *lut)
91 {
92 	int i;
93 
94 	switch (format->format) {
95 	case DRM_FORMAT_C8: /* In this case, gamma table is used as color palette */
96 	case DRM_FORMAT_RGB565:
97 	case DRM_FORMAT_XRGB8888:
98 		for (i = 0; i < AST_LUT_SIZE; i++)
99 			ast_load_palette_index(ast, i,
100 					       lut[i].red >> 8,
101 					       lut[i].green >> 8,
102 					       lut[i].blue >> 8);
103 		break;
104 	default:
105 		drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n",
106 			      &format->format);
107 		break;
108 	}
109 }
110 
111 static bool ast_get_vbios_mode_info(const struct drm_format_info *format,
112 				    const struct drm_display_mode *mode,
113 				    struct drm_display_mode *adjusted_mode,
114 				    struct ast_vbios_mode_info *vbios_mode)
115 {
116 	u32 refresh_rate_index = 0, refresh_rate;
117 	const struct ast_vbios_enhtable *best = NULL;
118 	u32 hborder, vborder;
119 	bool check_sync;
120 
121 	switch (format->cpp[0] * 8) {
122 	case 8:
123 		vbios_mode->std_table = &vbios_stdtable[VGAModeIndex];
124 		break;
125 	case 16:
126 		vbios_mode->std_table = &vbios_stdtable[HiCModeIndex];
127 		break;
128 	case 24:
129 	case 32:
130 		vbios_mode->std_table = &vbios_stdtable[TrueCModeIndex];
131 		break;
132 	default:
133 		return false;
134 	}
135 
136 	switch (mode->crtc_hdisplay) {
137 	case 640:
138 		vbios_mode->enh_table = &res_640x480[refresh_rate_index];
139 		break;
140 	case 800:
141 		vbios_mode->enh_table = &res_800x600[refresh_rate_index];
142 		break;
143 	case 1024:
144 		vbios_mode->enh_table = &res_1024x768[refresh_rate_index];
145 		break;
146 	case 1152:
147 		vbios_mode->enh_table = &res_1152x864[refresh_rate_index];
148 		break;
149 	case 1280:
150 		if (mode->crtc_vdisplay == 800)
151 			vbios_mode->enh_table = &res_1280x800[refresh_rate_index];
152 		else
153 			vbios_mode->enh_table = &res_1280x1024[refresh_rate_index];
154 		break;
155 	case 1360:
156 		vbios_mode->enh_table = &res_1360x768[refresh_rate_index];
157 		break;
158 	case 1440:
159 		vbios_mode->enh_table = &res_1440x900[refresh_rate_index];
160 		break;
161 	case 1600:
162 		if (mode->crtc_vdisplay == 900)
163 			vbios_mode->enh_table = &res_1600x900[refresh_rate_index];
164 		else
165 			vbios_mode->enh_table = &res_1600x1200[refresh_rate_index];
166 		break;
167 	case 1680:
168 		vbios_mode->enh_table = &res_1680x1050[refresh_rate_index];
169 		break;
170 	case 1920:
171 		if (mode->crtc_vdisplay == 1080)
172 			vbios_mode->enh_table = &res_1920x1080[refresh_rate_index];
173 		else
174 			vbios_mode->enh_table = &res_1920x1200[refresh_rate_index];
175 		break;
176 	default:
177 		return false;
178 	}
179 
180 	refresh_rate = drm_mode_vrefresh(mode);
181 	check_sync = vbios_mode->enh_table->flags & WideScreenMode;
182 
183 	while (1) {
184 		const struct ast_vbios_enhtable *loop = vbios_mode->enh_table;
185 
186 		while (loop->refresh_rate != 0xff) {
187 			if ((check_sync) &&
188 			    (((mode->flags & DRM_MODE_FLAG_NVSYNC)  &&
189 			      (loop->flags & PVSync))  ||
190 			     ((mode->flags & DRM_MODE_FLAG_PVSYNC)  &&
191 			      (loop->flags & NVSync))  ||
192 			     ((mode->flags & DRM_MODE_FLAG_NHSYNC)  &&
193 			      (loop->flags & PHSync))  ||
194 			     ((mode->flags & DRM_MODE_FLAG_PHSYNC)  &&
195 			      (loop->flags & NHSync)))) {
196 				loop++;
197 				continue;
198 			}
199 			if (loop->refresh_rate <= refresh_rate
200 			    && (!best || loop->refresh_rate > best->refresh_rate))
201 				best = loop;
202 			loop++;
203 		}
204 		if (best || !check_sync)
205 			break;
206 		check_sync = 0;
207 	}
208 
209 	if (best)
210 		vbios_mode->enh_table = best;
211 
212 	hborder = (vbios_mode->enh_table->flags & HBorder) ? 8 : 0;
213 	vborder = (vbios_mode->enh_table->flags & VBorder) ? 8 : 0;
214 
215 	adjusted_mode->crtc_htotal = vbios_mode->enh_table->ht;
216 	adjusted_mode->crtc_hblank_start = vbios_mode->enh_table->hde + hborder;
217 	adjusted_mode->crtc_hblank_end = vbios_mode->enh_table->ht - hborder;
218 	adjusted_mode->crtc_hsync_start = vbios_mode->enh_table->hde + hborder +
219 		vbios_mode->enh_table->hfp;
220 	adjusted_mode->crtc_hsync_end = (vbios_mode->enh_table->hde + hborder +
221 					 vbios_mode->enh_table->hfp +
222 					 vbios_mode->enh_table->hsync);
223 
224 	adjusted_mode->crtc_vtotal = vbios_mode->enh_table->vt;
225 	adjusted_mode->crtc_vblank_start = vbios_mode->enh_table->vde + vborder;
226 	adjusted_mode->crtc_vblank_end = vbios_mode->enh_table->vt - vborder;
227 	adjusted_mode->crtc_vsync_start = vbios_mode->enh_table->vde + vborder +
228 		vbios_mode->enh_table->vfp;
229 	adjusted_mode->crtc_vsync_end = (vbios_mode->enh_table->vde + vborder +
230 					 vbios_mode->enh_table->vfp +
231 					 vbios_mode->enh_table->vsync);
232 
233 	return true;
234 }
235 
236 static void ast_set_vbios_color_reg(struct ast_device *ast,
237 				    const struct drm_format_info *format,
238 				    const struct ast_vbios_mode_info *vbios_mode)
239 {
240 	u32 color_index;
241 
242 	switch (format->cpp[0]) {
243 	case 1:
244 		color_index = VGAModeIndex - 1;
245 		break;
246 	case 2:
247 		color_index = HiCModeIndex;
248 		break;
249 	case 3:
250 	case 4:
251 		color_index = TrueCModeIndex;
252 		break;
253 	default:
254 		return;
255 	}
256 
257 	ast_set_index_reg(ast, AST_IO_VGACRI, 0x8c, (u8)((color_index & 0x0f) << 4));
258 
259 	ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0x00);
260 
261 	if (vbios_mode->enh_table->flags & NewModeInfo) {
262 		ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0xa8);
263 		ast_set_index_reg(ast, AST_IO_VGACRI, 0x92, format->cpp[0] * 8);
264 	}
265 }
266 
267 static void ast_set_vbios_mode_reg(struct ast_device *ast,
268 				   const struct drm_display_mode *adjusted_mode,
269 				   const struct ast_vbios_mode_info *vbios_mode)
270 {
271 	u32 refresh_rate_index, mode_id;
272 
273 	refresh_rate_index = vbios_mode->enh_table->refresh_rate_index;
274 	mode_id = vbios_mode->enh_table->mode_id;
275 
276 	ast_set_index_reg(ast, AST_IO_VGACRI, 0x8d, refresh_rate_index & 0xff);
277 	ast_set_index_reg(ast, AST_IO_VGACRI, 0x8e, mode_id & 0xff);
278 
279 	ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0x00);
280 
281 	if (vbios_mode->enh_table->flags & NewModeInfo) {
282 		ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0xa8);
283 		ast_set_index_reg(ast, AST_IO_VGACRI, 0x93, adjusted_mode->clock / 1000);
284 		ast_set_index_reg(ast, AST_IO_VGACRI, 0x94, adjusted_mode->crtc_hdisplay);
285 		ast_set_index_reg(ast, AST_IO_VGACRI, 0x95, adjusted_mode->crtc_hdisplay >> 8);
286 		ast_set_index_reg(ast, AST_IO_VGACRI, 0x96, adjusted_mode->crtc_vdisplay);
287 		ast_set_index_reg(ast, AST_IO_VGACRI, 0x97, adjusted_mode->crtc_vdisplay >> 8);
288 	}
289 }
290 
291 static void ast_set_std_reg(struct ast_device *ast,
292 			    struct drm_display_mode *mode,
293 			    struct ast_vbios_mode_info *vbios_mode)
294 {
295 	const struct ast_vbios_stdtable *stdtable;
296 	u32 i;
297 	u8 jreg;
298 
299 	stdtable = vbios_mode->std_table;
300 
301 	jreg = stdtable->misc;
302 	ast_io_write8(ast, AST_IO_VGAMR_W, jreg);
303 
304 	/* Set SEQ; except Screen Disable field */
305 	ast_set_index_reg(ast, AST_IO_VGASRI, 0x00, 0x03);
306 	ast_set_index_reg_mask(ast, AST_IO_VGASRI, 0x01, 0x20, stdtable->seq[0]);
307 	for (i = 1; i < 4; i++) {
308 		jreg = stdtable->seq[i];
309 		ast_set_index_reg(ast, AST_IO_VGASRI, (i + 1), jreg);
310 	}
311 
312 	/* Set CRTC; except base address and offset */
313 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x00);
314 	for (i = 0; i < 12; i++)
315 		ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]);
316 	for (i = 14; i < 19; i++)
317 		ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]);
318 	for (i = 20; i < 25; i++)
319 		ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]);
320 
321 	/* set AR */
322 	jreg = ast_io_read8(ast, AST_IO_VGAIR1_R);
323 	for (i = 0; i < 20; i++) {
324 		jreg = stdtable->ar[i];
325 		ast_io_write8(ast, AST_IO_VGAARI_W, (u8)i);
326 		ast_io_write8(ast, AST_IO_VGAARI_W, jreg);
327 	}
328 	ast_io_write8(ast, AST_IO_VGAARI_W, 0x14);
329 	ast_io_write8(ast, AST_IO_VGAARI_W, 0x00);
330 
331 	jreg = ast_io_read8(ast, AST_IO_VGAIR1_R);
332 	ast_io_write8(ast, AST_IO_VGAARI_W, 0x20);
333 
334 	/* Set GR */
335 	for (i = 0; i < 9; i++)
336 		ast_set_index_reg(ast, AST_IO_VGAGRI, i, stdtable->gr[i]);
337 }
338 
339 static void ast_set_crtc_reg(struct ast_device *ast,
340 			     struct drm_display_mode *mode,
341 			     struct ast_vbios_mode_info *vbios_mode)
342 {
343 	u8 jreg05 = 0, jreg07 = 0, jreg09 = 0, jregAC = 0, jregAD = 0, jregAE = 0;
344 	u16 temp, precache = 0;
345 
346 	if ((IS_AST_GEN6(ast) || IS_AST_GEN7(ast)) &&
347 	    (vbios_mode->enh_table->flags & AST2500PreCatchCRT))
348 		precache = 40;
349 
350 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x00);
351 
352 	temp = (mode->crtc_htotal >> 3) - 5;
353 	if (temp & 0x100)
354 		jregAC |= 0x01; /* HT D[8] */
355 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x00, 0x00, temp);
356 
357 	temp = (mode->crtc_hdisplay >> 3) - 1;
358 	if (temp & 0x100)
359 		jregAC |= 0x04; /* HDE D[8] */
360 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x01, 0x00, temp);
361 
362 	temp = (mode->crtc_hblank_start >> 3) - 1;
363 	if (temp & 0x100)
364 		jregAC |= 0x10; /* HBS D[8] */
365 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x02, 0x00, temp);
366 
367 	temp = ((mode->crtc_hblank_end >> 3) - 1) & 0x7f;
368 	if (temp & 0x20)
369 		jreg05 |= 0x80;  /* HBE D[5] */
370 	if (temp & 0x40)
371 		jregAD |= 0x01;  /* HBE D[5] */
372 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x03, 0xE0, (temp & 0x1f));
373 
374 	temp = ((mode->crtc_hsync_start-precache) >> 3) - 1;
375 	if (temp & 0x100)
376 		jregAC |= 0x40; /* HRS D[5] */
377 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x04, 0x00, temp);
378 
379 	temp = (((mode->crtc_hsync_end-precache) >> 3) - 1) & 0x3f;
380 	if (temp & 0x20)
381 		jregAD |= 0x04; /* HRE D[5] */
382 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x05, 0x60, (u8)((temp & 0x1f) | jreg05));
383 
384 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAC, 0x00, jregAC);
385 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAD, 0x00, jregAD);
386 
387 	// Workaround for HSync Time non octave pixels (1920x1080@60Hz HSync 44 pixels);
388 	if (IS_AST_GEN7(ast) && (mode->crtc_vdisplay == 1080))
389 		ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xFC, 0xFD, 0x02);
390 	else
391 		ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xFC, 0xFD, 0x00);
392 
393 	/* vert timings */
394 	temp = (mode->crtc_vtotal) - 2;
395 	if (temp & 0x100)
396 		jreg07 |= 0x01;
397 	if (temp & 0x200)
398 		jreg07 |= 0x20;
399 	if (temp & 0x400)
400 		jregAE |= 0x01;
401 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x06, 0x00, temp);
402 
403 	temp = (mode->crtc_vsync_start) - 1;
404 	if (temp & 0x100)
405 		jreg07 |= 0x04;
406 	if (temp & 0x200)
407 		jreg07 |= 0x80;
408 	if (temp & 0x400)
409 		jregAE |= 0x08;
410 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x10, 0x00, temp);
411 
412 	temp = (mode->crtc_vsync_end - 1) & 0x3f;
413 	if (temp & 0x10)
414 		jregAE |= 0x20;
415 	if (temp & 0x20)
416 		jregAE |= 0x40;
417 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x70, temp & 0xf);
418 
419 	temp = mode->crtc_vdisplay - 1;
420 	if (temp & 0x100)
421 		jreg07 |= 0x02;
422 	if (temp & 0x200)
423 		jreg07 |= 0x40;
424 	if (temp & 0x400)
425 		jregAE |= 0x02;
426 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x12, 0x00, temp);
427 
428 	temp = mode->crtc_vblank_start - 1;
429 	if (temp & 0x100)
430 		jreg07 |= 0x08;
431 	if (temp & 0x200)
432 		jreg09 |= 0x20;
433 	if (temp & 0x400)
434 		jregAE |= 0x04;
435 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x15, 0x00, temp);
436 
437 	temp = mode->crtc_vblank_end - 1;
438 	if (temp & 0x100)
439 		jregAE |= 0x10;
440 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x16, 0x00, temp);
441 
442 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x07, 0x00, jreg07);
443 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x09, 0xdf, jreg09);
444 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAE, 0x00, (jregAE | 0x80));
445 
446 	if (precache)
447 		ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0x3f, 0x80);
448 	else
449 		ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0x3f, 0x00);
450 
451 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x80);
452 }
453 
454 static void ast_set_offset_reg(struct ast_device *ast,
455 			       struct drm_framebuffer *fb)
456 {
457 	u16 offset;
458 
459 	offset = fb->pitches[0] >> 3;
460 	ast_set_index_reg(ast, AST_IO_VGACRI, 0x13, (offset & 0xff));
461 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xb0, (offset >> 8) & 0x3f);
462 }
463 
464 static void ast_set_dclk_reg(struct ast_device *ast,
465 			     struct drm_display_mode *mode,
466 			     struct ast_vbios_mode_info *vbios_mode)
467 {
468 	const struct ast_vbios_dclk_info *clk_info;
469 
470 	if (IS_AST_GEN6(ast) || IS_AST_GEN7(ast))
471 		clk_info = &dclk_table_ast2500[vbios_mode->enh_table->dclk_index];
472 	else
473 		clk_info = &dclk_table[vbios_mode->enh_table->dclk_index];
474 
475 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xc0, 0x00, clk_info->param1);
476 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xc1, 0x00, clk_info->param2);
477 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xbb, 0x0f,
478 			       (clk_info->param3 & 0xc0) |
479 			       ((clk_info->param3 & 0x3) << 4));
480 }
481 
482 static void ast_set_color_reg(struct ast_device *ast,
483 			      const struct drm_format_info *format)
484 {
485 	u8 jregA0 = 0, jregA3 = 0, jregA8 = 0;
486 
487 	switch (format->cpp[0] * 8) {
488 	case 8:
489 		jregA0 = 0x70;
490 		jregA3 = 0x01;
491 		jregA8 = 0x00;
492 		break;
493 	case 15:
494 	case 16:
495 		jregA0 = 0x70;
496 		jregA3 = 0x04;
497 		jregA8 = 0x02;
498 		break;
499 	case 32:
500 		jregA0 = 0x70;
501 		jregA3 = 0x08;
502 		jregA8 = 0x02;
503 		break;
504 	}
505 
506 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa0, 0x8f, jregA0);
507 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xf0, jregA3);
508 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa8, 0xfd, jregA8);
509 }
510 
511 static void ast_set_crtthd_reg(struct ast_device *ast)
512 {
513 	/* Set Threshold */
514 	if (IS_AST_GEN7(ast)) {
515 		ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0xe0);
516 		ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0xa0);
517 	} else if (IS_AST_GEN6(ast) || IS_AST_GEN5(ast) || IS_AST_GEN4(ast)) {
518 		ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0x78);
519 		ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0x60);
520 	} else if (IS_AST_GEN3(ast) || IS_AST_GEN2(ast)) {
521 		ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0x3f);
522 		ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0x2f);
523 	} else {
524 		ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0x2f);
525 		ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0x1f);
526 	}
527 }
528 
529 static void ast_set_sync_reg(struct ast_device *ast,
530 			     struct drm_display_mode *mode,
531 			     struct ast_vbios_mode_info *vbios_mode)
532 {
533 	u8 jreg;
534 
535 	jreg  = ast_io_read8(ast, AST_IO_VGAMR_R);
536 	jreg &= ~0xC0;
537 	if (vbios_mode->enh_table->flags & NVSync)
538 		jreg |= 0x80;
539 	if (vbios_mode->enh_table->flags & NHSync)
540 		jreg |= 0x40;
541 	ast_io_write8(ast, AST_IO_VGAMR_W, jreg);
542 }
543 
544 static void ast_set_start_address_crt1(struct ast_device *ast,
545 				       unsigned int offset)
546 {
547 	u32 addr;
548 
549 	addr = offset >> 2;
550 	ast_set_index_reg(ast, AST_IO_VGACRI, 0x0d, (u8)(addr & 0xff));
551 	ast_set_index_reg(ast, AST_IO_VGACRI, 0x0c, (u8)((addr >> 8) & 0xff));
552 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xaf, (u8)((addr >> 16) & 0xff));
553 
554 }
555 
556 static void ast_wait_for_vretrace(struct ast_device *ast)
557 {
558 	unsigned long timeout = jiffies + HZ;
559 	u8 vgair1;
560 
561 	do {
562 		vgair1 = ast_io_read8(ast, AST_IO_VGAIR1_R);
563 	} while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout));
564 }
565 
566 /*
567  * Planes
568  */
569 
570 static int ast_plane_init(struct drm_device *dev, struct ast_plane *ast_plane,
571 			  void __iomem *vaddr, u64 offset, unsigned long size,
572 			  uint32_t possible_crtcs,
573 			  const struct drm_plane_funcs *funcs,
574 			  const uint32_t *formats, unsigned int format_count,
575 			  const uint64_t *format_modifiers,
576 			  enum drm_plane_type type)
577 {
578 	struct drm_plane *plane = &ast_plane->base;
579 
580 	ast_plane->vaddr = vaddr;
581 	ast_plane->offset = offset;
582 	ast_plane->size = size;
583 
584 	return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
585 					formats, format_count, format_modifiers,
586 					type, NULL);
587 }
588 
589 /*
590  * Primary plane
591  */
592 
593 static const uint32_t ast_primary_plane_formats[] = {
594 	DRM_FORMAT_XRGB8888,
595 	DRM_FORMAT_RGB565,
596 	DRM_FORMAT_C8,
597 };
598 
599 static int ast_primary_plane_helper_atomic_check(struct drm_plane *plane,
600 						 struct drm_atomic_state *state)
601 {
602 	struct drm_device *dev = plane->dev;
603 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
604 	struct drm_crtc_state *new_crtc_state = NULL;
605 	struct ast_crtc_state *new_ast_crtc_state;
606 	int ret;
607 
608 	if (new_plane_state->crtc)
609 		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
610 
611 	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
612 						  DRM_PLANE_NO_SCALING,
613 						  DRM_PLANE_NO_SCALING,
614 						  false, true);
615 	if (ret) {
616 		return ret;
617 	} else if (!new_plane_state->visible) {
618 		if (drm_WARN_ON(dev, new_plane_state->crtc)) /* cannot legally happen */
619 			return -EINVAL;
620 		else
621 			return 0;
622 	}
623 
624 	new_ast_crtc_state = to_ast_crtc_state(new_crtc_state);
625 
626 	new_ast_crtc_state->format = new_plane_state->fb->format;
627 
628 	return 0;
629 }
630 
631 static void ast_handle_damage(struct ast_plane *ast_plane, struct iosys_map *src,
632 			      struct drm_framebuffer *fb,
633 			      const struct drm_rect *clip)
634 {
635 	struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(ast_plane->vaddr);
636 
637 	iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, clip));
638 	drm_fb_memcpy(&dst, fb->pitches, src, fb, clip);
639 }
640 
641 static void ast_primary_plane_helper_atomic_update(struct drm_plane *plane,
642 						   struct drm_atomic_state *state)
643 {
644 	struct drm_device *dev = plane->dev;
645 	struct ast_device *ast = to_ast_device(dev);
646 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
647 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
648 	struct drm_framebuffer *fb = plane_state->fb;
649 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
650 	struct drm_framebuffer *old_fb = old_plane_state->fb;
651 	struct ast_plane *ast_plane = to_ast_plane(plane);
652 	struct drm_crtc *crtc = plane_state->crtc;
653 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
654 	struct drm_rect damage;
655 	struct drm_atomic_helper_damage_iter iter;
656 
657 	if (!old_fb || (fb->format != old_fb->format) || crtc_state->mode_changed) {
658 		struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
659 		struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
660 
661 		ast_set_color_reg(ast, fb->format);
662 		ast_set_vbios_color_reg(ast, fb->format, vbios_mode_info);
663 	}
664 
665 	drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
666 	drm_atomic_for_each_plane_damage(&iter, &damage) {
667 		ast_handle_damage(ast_plane, shadow_plane_state->data, fb, &damage);
668 	}
669 
670 	/*
671 	 * Some BMCs stop scanning out the video signal after the driver
672 	 * reprogrammed the offset. This stalls display output for several
673 	 * seconds and makes the display unusable. Therefore only update
674 	 * the offset if it changes.
675 	 */
676 	if (!old_fb || old_fb->pitches[0] != fb->pitches[0])
677 		ast_set_offset_reg(ast, fb);
678 }
679 
680 static void ast_primary_plane_helper_atomic_enable(struct drm_plane *plane,
681 						   struct drm_atomic_state *state)
682 {
683 	struct ast_device *ast = to_ast_device(plane->dev);
684 	struct ast_plane *ast_plane = to_ast_plane(plane);
685 
686 	/*
687 	 * Some BMCs stop scanning out the video signal after the driver
688 	 * reprogrammed the scanout address. This stalls display
689 	 * output for several seconds and makes the display unusable.
690 	 * Therefore only reprogram the address after enabling the plane.
691 	 */
692 	ast_set_start_address_crt1(ast, (u32)ast_plane->offset);
693 }
694 
695 static void ast_primary_plane_helper_atomic_disable(struct drm_plane *plane,
696 						    struct drm_atomic_state *state)
697 {
698 	/*
699 	 * Keep this empty function to avoid calling
700 	 * atomic_update when disabling the plane.
701 	 */
702 }
703 
704 static int ast_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane,
705 						       struct drm_scanout_buffer *sb)
706 {
707 	struct ast_plane *ast_plane = to_ast_plane(plane);
708 
709 	if (plane->state && plane->state->fb && ast_plane->vaddr) {
710 		sb->format = plane->state->fb->format;
711 		sb->width = plane->state->fb->width;
712 		sb->height = plane->state->fb->height;
713 		sb->pitch[0] = plane->state->fb->pitches[0];
714 		iosys_map_set_vaddr_iomem(&sb->map[0], ast_plane->vaddr);
715 		return 0;
716 	}
717 	return -ENODEV;
718 }
719 
720 static const struct drm_plane_helper_funcs ast_primary_plane_helper_funcs = {
721 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
722 	.atomic_check = ast_primary_plane_helper_atomic_check,
723 	.atomic_update = ast_primary_plane_helper_atomic_update,
724 	.atomic_enable = ast_primary_plane_helper_atomic_enable,
725 	.atomic_disable = ast_primary_plane_helper_atomic_disable,
726 	.get_scanout_buffer = ast_primary_plane_helper_get_scanout_buffer,
727 };
728 
729 static const struct drm_plane_funcs ast_primary_plane_funcs = {
730 	.update_plane = drm_atomic_helper_update_plane,
731 	.disable_plane = drm_atomic_helper_disable_plane,
732 	.destroy = drm_plane_cleanup,
733 	DRM_GEM_SHADOW_PLANE_FUNCS,
734 };
735 
736 static int ast_primary_plane_init(struct ast_device *ast)
737 {
738 	struct drm_device *dev = &ast->base;
739 	struct ast_plane *ast_primary_plane = &ast->primary_plane;
740 	struct drm_plane *primary_plane = &ast_primary_plane->base;
741 	void __iomem *vaddr = ast->vram;
742 	u64 offset = 0; /* with shmem, the primary plane is always at offset 0 */
743 	unsigned long cursor_size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
744 	unsigned long size = ast->vram_fb_available - cursor_size;
745 	int ret;
746 
747 	ret = ast_plane_init(dev, ast_primary_plane, vaddr, offset, size,
748 			     0x01, &ast_primary_plane_funcs,
749 			     ast_primary_plane_formats, ARRAY_SIZE(ast_primary_plane_formats),
750 			     NULL, DRM_PLANE_TYPE_PRIMARY);
751 	if (ret) {
752 		drm_err(dev, "ast_plane_init() failed: %d\n", ret);
753 		return ret;
754 	}
755 	drm_plane_helper_add(primary_plane, &ast_primary_plane_helper_funcs);
756 	drm_plane_enable_fb_damage_clips(primary_plane);
757 
758 	return 0;
759 }
760 
761 /*
762  * Cursor plane
763  */
764 
765 static void ast_update_cursor_image(u8 __iomem *dst, const u8 *src, int width, int height)
766 {
767 	union {
768 		u32 ul;
769 		u8 b[4];
770 	} srcdata32[2], data32;
771 	union {
772 		u16 us;
773 		u8 b[2];
774 	} data16;
775 	u32 csum = 0;
776 	s32 alpha_dst_delta, last_alpha_dst_delta;
777 	u8 __iomem *dstxor;
778 	const u8 *srcxor;
779 	int i, j;
780 	u32 per_pixel_copy, two_pixel_copy;
781 
782 	alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
783 	last_alpha_dst_delta = alpha_dst_delta - (width << 1);
784 
785 	srcxor = src;
786 	dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
787 	per_pixel_copy = width & 1;
788 	two_pixel_copy = width >> 1;
789 
790 	for (j = 0; j < height; j++) {
791 		for (i = 0; i < two_pixel_copy; i++) {
792 			srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
793 			srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
794 			data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
795 			data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
796 			data32.b[2] = srcdata32[1].b[1] | (srcdata32[1].b[0] >> 4);
797 			data32.b[3] = srcdata32[1].b[3] | (srcdata32[1].b[2] >> 4);
798 
799 			writel(data32.ul, dstxor);
800 			csum += data32.ul;
801 
802 			dstxor += 4;
803 			srcxor += 8;
804 
805 		}
806 
807 		for (i = 0; i < per_pixel_copy; i++) {
808 			srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
809 			data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
810 			data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
811 			writew(data16.us, dstxor);
812 			csum += (u32)data16.us;
813 
814 			dstxor += 2;
815 			srcxor += 4;
816 		}
817 		dstxor += last_alpha_dst_delta;
818 	}
819 
820 	/* write checksum + signature */
821 	dst += AST_HWC_SIZE;
822 	writel(csum, dst);
823 	writel(width, dst + AST_HWC_SIGNATURE_SizeX);
824 	writel(height, dst + AST_HWC_SIGNATURE_SizeY);
825 	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
826 	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
827 }
828 
829 static void ast_set_cursor_base(struct ast_device *ast, u64 address)
830 {
831 	u8 addr0 = (address >> 3) & 0xff;
832 	u8 addr1 = (address >> 11) & 0xff;
833 	u8 addr2 = (address >> 19) & 0xff;
834 
835 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc8, addr0);
836 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc9, addr1);
837 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xca, addr2);
838 }
839 
840 static void ast_set_cursor_location(struct ast_device *ast, u16 x, u16 y,
841 				    u8 x_offset, u8 y_offset)
842 {
843 	u8 x0 = (x & 0x00ff);
844 	u8 x1 = (x & 0x0f00) >> 8;
845 	u8 y0 = (y & 0x00ff);
846 	u8 y1 = (y & 0x0700) >> 8;
847 
848 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc2, x_offset);
849 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc3, y_offset);
850 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc4, x0);
851 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc5, x1);
852 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc6, y0);
853 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc7, y1);
854 }
855 
856 static void ast_set_cursor_enabled(struct ast_device *ast, bool enabled)
857 {
858 	static const u8 mask = (u8)~(AST_IO_VGACRCB_HWC_16BPP |
859 				     AST_IO_VGACRCB_HWC_ENABLED);
860 
861 	u8 vgacrcb = AST_IO_VGACRCB_HWC_16BPP;
862 
863 	if (enabled)
864 		vgacrcb |= AST_IO_VGACRCB_HWC_ENABLED;
865 
866 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xcb, mask, vgacrcb);
867 }
868 
869 static const uint32_t ast_cursor_plane_formats[] = {
870 	DRM_FORMAT_ARGB8888,
871 };
872 
873 static int ast_cursor_plane_helper_atomic_check(struct drm_plane *plane,
874 						struct drm_atomic_state *state)
875 {
876 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
877 	struct drm_framebuffer *new_fb = new_plane_state->fb;
878 	struct drm_crtc_state *new_crtc_state = NULL;
879 	int ret;
880 
881 	if (new_plane_state->crtc)
882 		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
883 
884 	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
885 						  DRM_PLANE_NO_SCALING,
886 						  DRM_PLANE_NO_SCALING,
887 						  true, true);
888 	if (ret || !new_plane_state->visible)
889 		return ret;
890 
891 	if (new_fb->width > AST_MAX_HWC_WIDTH || new_fb->height > AST_MAX_HWC_HEIGHT)
892 		return -EINVAL;
893 
894 	return 0;
895 }
896 
897 static void ast_cursor_plane_helper_atomic_update(struct drm_plane *plane,
898 						  struct drm_atomic_state *state)
899 {
900 	struct ast_plane *ast_plane = to_ast_plane(plane);
901 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
902 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
903 	struct drm_framebuffer *fb = plane_state->fb;
904 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
905 	struct ast_device *ast = to_ast_device(plane->dev);
906 	struct iosys_map src_map = shadow_plane_state->data[0];
907 	struct drm_rect damage;
908 	const u8 *src = src_map.vaddr; /* TODO: Use mapping abstraction properly */
909 	u64 dst_off = ast_plane->offset;
910 	u8 __iomem *dst = ast_plane->vaddr; /* TODO: Use mapping abstraction properly */
911 	u8 __iomem *sig = dst + AST_HWC_SIZE; /* TODO: Use mapping abstraction properly */
912 	unsigned int offset_x, offset_y;
913 	u16 x, y;
914 	u8 x_offset, y_offset;
915 
916 	/*
917 	 * Do data transfer to hardware buffer and point the scanout
918 	 * engine to the offset.
919 	 */
920 
921 	if (drm_atomic_helper_damage_merged(old_plane_state, plane_state, &damage)) {
922 		ast_update_cursor_image(dst, src, fb->width, fb->height);
923 		ast_set_cursor_base(ast, dst_off);
924 	}
925 
926 	/*
927 	 * Update location in HWC signature and registers.
928 	 */
929 
930 	writel(plane_state->crtc_x, sig + AST_HWC_SIGNATURE_X);
931 	writel(plane_state->crtc_y, sig + AST_HWC_SIGNATURE_Y);
932 
933 	offset_x = AST_MAX_HWC_WIDTH - fb->width;
934 	offset_y = AST_MAX_HWC_HEIGHT - fb->height;
935 
936 	if (plane_state->crtc_x < 0) {
937 		x_offset = (-plane_state->crtc_x) + offset_x;
938 		x = 0;
939 	} else {
940 		x_offset = offset_x;
941 		x = plane_state->crtc_x;
942 	}
943 	if (plane_state->crtc_y < 0) {
944 		y_offset = (-plane_state->crtc_y) + offset_y;
945 		y = 0;
946 	} else {
947 		y_offset = offset_y;
948 		y = plane_state->crtc_y;
949 	}
950 
951 	ast_set_cursor_location(ast, x, y, x_offset, y_offset);
952 
953 	/* Dummy write to enable HWC and make the HW pick-up the changes. */
954 	ast_set_cursor_enabled(ast, true);
955 }
956 
957 static void ast_cursor_plane_helper_atomic_disable(struct drm_plane *plane,
958 						   struct drm_atomic_state *state)
959 {
960 	struct ast_device *ast = to_ast_device(plane->dev);
961 
962 	ast_set_cursor_enabled(ast, false);
963 }
964 
965 static const struct drm_plane_helper_funcs ast_cursor_plane_helper_funcs = {
966 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
967 	.atomic_check = ast_cursor_plane_helper_atomic_check,
968 	.atomic_update = ast_cursor_plane_helper_atomic_update,
969 	.atomic_disable = ast_cursor_plane_helper_atomic_disable,
970 };
971 
972 static const struct drm_plane_funcs ast_cursor_plane_funcs = {
973 	.update_plane = drm_atomic_helper_update_plane,
974 	.disable_plane = drm_atomic_helper_disable_plane,
975 	.destroy = drm_plane_cleanup,
976 	DRM_GEM_SHADOW_PLANE_FUNCS,
977 };
978 
979 static int ast_cursor_plane_init(struct ast_device *ast)
980 {
981 	struct drm_device *dev = &ast->base;
982 	struct ast_plane *ast_cursor_plane = &ast->cursor_plane;
983 	struct drm_plane *cursor_plane = &ast_cursor_plane->base;
984 	size_t size;
985 	void __iomem *vaddr;
986 	u64 offset;
987 	int ret;
988 
989 	/*
990 	 * Allocate backing storage for cursors. The BOs are permanently
991 	 * pinned to the top end of the VRAM.
992 	 */
993 
994 	size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
995 
996 	if (ast->vram_fb_available < size)
997 		return -ENOMEM;
998 
999 	vaddr = ast->vram + ast->vram_fb_available - size;
1000 	offset = ast->vram_fb_available - size;
1001 
1002 	ret = ast_plane_init(dev, ast_cursor_plane, vaddr, offset, size,
1003 			     0x01, &ast_cursor_plane_funcs,
1004 			     ast_cursor_plane_formats, ARRAY_SIZE(ast_cursor_plane_formats),
1005 			     NULL, DRM_PLANE_TYPE_CURSOR);
1006 	if (ret) {
1007 		drm_err(dev, "ast_plane_init() failed: %d\n", ret);
1008 		return ret;
1009 	}
1010 	drm_plane_helper_add(cursor_plane, &ast_cursor_plane_helper_funcs);
1011 	drm_plane_enable_fb_damage_clips(cursor_plane);
1012 
1013 	ast->vram_fb_available -= size;
1014 
1015 	return 0;
1016 }
1017 
1018 /*
1019  * CRTC
1020  */
1021 
1022 static enum drm_mode_status
1023 ast_crtc_helper_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode)
1024 {
1025 	struct ast_device *ast = to_ast_device(crtc->dev);
1026 	enum drm_mode_status status;
1027 	uint32_t jtemp;
1028 
1029 	if (ast->support_wide_screen) {
1030 		if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
1031 			return MODE_OK;
1032 		if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
1033 			return MODE_OK;
1034 		if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
1035 			return MODE_OK;
1036 		if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
1037 			return MODE_OK;
1038 		if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
1039 			return MODE_OK;
1040 		if ((mode->hdisplay == 1152) && (mode->vdisplay == 864))
1041 			return MODE_OK;
1042 
1043 		if ((ast->chip == AST2100) || // GEN2, but not AST1100 (?)
1044 		    (ast->chip == AST2200) || // GEN3, but not AST2150 (?)
1045 		    IS_AST_GEN4(ast) || IS_AST_GEN5(ast) ||
1046 		    IS_AST_GEN6(ast) || IS_AST_GEN7(ast)) {
1047 			if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
1048 				return MODE_OK;
1049 
1050 			if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
1051 				jtemp = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, 0xff);
1052 				if (jtemp & 0x01)
1053 					return MODE_NOMODE;
1054 				else
1055 					return MODE_OK;
1056 			}
1057 		}
1058 	}
1059 
1060 	status = MODE_NOMODE;
1061 
1062 	switch (mode->hdisplay) {
1063 	case 640:
1064 		if (mode->vdisplay == 480)
1065 			status = MODE_OK;
1066 		break;
1067 	case 800:
1068 		if (mode->vdisplay == 600)
1069 			status = MODE_OK;
1070 		break;
1071 	case 1024:
1072 		if (mode->vdisplay == 768)
1073 			status = MODE_OK;
1074 		break;
1075 	case 1152:
1076 		if (mode->vdisplay == 864)
1077 			status = MODE_OK;
1078 		break;
1079 	case 1280:
1080 		if (mode->vdisplay == 1024)
1081 			status = MODE_OK;
1082 		break;
1083 	case 1600:
1084 		if (mode->vdisplay == 1200)
1085 			status = MODE_OK;
1086 		break;
1087 	default:
1088 		break;
1089 	}
1090 
1091 	return status;
1092 }
1093 
1094 static void ast_crtc_helper_mode_set_nofb(struct drm_crtc *crtc)
1095 {
1096 	struct drm_device *dev = crtc->dev;
1097 	struct ast_device *ast = to_ast_device(dev);
1098 	struct drm_crtc_state *crtc_state = crtc->state;
1099 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
1100 	struct ast_vbios_mode_info *vbios_mode_info =
1101 		&ast_crtc_state->vbios_mode_info;
1102 	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
1103 
1104 	/*
1105 	 * Ensure that no scanout takes place before reprogramming mode
1106 	 * and format registers.
1107 	 *
1108 	 * TODO: Get vblank interrupts working and remove this line.
1109 	 */
1110 	ast_wait_for_vretrace(ast);
1111 
1112 	ast_set_vbios_mode_reg(ast, adjusted_mode, vbios_mode_info);
1113 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xa1, 0x06);
1114 	ast_set_std_reg(ast, adjusted_mode, vbios_mode_info);
1115 	ast_set_crtc_reg(ast, adjusted_mode, vbios_mode_info);
1116 	ast_set_dclk_reg(ast, adjusted_mode, vbios_mode_info);
1117 	ast_set_crtthd_reg(ast);
1118 	ast_set_sync_reg(ast, adjusted_mode, vbios_mode_info);
1119 }
1120 
1121 static int ast_crtc_helper_atomic_check(struct drm_crtc *crtc,
1122 					struct drm_atomic_state *state)
1123 {
1124 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1125 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
1126 	struct ast_crtc_state *old_ast_crtc_state = to_ast_crtc_state(old_crtc_state);
1127 	struct drm_device *dev = crtc->dev;
1128 	struct ast_crtc_state *ast_state;
1129 	const struct drm_format_info *format;
1130 	bool succ;
1131 	int ret;
1132 
1133 	if (!crtc_state->enable)
1134 		return 0;
1135 
1136 	ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state);
1137 	if (ret)
1138 		return ret;
1139 
1140 	ast_state = to_ast_crtc_state(crtc_state);
1141 
1142 	format = ast_state->format;
1143 	if (drm_WARN_ON_ONCE(dev, !format))
1144 		return -EINVAL; /* BUG: We didn't set format in primary check(). */
1145 
1146 	/*
1147 	 * The gamma LUT has to be reloaded after changing the primary
1148 	 * plane's color format.
1149 	 */
1150 	if (old_ast_crtc_state->format != format)
1151 		crtc_state->color_mgmt_changed = true;
1152 
1153 	if (crtc_state->color_mgmt_changed && crtc_state->gamma_lut) {
1154 		if (crtc_state->gamma_lut->length !=
1155 		    AST_LUT_SIZE * sizeof(struct drm_color_lut)) {
1156 			drm_err(dev, "Wrong size for gamma_lut %zu\n",
1157 				crtc_state->gamma_lut->length);
1158 			return -EINVAL;
1159 		}
1160 	}
1161 
1162 	succ = ast_get_vbios_mode_info(format, &crtc_state->mode,
1163 				       &crtc_state->adjusted_mode,
1164 				       &ast_state->vbios_mode_info);
1165 	if (!succ)
1166 		return -EINVAL;
1167 
1168 	return 0;
1169 }
1170 
1171 static void
1172 ast_crtc_helper_atomic_flush(struct drm_crtc *crtc,
1173 			     struct drm_atomic_state *state)
1174 {
1175 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
1176 									  crtc);
1177 	struct drm_device *dev = crtc->dev;
1178 	struct ast_device *ast = to_ast_device(dev);
1179 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
1180 
1181 	/*
1182 	 * The gamma LUT has to be reloaded after changing the primary
1183 	 * plane's color format.
1184 	 */
1185 	if (crtc_state->enable && crtc_state->color_mgmt_changed) {
1186 		if (crtc_state->gamma_lut)
1187 			ast_crtc_set_gamma(ast,
1188 					   ast_crtc_state->format,
1189 					   crtc_state->gamma_lut->data);
1190 		else
1191 			ast_crtc_set_gamma_linear(ast, ast_crtc_state->format);
1192 	}
1193 }
1194 
1195 static void ast_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state)
1196 {
1197 	struct ast_device *ast = to_ast_device(crtc->dev);
1198 
1199 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0xfc, 0x00);
1200 	ast_set_index_reg_mask(ast, AST_IO_VGASRI, 0x01, 0xdf, 0x00);
1201 }
1202 
1203 static void ast_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state)
1204 {
1205 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
1206 	struct ast_device *ast = to_ast_device(crtc->dev);
1207 	u8 vgacrb6;
1208 
1209 	ast_set_index_reg_mask(ast, AST_IO_VGASRI, 0x01, 0xdf, AST_IO_VGASR1_SD);
1210 
1211 	vgacrb6 = AST_IO_VGACRB6_VSYNC_OFF |
1212 		  AST_IO_VGACRB6_HSYNC_OFF;
1213 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0xfc, vgacrb6);
1214 
1215 	/*
1216 	 * HW cursors require the underlying primary plane and CRTC to
1217 	 * display a valid mode and image. This is not the case during
1218 	 * full modeset operations. So we temporarily disable any active
1219 	 * plane, including the HW cursor. Each plane's atomic_update()
1220 	 * helper will re-enable it if necessary.
1221 	 *
1222 	 * We only do this during *full* modesets. It does not affect
1223 	 * simple pageflips on the planes.
1224 	 */
1225 	drm_atomic_helper_disable_planes_on_crtc(old_crtc_state, false);
1226 }
1227 
1228 static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
1229 	.mode_valid = ast_crtc_helper_mode_valid,
1230 	.mode_set_nofb = ast_crtc_helper_mode_set_nofb,
1231 	.atomic_check = ast_crtc_helper_atomic_check,
1232 	.atomic_flush = ast_crtc_helper_atomic_flush,
1233 	.atomic_enable = ast_crtc_helper_atomic_enable,
1234 	.atomic_disable = ast_crtc_helper_atomic_disable,
1235 };
1236 
1237 static void ast_crtc_reset(struct drm_crtc *crtc)
1238 {
1239 	struct ast_crtc_state *ast_state =
1240 		kzalloc(sizeof(*ast_state), GFP_KERNEL);
1241 
1242 	if (crtc->state)
1243 		crtc->funcs->atomic_destroy_state(crtc, crtc->state);
1244 
1245 	if (ast_state)
1246 		__drm_atomic_helper_crtc_reset(crtc, &ast_state->base);
1247 	else
1248 		__drm_atomic_helper_crtc_reset(crtc, NULL);
1249 }
1250 
1251 static struct drm_crtc_state *
1252 ast_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1253 {
1254 	struct ast_crtc_state *new_ast_state, *ast_state;
1255 	struct drm_device *dev = crtc->dev;
1256 
1257 	if (drm_WARN_ON(dev, !crtc->state))
1258 		return NULL;
1259 
1260 	new_ast_state = kmalloc(sizeof(*new_ast_state), GFP_KERNEL);
1261 	if (!new_ast_state)
1262 		return NULL;
1263 	__drm_atomic_helper_crtc_duplicate_state(crtc, &new_ast_state->base);
1264 
1265 	ast_state = to_ast_crtc_state(crtc->state);
1266 
1267 	new_ast_state->format = ast_state->format;
1268 	memcpy(&new_ast_state->vbios_mode_info, &ast_state->vbios_mode_info,
1269 	       sizeof(new_ast_state->vbios_mode_info));
1270 
1271 	return &new_ast_state->base;
1272 }
1273 
1274 static void ast_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1275 					  struct drm_crtc_state *state)
1276 {
1277 	struct ast_crtc_state *ast_state = to_ast_crtc_state(state);
1278 
1279 	__drm_atomic_helper_crtc_destroy_state(&ast_state->base);
1280 	kfree(ast_state);
1281 }
1282 
1283 static const struct drm_crtc_funcs ast_crtc_funcs = {
1284 	.reset = ast_crtc_reset,
1285 	.destroy = drm_crtc_cleanup,
1286 	.set_config = drm_atomic_helper_set_config,
1287 	.page_flip = drm_atomic_helper_page_flip,
1288 	.atomic_duplicate_state = ast_crtc_atomic_duplicate_state,
1289 	.atomic_destroy_state = ast_crtc_atomic_destroy_state,
1290 };
1291 
1292 static int ast_crtc_init(struct drm_device *dev)
1293 {
1294 	struct ast_device *ast = to_ast_device(dev);
1295 	struct drm_crtc *crtc = &ast->crtc;
1296 	int ret;
1297 
1298 	ret = drm_crtc_init_with_planes(dev, crtc, &ast->primary_plane.base,
1299 					&ast->cursor_plane.base, &ast_crtc_funcs,
1300 					NULL);
1301 	if (ret)
1302 		return ret;
1303 
1304 	drm_mode_crtc_set_gamma_size(crtc, AST_LUT_SIZE);
1305 	drm_crtc_enable_color_mgmt(crtc, 0, false, AST_LUT_SIZE);
1306 
1307 	drm_crtc_helper_add(crtc, &ast_crtc_helper_funcs);
1308 
1309 	return 0;
1310 }
1311 
1312 /*
1313  * VGA Encoder
1314  */
1315 
1316 static const struct drm_encoder_funcs ast_vga_encoder_funcs = {
1317 	.destroy = drm_encoder_cleanup,
1318 };
1319 
1320 /*
1321  * VGA Connector
1322  */
1323 
1324 static const struct drm_connector_helper_funcs ast_vga_connector_helper_funcs = {
1325 	.get_modes = drm_connector_helper_get_modes,
1326 	.detect_ctx = drm_connector_helper_detect_from_ddc,
1327 };
1328 
1329 static const struct drm_connector_funcs ast_vga_connector_funcs = {
1330 	.reset = drm_atomic_helper_connector_reset,
1331 	.fill_modes = drm_helper_probe_single_connector_modes,
1332 	.destroy = drm_connector_cleanup,
1333 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1334 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1335 };
1336 
1337 static int ast_vga_connector_init(struct drm_device *dev, struct drm_connector *connector)
1338 {
1339 	struct ast_device *ast = to_ast_device(dev);
1340 	struct i2c_adapter *ddc;
1341 	int ret;
1342 
1343 	ddc = ast_ddc_create(ast);
1344 	if (IS_ERR(ddc)) {
1345 		ret = PTR_ERR(ddc);
1346 		drm_err(dev, "failed to add DDC bus for connector; ret=%d\n", ret);
1347 		return ret;
1348 	}
1349 
1350 	ret = drm_connector_init_with_ddc(dev, connector, &ast_vga_connector_funcs,
1351 					  DRM_MODE_CONNECTOR_VGA, ddc);
1352 	if (ret)
1353 		return ret;
1354 
1355 	drm_connector_helper_add(connector, &ast_vga_connector_helper_funcs);
1356 
1357 	connector->interlace_allowed = 0;
1358 	connector->doublescan_allowed = 0;
1359 
1360 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
1361 
1362 	return 0;
1363 }
1364 
1365 static int ast_vga_output_init(struct ast_device *ast)
1366 {
1367 	struct drm_device *dev = &ast->base;
1368 	struct drm_crtc *crtc = &ast->crtc;
1369 	struct drm_encoder *encoder = &ast->output.vga.encoder;
1370 	struct drm_connector *connector = &ast->output.vga.connector;
1371 	int ret;
1372 
1373 	ret = drm_encoder_init(dev, encoder, &ast_vga_encoder_funcs,
1374 			       DRM_MODE_ENCODER_DAC, NULL);
1375 	if (ret)
1376 		return ret;
1377 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1378 
1379 	ret = ast_vga_connector_init(dev, connector);
1380 	if (ret)
1381 		return ret;
1382 
1383 	ret = drm_connector_attach_encoder(connector, encoder);
1384 	if (ret)
1385 		return ret;
1386 
1387 	return 0;
1388 }
1389 
1390 /*
1391  * SIL164 Encoder
1392  */
1393 
1394 static const struct drm_encoder_funcs ast_sil164_encoder_funcs = {
1395 	.destroy = drm_encoder_cleanup,
1396 };
1397 
1398 /*
1399  * SIL164 Connector
1400  */
1401 
1402 static const struct drm_connector_helper_funcs ast_sil164_connector_helper_funcs = {
1403 	.get_modes = drm_connector_helper_get_modes,
1404 	.detect_ctx = drm_connector_helper_detect_from_ddc,
1405 };
1406 
1407 static const struct drm_connector_funcs ast_sil164_connector_funcs = {
1408 	.reset = drm_atomic_helper_connector_reset,
1409 	.fill_modes = drm_helper_probe_single_connector_modes,
1410 	.destroy = drm_connector_cleanup,
1411 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1412 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1413 };
1414 
1415 static int ast_sil164_connector_init(struct drm_device *dev, struct drm_connector *connector)
1416 {
1417 	struct ast_device *ast = to_ast_device(dev);
1418 	struct i2c_adapter *ddc;
1419 	int ret;
1420 
1421 	ddc = ast_ddc_create(ast);
1422 	if (IS_ERR(ddc)) {
1423 		ret = PTR_ERR(ddc);
1424 		drm_err(dev, "failed to add DDC bus for connector; ret=%d\n", ret);
1425 		return ret;
1426 	}
1427 
1428 	ret = drm_connector_init_with_ddc(dev, connector, &ast_sil164_connector_funcs,
1429 					  DRM_MODE_CONNECTOR_DVII, ddc);
1430 	if (ret)
1431 		return ret;
1432 
1433 	drm_connector_helper_add(connector, &ast_sil164_connector_helper_funcs);
1434 
1435 	connector->interlace_allowed = 0;
1436 	connector->doublescan_allowed = 0;
1437 
1438 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
1439 
1440 	return 0;
1441 }
1442 
1443 static int ast_sil164_output_init(struct ast_device *ast)
1444 {
1445 	struct drm_device *dev = &ast->base;
1446 	struct drm_crtc *crtc = &ast->crtc;
1447 	struct drm_encoder *encoder = &ast->output.sil164.encoder;
1448 	struct drm_connector *connector = &ast->output.sil164.connector;
1449 	int ret;
1450 
1451 	ret = drm_encoder_init(dev, encoder, &ast_sil164_encoder_funcs,
1452 			       DRM_MODE_ENCODER_TMDS, NULL);
1453 	if (ret)
1454 		return ret;
1455 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1456 
1457 	ret = ast_sil164_connector_init(dev, connector);
1458 	if (ret)
1459 		return ret;
1460 
1461 	ret = drm_connector_attach_encoder(connector, encoder);
1462 	if (ret)
1463 		return ret;
1464 
1465 	return 0;
1466 }
1467 
1468 /*
1469  * DP501 Encoder
1470  */
1471 
1472 static const struct drm_encoder_funcs ast_dp501_encoder_funcs = {
1473 	.destroy = drm_encoder_cleanup,
1474 };
1475 
1476 static void ast_dp501_encoder_helper_atomic_enable(struct drm_encoder *encoder,
1477 						   struct drm_atomic_state *state)
1478 {
1479 	struct drm_device *dev = encoder->dev;
1480 
1481 	ast_set_dp501_video_output(dev, 1);
1482 }
1483 
1484 static void ast_dp501_encoder_helper_atomic_disable(struct drm_encoder *encoder,
1485 						    struct drm_atomic_state *state)
1486 {
1487 	struct drm_device *dev = encoder->dev;
1488 
1489 	ast_set_dp501_video_output(dev, 0);
1490 }
1491 
1492 static const struct drm_encoder_helper_funcs ast_dp501_encoder_helper_funcs = {
1493 	.atomic_enable = ast_dp501_encoder_helper_atomic_enable,
1494 	.atomic_disable = ast_dp501_encoder_helper_atomic_disable,
1495 };
1496 
1497 /*
1498  * DP501 Connector
1499  */
1500 
1501 static int ast_dp501_connector_helper_get_modes(struct drm_connector *connector)
1502 {
1503 	void *edid;
1504 	bool succ;
1505 	int count;
1506 
1507 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1508 	if (!edid)
1509 		goto err_drm_connector_update_edid_property;
1510 
1511 	succ = ast_dp501_read_edid(connector->dev, edid);
1512 	if (!succ)
1513 		goto err_kfree;
1514 
1515 	drm_connector_update_edid_property(connector, edid);
1516 	count = drm_add_edid_modes(connector, edid);
1517 	kfree(edid);
1518 
1519 	return count;
1520 
1521 err_kfree:
1522 	kfree(edid);
1523 err_drm_connector_update_edid_property:
1524 	drm_connector_update_edid_property(connector, NULL);
1525 	return 0;
1526 }
1527 
1528 static int ast_dp501_connector_helper_detect_ctx(struct drm_connector *connector,
1529 						 struct drm_modeset_acquire_ctx *ctx,
1530 						 bool force)
1531 {
1532 	struct ast_device *ast = to_ast_device(connector->dev);
1533 
1534 	if (ast_dp501_is_connected(ast))
1535 		return connector_status_connected;
1536 	return connector_status_disconnected;
1537 }
1538 
1539 static const struct drm_connector_helper_funcs ast_dp501_connector_helper_funcs = {
1540 	.get_modes = ast_dp501_connector_helper_get_modes,
1541 	.detect_ctx = ast_dp501_connector_helper_detect_ctx,
1542 };
1543 
1544 static const struct drm_connector_funcs ast_dp501_connector_funcs = {
1545 	.reset = drm_atomic_helper_connector_reset,
1546 	.fill_modes = drm_helper_probe_single_connector_modes,
1547 	.destroy = drm_connector_cleanup,
1548 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1549 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1550 };
1551 
1552 static int ast_dp501_connector_init(struct drm_device *dev, struct drm_connector *connector)
1553 {
1554 	int ret;
1555 
1556 	ret = drm_connector_init(dev, connector, &ast_dp501_connector_funcs,
1557 				 DRM_MODE_CONNECTOR_DisplayPort);
1558 	if (ret)
1559 		return ret;
1560 
1561 	drm_connector_helper_add(connector, &ast_dp501_connector_helper_funcs);
1562 
1563 	connector->interlace_allowed = 0;
1564 	connector->doublescan_allowed = 0;
1565 
1566 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
1567 
1568 	return 0;
1569 }
1570 
1571 static int ast_dp501_output_init(struct ast_device *ast)
1572 {
1573 	struct drm_device *dev = &ast->base;
1574 	struct drm_crtc *crtc = &ast->crtc;
1575 	struct drm_encoder *encoder = &ast->output.dp501.encoder;
1576 	struct drm_connector *connector = &ast->output.dp501.connector;
1577 	int ret;
1578 
1579 	ret = drm_encoder_init(dev, encoder, &ast_dp501_encoder_funcs,
1580 			       DRM_MODE_ENCODER_TMDS, NULL);
1581 	if (ret)
1582 		return ret;
1583 	drm_encoder_helper_add(encoder, &ast_dp501_encoder_helper_funcs);
1584 
1585 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1586 
1587 	ret = ast_dp501_connector_init(dev, connector);
1588 	if (ret)
1589 		return ret;
1590 
1591 	ret = drm_connector_attach_encoder(connector, encoder);
1592 	if (ret)
1593 		return ret;
1594 
1595 	return 0;
1596 }
1597 
1598 /*
1599  * ASPEED Display-Port Encoder
1600  */
1601 
1602 static const struct drm_encoder_funcs ast_astdp_encoder_funcs = {
1603 	.destroy = drm_encoder_cleanup,
1604 };
1605 
1606 static void ast_astdp_encoder_helper_atomic_mode_set(struct drm_encoder *encoder,
1607 						     struct drm_crtc_state *crtc_state,
1608 						     struct drm_connector_state *conn_state)
1609 {
1610 	struct drm_crtc *crtc = crtc_state->crtc;
1611 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
1612 	struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
1613 
1614 	ast_dp_set_mode(crtc, vbios_mode_info);
1615 }
1616 
1617 static void ast_astdp_encoder_helper_atomic_enable(struct drm_encoder *encoder,
1618 						   struct drm_atomic_state *state)
1619 {
1620 	struct drm_device *dev = encoder->dev;
1621 	struct ast_device *ast = to_ast_device(dev);
1622 
1623 	ast_dp_power_on_off(dev, AST_DP_POWER_ON);
1624 	ast_wait_for_vretrace(ast);
1625 	ast_dp_set_on_off(dev, 1);
1626 }
1627 
1628 static void ast_astdp_encoder_helper_atomic_disable(struct drm_encoder *encoder,
1629 						    struct drm_atomic_state *state)
1630 {
1631 	struct drm_device *dev = encoder->dev;
1632 
1633 	ast_dp_set_on_off(dev, 0);
1634 	ast_dp_power_on_off(dev, AST_DP_POWER_OFF);
1635 }
1636 
1637 static const struct drm_encoder_helper_funcs ast_astdp_encoder_helper_funcs = {
1638 	.atomic_mode_set = ast_astdp_encoder_helper_atomic_mode_set,
1639 	.atomic_enable = ast_astdp_encoder_helper_atomic_enable,
1640 	.atomic_disable = ast_astdp_encoder_helper_atomic_disable,
1641 };
1642 
1643 /*
1644  * ASPEED Display-Port Connector
1645  */
1646 
1647 static int ast_astdp_connector_helper_get_modes(struct drm_connector *connector)
1648 {
1649 	void *edid;
1650 	struct drm_device *dev = connector->dev;
1651 	struct ast_device *ast = to_ast_device(dev);
1652 
1653 	int succ;
1654 	int count;
1655 
1656 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1657 	if (!edid)
1658 		goto err_drm_connector_update_edid_property;
1659 
1660 	/*
1661 	 * Protect access to I/O registers from concurrent modesetting
1662 	 * by acquiring the I/O-register lock.
1663 	 */
1664 	mutex_lock(&ast->modeset_lock);
1665 
1666 	succ = ast_astdp_read_edid(connector->dev, edid);
1667 	if (succ < 0)
1668 		goto err_mutex_unlock;
1669 
1670 	mutex_unlock(&ast->modeset_lock);
1671 
1672 	drm_connector_update_edid_property(connector, edid);
1673 	count = drm_add_edid_modes(connector, edid);
1674 	kfree(edid);
1675 
1676 	return count;
1677 
1678 err_mutex_unlock:
1679 	mutex_unlock(&ast->modeset_lock);
1680 	kfree(edid);
1681 err_drm_connector_update_edid_property:
1682 	drm_connector_update_edid_property(connector, NULL);
1683 	return 0;
1684 }
1685 
1686 static int ast_astdp_connector_helper_detect_ctx(struct drm_connector *connector,
1687 						 struct drm_modeset_acquire_ctx *ctx,
1688 						 bool force)
1689 {
1690 	struct ast_device *ast = to_ast_device(connector->dev);
1691 
1692 	if (ast_astdp_is_connected(ast))
1693 		return connector_status_connected;
1694 	return connector_status_disconnected;
1695 }
1696 
1697 static const struct drm_connector_helper_funcs ast_astdp_connector_helper_funcs = {
1698 	.get_modes = ast_astdp_connector_helper_get_modes,
1699 	.detect_ctx = ast_astdp_connector_helper_detect_ctx,
1700 };
1701 
1702 static const struct drm_connector_funcs ast_astdp_connector_funcs = {
1703 	.reset = drm_atomic_helper_connector_reset,
1704 	.fill_modes = drm_helper_probe_single_connector_modes,
1705 	.destroy = drm_connector_cleanup,
1706 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1707 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1708 };
1709 
1710 static int ast_astdp_connector_init(struct drm_device *dev, struct drm_connector *connector)
1711 {
1712 	int ret;
1713 
1714 	ret = drm_connector_init(dev, connector, &ast_astdp_connector_funcs,
1715 				 DRM_MODE_CONNECTOR_DisplayPort);
1716 	if (ret)
1717 		return ret;
1718 
1719 	drm_connector_helper_add(connector, &ast_astdp_connector_helper_funcs);
1720 
1721 	connector->interlace_allowed = 0;
1722 	connector->doublescan_allowed = 0;
1723 
1724 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
1725 
1726 	return 0;
1727 }
1728 
1729 static int ast_astdp_output_init(struct ast_device *ast)
1730 {
1731 	struct drm_device *dev = &ast->base;
1732 	struct drm_crtc *crtc = &ast->crtc;
1733 	struct drm_encoder *encoder = &ast->output.astdp.encoder;
1734 	struct drm_connector *connector = &ast->output.astdp.connector;
1735 	int ret;
1736 
1737 	ret = drm_encoder_init(dev, encoder, &ast_astdp_encoder_funcs,
1738 			       DRM_MODE_ENCODER_TMDS, NULL);
1739 	if (ret)
1740 		return ret;
1741 	drm_encoder_helper_add(encoder, &ast_astdp_encoder_helper_funcs);
1742 
1743 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1744 
1745 	ret = ast_astdp_connector_init(dev, connector);
1746 	if (ret)
1747 		return ret;
1748 
1749 	ret = drm_connector_attach_encoder(connector, encoder);
1750 	if (ret)
1751 		return ret;
1752 
1753 	return 0;
1754 }
1755 
1756 /*
1757  * BMC virtual Connector
1758  */
1759 
1760 static const struct drm_encoder_funcs ast_bmc_encoder_funcs = {
1761 	.destroy = drm_encoder_cleanup,
1762 };
1763 
1764 static int ast_bmc_connector_helper_detect_ctx(struct drm_connector *connector,
1765 					       struct drm_modeset_acquire_ctx *ctx,
1766 					       bool force)
1767 {
1768 	struct ast_bmc_connector *bmc_connector = to_ast_bmc_connector(connector);
1769 	struct drm_connector *physical_connector = bmc_connector->physical_connector;
1770 
1771 	/*
1772 	 * Most user-space compositors cannot handle more than one connected
1773 	 * connector per CRTC. Hence, we only mark the BMC as connected if the
1774 	 * physical connector is disconnected. If the physical connector's status
1775 	 * is connected or unknown, the BMC remains disconnected. This has no
1776 	 * effect on the output of the BMC.
1777 	 *
1778 	 * FIXME: Remove this logic once user-space compositors can handle more
1779 	 *        than one connector per CRTC. The BMC should always be connected.
1780 	 */
1781 
1782 	if (physical_connector && physical_connector->status == connector_status_disconnected)
1783 		return connector_status_connected;
1784 
1785 	return connector_status_disconnected;
1786 }
1787 
1788 static int ast_bmc_connector_helper_get_modes(struct drm_connector *connector)
1789 {
1790 	return drm_add_modes_noedid(connector, 4096, 4096);
1791 }
1792 
1793 static const struct drm_connector_helper_funcs ast_bmc_connector_helper_funcs = {
1794 	.get_modes = ast_bmc_connector_helper_get_modes,
1795 	.detect_ctx = ast_bmc_connector_helper_detect_ctx,
1796 };
1797 
1798 static const struct drm_connector_funcs ast_bmc_connector_funcs = {
1799 	.reset = drm_atomic_helper_connector_reset,
1800 	.fill_modes = drm_helper_probe_single_connector_modes,
1801 	.destroy = drm_connector_cleanup,
1802 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1803 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1804 };
1805 
1806 static int ast_bmc_connector_init(struct drm_device *dev,
1807 				  struct ast_bmc_connector *bmc_connector,
1808 				  struct drm_connector *physical_connector)
1809 {
1810 	struct drm_connector *connector = &bmc_connector->base;
1811 	int ret;
1812 
1813 	ret = drm_connector_init(dev, connector, &ast_bmc_connector_funcs,
1814 				 DRM_MODE_CONNECTOR_VIRTUAL);
1815 	if (ret)
1816 		return ret;
1817 
1818 	drm_connector_helper_add(connector, &ast_bmc_connector_helper_funcs);
1819 
1820 	bmc_connector->physical_connector = physical_connector;
1821 
1822 	return 0;
1823 }
1824 
1825 static int ast_bmc_output_init(struct ast_device *ast,
1826 			       struct drm_connector *physical_connector)
1827 {
1828 	struct drm_device *dev = &ast->base;
1829 	struct drm_crtc *crtc = &ast->crtc;
1830 	struct drm_encoder *encoder = &ast->output.bmc.encoder;
1831 	struct ast_bmc_connector *bmc_connector = &ast->output.bmc.bmc_connector;
1832 	struct drm_connector *connector = &bmc_connector->base;
1833 	int ret;
1834 
1835 	ret = drm_encoder_init(dev, encoder,
1836 			       &ast_bmc_encoder_funcs,
1837 			       DRM_MODE_ENCODER_VIRTUAL, "ast_bmc");
1838 	if (ret)
1839 		return ret;
1840 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1841 
1842 	ret = ast_bmc_connector_init(dev, bmc_connector, physical_connector);
1843 	if (ret)
1844 		return ret;
1845 
1846 	ret = drm_connector_attach_encoder(connector, encoder);
1847 	if (ret)
1848 		return ret;
1849 
1850 	return 0;
1851 }
1852 
1853 /*
1854  * Mode config
1855  */
1856 
1857 static void ast_mode_config_helper_atomic_commit_tail(struct drm_atomic_state *state)
1858 {
1859 	struct ast_device *ast = to_ast_device(state->dev);
1860 
1861 	/*
1862 	 * Concurrent operations could possibly trigger a call to
1863 	 * drm_connector_helper_funcs.get_modes by trying to read the
1864 	 * display modes. Protect access to I/O registers by acquiring
1865 	 * the I/O-register lock. Released in atomic_flush().
1866 	 */
1867 	mutex_lock(&ast->modeset_lock);
1868 	drm_atomic_helper_commit_tail(state);
1869 	mutex_unlock(&ast->modeset_lock);
1870 }
1871 
1872 static const struct drm_mode_config_helper_funcs ast_mode_config_helper_funcs = {
1873 	.atomic_commit_tail = ast_mode_config_helper_atomic_commit_tail,
1874 };
1875 
1876 static enum drm_mode_status ast_mode_config_mode_valid(struct drm_device *dev,
1877 						       const struct drm_display_mode *mode)
1878 {
1879 	static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1880 	struct ast_device *ast = to_ast_device(dev);
1881 	unsigned long fbsize, fbpages, max_fbpages;
1882 
1883 	max_fbpages = (ast->vram_fb_available) >> PAGE_SHIFT;
1884 
1885 	fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1886 	fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1887 
1888 	if (fbpages > max_fbpages)
1889 		return MODE_MEM;
1890 
1891 	return MODE_OK;
1892 }
1893 
1894 static const struct drm_mode_config_funcs ast_mode_config_funcs = {
1895 	.fb_create = drm_gem_fb_create_with_dirty,
1896 	.mode_valid = ast_mode_config_mode_valid,
1897 	.atomic_check = drm_atomic_helper_check,
1898 	.atomic_commit = drm_atomic_helper_commit,
1899 };
1900 
1901 int ast_mode_config_init(struct ast_device *ast)
1902 {
1903 	struct drm_device *dev = &ast->base;
1904 	struct drm_connector *physical_connector = NULL;
1905 	int ret;
1906 
1907 	ret = drmm_mutex_init(dev, &ast->modeset_lock);
1908 	if (ret)
1909 		return ret;
1910 
1911 	ret = drmm_mode_config_init(dev);
1912 	if (ret)
1913 		return ret;
1914 
1915 	dev->mode_config.funcs = &ast_mode_config_funcs;
1916 	dev->mode_config.min_width = 0;
1917 	dev->mode_config.min_height = 0;
1918 	dev->mode_config.preferred_depth = 24;
1919 
1920 	if (ast->chip == AST2100 || // GEN2, but not AST1100 (?)
1921 	    ast->chip == AST2200 || // GEN3, but not AST2150 (?)
1922 	    IS_AST_GEN7(ast) ||
1923 	    IS_AST_GEN6(ast) ||
1924 	    IS_AST_GEN5(ast) ||
1925 	    IS_AST_GEN4(ast)) {
1926 		dev->mode_config.max_width = 1920;
1927 		dev->mode_config.max_height = 2048;
1928 	} else {
1929 		dev->mode_config.max_width = 1600;
1930 		dev->mode_config.max_height = 1200;
1931 	}
1932 
1933 	dev->mode_config.helper_private = &ast_mode_config_helper_funcs;
1934 
1935 	ret = ast_primary_plane_init(ast);
1936 	if (ret)
1937 		return ret;
1938 
1939 	ret = ast_cursor_plane_init(ast);
1940 	if (ret)
1941 		return ret;
1942 
1943 	ast_crtc_init(dev);
1944 
1945 	if (ast->tx_chip_types & AST_TX_NONE_BIT) {
1946 		ret = ast_vga_output_init(ast);
1947 		if (ret)
1948 			return ret;
1949 		physical_connector = &ast->output.vga.connector;
1950 	}
1951 	if (ast->tx_chip_types & AST_TX_SIL164_BIT) {
1952 		ret = ast_sil164_output_init(ast);
1953 		if (ret)
1954 			return ret;
1955 		physical_connector = &ast->output.sil164.connector;
1956 	}
1957 	if (ast->tx_chip_types & AST_TX_DP501_BIT) {
1958 		ret = ast_dp501_output_init(ast);
1959 		if (ret)
1960 			return ret;
1961 		physical_connector = &ast->output.dp501.connector;
1962 	}
1963 	if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
1964 		ret = ast_astdp_output_init(ast);
1965 		if (ret)
1966 			return ret;
1967 		physical_connector = &ast->output.astdp.connector;
1968 	}
1969 	ret = ast_bmc_output_init(ast, physical_connector);
1970 	if (ret)
1971 		return ret;
1972 
1973 	drm_mode_config_reset(dev);
1974 
1975 	ret = drmm_kms_helper_poll_init(dev);
1976 	if (ret)
1977 		return ret;
1978 
1979 	return 0;
1980 }
1981