xref: /openbmc/linux/drivers/gpu/drm/ast/ast_mode.c (revision 963a2ba2)
1312fec14SDave Airlie /*
2312fec14SDave Airlie  * Copyright 2012 Red Hat Inc.
3312fec14SDave Airlie  * Parts based on xf86-video-ast
4312fec14SDave Airlie  * Copyright (c) 2005 ASPEED Technology Inc.
5312fec14SDave Airlie  *
6312fec14SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
7312fec14SDave Airlie  * copy of this software and associated documentation files (the
8312fec14SDave Airlie  * "Software"), to deal in the Software without restriction, including
9312fec14SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
10312fec14SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
11312fec14SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
12312fec14SDave Airlie  * the following conditions:
13312fec14SDave Airlie  *
14312fec14SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15312fec14SDave Airlie  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16312fec14SDave Airlie  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17312fec14SDave Airlie  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18312fec14SDave Airlie  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19312fec14SDave Airlie  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20312fec14SDave Airlie  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21312fec14SDave Airlie  *
22312fec14SDave Airlie  * The above copyright notice and this permission notice (including the
23312fec14SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
24312fec14SDave Airlie  * of the Software.
25312fec14SDave Airlie  *
26312fec14SDave Airlie  */
27312fec14SDave Airlie /*
28312fec14SDave Airlie  * Authors: Dave Airlie <airlied@redhat.com>
29312fec14SDave Airlie  */
30fbbbd160SSam Ravnborg 
31312fec14SDave Airlie #include <linux/export.h>
32fbbbd160SSam Ravnborg #include <linux/pci.h>
33fbbbd160SSam Ravnborg 
34ae46a57dSThomas Zimmermann #include <drm/drm_atomic.h>
35a6ff807bSThomas Zimmermann #include <drm/drm_atomic_helper.h>
36a6ff807bSThomas Zimmermann #include <drm/drm_atomic_state_helper.h>
37760285e7SDavid Howells #include <drm/drm_crtc.h>
38760285e7SDavid Howells #include <drm/drm_crtc_helper.h>
39255490f9SVille Syrjälä #include <drm/drm_edid.h>
40fbbbd160SSam Ravnborg #include <drm/drm_fourcc.h>
414d36cf07SThomas Zimmermann #include <drm/drm_gem_atomic_helper.h>
42e6949ff3SThomas Zimmermann #include <drm/drm_gem_framebuffer_helper.h>
43fbbbd160SSam Ravnborg #include <drm/drm_gem_vram_helper.h>
443ab26eddSThomas Zimmermann #include <drm/drm_managed.h>
45fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
464220fdf0SThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
47312fec14SDave Airlie 
48fbbbd160SSam Ravnborg #include "ast_drv.h"
49312fec14SDave Airlie #include "ast_tables.h"
50312fec14SDave Airlie 
51ce7fcf70SJocelyn Falempe #define AST_LUT_SIZE 256
52ce7fcf70SJocelyn Falempe 
53312fec14SDave Airlie static inline void ast_load_palette_index(struct ast_private *ast,
54312fec14SDave Airlie 				     u8 index, u8 red, u8 green,
55312fec14SDave Airlie 				     u8 blue)
56312fec14SDave Airlie {
57312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_DAC_INDEX_WRITE, index);
58312fec14SDave Airlie 	ast_io_read8(ast, AST_IO_SEQ_PORT);
59312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_DAC_DATA, red);
60312fec14SDave Airlie 	ast_io_read8(ast, AST_IO_SEQ_PORT);
61312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_DAC_DATA, green);
62312fec14SDave Airlie 	ast_io_read8(ast, AST_IO_SEQ_PORT);
63312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_DAC_DATA, blue);
64312fec14SDave Airlie 	ast_io_read8(ast, AST_IO_SEQ_PORT);
65312fec14SDave Airlie }
66312fec14SDave Airlie 
67ce7fcf70SJocelyn Falempe static void ast_crtc_set_gamma_linear(struct ast_private *ast,
68ce7fcf70SJocelyn Falempe 				      const struct drm_format_info *format)
69312fec14SDave Airlie {
70312fec14SDave Airlie 	int i;
71312fec14SDave Airlie 
72ce7fcf70SJocelyn Falempe 	switch (format->format) {
73ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_C8: /* In this case, gamma table is used as color palette */
74ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_RGB565:
75ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_XRGB8888:
76ce7fcf70SJocelyn Falempe 		for (i = 0; i < AST_LUT_SIZE; i++)
77ce7fcf70SJocelyn Falempe 			ast_load_palette_index(ast, i, i, i, i);
78ce7fcf70SJocelyn Falempe 		break;
79ce7fcf70SJocelyn Falempe 	default:
80ce7fcf70SJocelyn Falempe 		drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n",
81ce7fcf70SJocelyn Falempe 			      &format->format);
82ce7fcf70SJocelyn Falempe 		break;
83ce7fcf70SJocelyn Falempe 	}
84ce7fcf70SJocelyn Falempe }
85312fec14SDave Airlie 
86ce7fcf70SJocelyn Falempe static void ast_crtc_set_gamma(struct ast_private *ast,
87ce7fcf70SJocelyn Falempe 			       const struct drm_format_info *format,
88ce7fcf70SJocelyn Falempe 			       struct drm_color_lut *lut)
89ce7fcf70SJocelyn Falempe {
90ce7fcf70SJocelyn Falempe 	int i;
913bffd962SPeter Rosin 
92ce7fcf70SJocelyn Falempe 	switch (format->format) {
93ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_C8: /* In this case, gamma table is used as color palette */
94ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_RGB565:
95ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_XRGB8888:
96ce7fcf70SJocelyn Falempe 		for (i = 0; i < AST_LUT_SIZE; i++)
97ce7fcf70SJocelyn Falempe 			ast_load_palette_index(ast, i,
98ce7fcf70SJocelyn Falempe 					       lut[i].red >> 8,
99ce7fcf70SJocelyn Falempe 					       lut[i].green >> 8,
100ce7fcf70SJocelyn Falempe 					       lut[i].blue >> 8);
101ce7fcf70SJocelyn Falempe 		break;
102ce7fcf70SJocelyn Falempe 	default:
103ce7fcf70SJocelyn Falempe 		drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n",
104ce7fcf70SJocelyn Falempe 			      &format->format);
105ce7fcf70SJocelyn Falempe 		break;
106ce7fcf70SJocelyn Falempe 	}
107312fec14SDave Airlie }
108312fec14SDave Airlie 
109ae37025dSThomas Zimmermann static bool ast_get_vbios_mode_info(const struct drm_format_info *format,
110259d14a7SThomas Zimmermann 				    const struct drm_display_mode *mode,
111312fec14SDave Airlie 				    struct drm_display_mode *adjusted_mode,
112312fec14SDave Airlie 				    struct ast_vbios_mode_info *vbios_mode)
113312fec14SDave Airlie {
114259d14a7SThomas Zimmermann 	u32 refresh_rate_index = 0, refresh_rate;
11522acdbb1SBenjamin Herrenschmidt 	const struct ast_vbios_enhtable *best = NULL;
116312fec14SDave Airlie 	u32 hborder, vborder;
11794d12b13SY.C. Chen 	bool check_sync;
118312fec14SDave Airlie 
119ae37025dSThomas Zimmermann 	switch (format->cpp[0] * 8) {
120312fec14SDave Airlie 	case 8:
121312fec14SDave Airlie 		vbios_mode->std_table = &vbios_stdtable[VGAModeIndex];
122312fec14SDave Airlie 		break;
123312fec14SDave Airlie 	case 16:
124312fec14SDave Airlie 		vbios_mode->std_table = &vbios_stdtable[HiCModeIndex];
125312fec14SDave Airlie 		break;
126312fec14SDave Airlie 	case 24:
127312fec14SDave Airlie 	case 32:
128312fec14SDave Airlie 		vbios_mode->std_table = &vbios_stdtable[TrueCModeIndex];
129312fec14SDave Airlie 		break;
130312fec14SDave Airlie 	default:
131312fec14SDave Airlie 		return false;
132312fec14SDave Airlie 	}
133312fec14SDave Airlie 
134259d14a7SThomas Zimmermann 	switch (mode->crtc_hdisplay) {
135312fec14SDave Airlie 	case 640:
136312fec14SDave Airlie 		vbios_mode->enh_table = &res_640x480[refresh_rate_index];
137312fec14SDave Airlie 		break;
138312fec14SDave Airlie 	case 800:
139312fec14SDave Airlie 		vbios_mode->enh_table = &res_800x600[refresh_rate_index];
140312fec14SDave Airlie 		break;
141312fec14SDave Airlie 	case 1024:
142312fec14SDave Airlie 		vbios_mode->enh_table = &res_1024x768[refresh_rate_index];
143312fec14SDave Airlie 		break;
14471dee036SJammy Huang 	case 1152:
14571dee036SJammy Huang 		vbios_mode->enh_table = &res_1152x864[refresh_rate_index];
14671dee036SJammy Huang 		break;
147312fec14SDave Airlie 	case 1280:
148259d14a7SThomas Zimmermann 		if (mode->crtc_vdisplay == 800)
149312fec14SDave Airlie 			vbios_mode->enh_table = &res_1280x800[refresh_rate_index];
150312fec14SDave Airlie 		else
151312fec14SDave Airlie 			vbios_mode->enh_table = &res_1280x1024[refresh_rate_index];
152312fec14SDave Airlie 		break;
153f1f62f2cSDave Airlie 	case 1360:
154f1f62f2cSDave Airlie 		vbios_mode->enh_table = &res_1360x768[refresh_rate_index];
155f1f62f2cSDave Airlie 		break;
156312fec14SDave Airlie 	case 1440:
157312fec14SDave Airlie 		vbios_mode->enh_table = &res_1440x900[refresh_rate_index];
158312fec14SDave Airlie 		break;
159312fec14SDave Airlie 	case 1600:
160259d14a7SThomas Zimmermann 		if (mode->crtc_vdisplay == 900)
161f1f62f2cSDave Airlie 			vbios_mode->enh_table = &res_1600x900[refresh_rate_index];
162f1f62f2cSDave Airlie 		else
163312fec14SDave Airlie 			vbios_mode->enh_table = &res_1600x1200[refresh_rate_index];
164312fec14SDave Airlie 		break;
165312fec14SDave Airlie 	case 1680:
166312fec14SDave Airlie 		vbios_mode->enh_table = &res_1680x1050[refresh_rate_index];
167312fec14SDave Airlie 		break;
168312fec14SDave Airlie 	case 1920:
169259d14a7SThomas Zimmermann 		if (mode->crtc_vdisplay == 1080)
170312fec14SDave Airlie 			vbios_mode->enh_table = &res_1920x1080[refresh_rate_index];
171312fec14SDave Airlie 		else
172312fec14SDave Airlie 			vbios_mode->enh_table = &res_1920x1200[refresh_rate_index];
173312fec14SDave Airlie 		break;
174312fec14SDave Airlie 	default:
175312fec14SDave Airlie 		return false;
176312fec14SDave Airlie 	}
177312fec14SDave Airlie 
178312fec14SDave Airlie 	refresh_rate = drm_mode_vrefresh(mode);
17994d12b13SY.C. Chen 	check_sync = vbios_mode->enh_table->flags & WideScreenMode;
180259d14a7SThomas Zimmermann 
181259d14a7SThomas Zimmermann 	while (1) {
18222acdbb1SBenjamin Herrenschmidt 		const struct ast_vbios_enhtable *loop = vbios_mode->enh_table;
18394d12b13SY.C. Chen 
18494d12b13SY.C. Chen 		while (loop->refresh_rate != 0xff) {
18594d12b13SY.C. Chen 			if ((check_sync) &&
18694d12b13SY.C. Chen 			    (((mode->flags & DRM_MODE_FLAG_NVSYNC)  &&
18794d12b13SY.C. Chen 			      (loop->flags & PVSync))  ||
18894d12b13SY.C. Chen 			     ((mode->flags & DRM_MODE_FLAG_PVSYNC)  &&
18994d12b13SY.C. Chen 			      (loop->flags & NVSync))  ||
19094d12b13SY.C. Chen 			     ((mode->flags & DRM_MODE_FLAG_NHSYNC)  &&
19194d12b13SY.C. Chen 			      (loop->flags & PHSync))  ||
19294d12b13SY.C. Chen 			     ((mode->flags & DRM_MODE_FLAG_PHSYNC)  &&
19394d12b13SY.C. Chen 			      (loop->flags & NHSync)))) {
19494d12b13SY.C. Chen 				loop++;
19594d12b13SY.C. Chen 				continue;
19694d12b13SY.C. Chen 			}
19794d12b13SY.C. Chen 			if (loop->refresh_rate <= refresh_rate
19894d12b13SY.C. Chen 			    && (!best || loop->refresh_rate > best->refresh_rate))
19994d12b13SY.C. Chen 				best = loop;
20094d12b13SY.C. Chen 			loop++;
20194d12b13SY.C. Chen 		}
20294d12b13SY.C. Chen 		if (best || !check_sync)
203312fec14SDave Airlie 			break;
20494d12b13SY.C. Chen 		check_sync = 0;
205259d14a7SThomas Zimmermann 	}
206259d14a7SThomas Zimmermann 
20794d12b13SY.C. Chen 	if (best)
20894d12b13SY.C. Chen 		vbios_mode->enh_table = best;
209312fec14SDave Airlie 
210312fec14SDave Airlie 	hborder = (vbios_mode->enh_table->flags & HBorder) ? 8 : 0;
211312fec14SDave Airlie 	vborder = (vbios_mode->enh_table->flags & VBorder) ? 8 : 0;
212312fec14SDave Airlie 
213312fec14SDave Airlie 	adjusted_mode->crtc_htotal = vbios_mode->enh_table->ht;
214312fec14SDave Airlie 	adjusted_mode->crtc_hblank_start = vbios_mode->enh_table->hde + hborder;
215312fec14SDave Airlie 	adjusted_mode->crtc_hblank_end = vbios_mode->enh_table->ht - hborder;
216312fec14SDave Airlie 	adjusted_mode->crtc_hsync_start = vbios_mode->enh_table->hde + hborder +
217312fec14SDave Airlie 		vbios_mode->enh_table->hfp;
218312fec14SDave Airlie 	adjusted_mode->crtc_hsync_end = (vbios_mode->enh_table->hde + hborder +
219312fec14SDave Airlie 					 vbios_mode->enh_table->hfp +
220312fec14SDave Airlie 					 vbios_mode->enh_table->hsync);
221312fec14SDave Airlie 
222312fec14SDave Airlie 	adjusted_mode->crtc_vtotal = vbios_mode->enh_table->vt;
223312fec14SDave Airlie 	adjusted_mode->crtc_vblank_start = vbios_mode->enh_table->vde + vborder;
224312fec14SDave Airlie 	adjusted_mode->crtc_vblank_end = vbios_mode->enh_table->vt - vborder;
225312fec14SDave Airlie 	adjusted_mode->crtc_vsync_start = vbios_mode->enh_table->vde + vborder +
226312fec14SDave Airlie 		vbios_mode->enh_table->vfp;
227312fec14SDave Airlie 	adjusted_mode->crtc_vsync_end = (vbios_mode->enh_table->vde + vborder +
228312fec14SDave Airlie 					 vbios_mode->enh_table->vfp +
229312fec14SDave Airlie 					 vbios_mode->enh_table->vsync);
230312fec14SDave Airlie 
231259d14a7SThomas Zimmermann 	return true;
232259d14a7SThomas Zimmermann }
233259d14a7SThomas Zimmermann 
234ae37025dSThomas Zimmermann static void ast_set_vbios_color_reg(struct ast_private *ast,
235ae37025dSThomas Zimmermann 				    const struct drm_format_info *format,
236259d14a7SThomas Zimmermann 				    const struct ast_vbios_mode_info *vbios_mode)
237259d14a7SThomas Zimmermann {
238259d14a7SThomas Zimmermann 	u32 color_index;
239259d14a7SThomas Zimmermann 
240ae37025dSThomas Zimmermann 	switch (format->cpp[0]) {
241259d14a7SThomas Zimmermann 	case 1:
242259d14a7SThomas Zimmermann 		color_index = VGAModeIndex - 1;
243259d14a7SThomas Zimmermann 		break;
244259d14a7SThomas Zimmermann 	case 2:
245259d14a7SThomas Zimmermann 		color_index = HiCModeIndex;
246259d14a7SThomas Zimmermann 		break;
247259d14a7SThomas Zimmermann 	case 3:
248259d14a7SThomas Zimmermann 	case 4:
249259d14a7SThomas Zimmermann 		color_index = TrueCModeIndex;
250291ddeb6SColin Ian King 		break;
251259d14a7SThomas Zimmermann 	default:
252259d14a7SThomas Zimmermann 		return;
253259d14a7SThomas Zimmermann 	}
254259d14a7SThomas Zimmermann 
255259d14a7SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8c, (u8)((color_index & 0x0f) << 4));
256259d14a7SThomas Zimmermann 
257259d14a7SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0x00);
258259d14a7SThomas Zimmermann 
259259d14a7SThomas Zimmermann 	if (vbios_mode->enh_table->flags & NewModeInfo) {
260259d14a7SThomas Zimmermann 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0xa8);
261ae37025dSThomas Zimmermann 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x92, format->cpp[0] * 8);
262259d14a7SThomas Zimmermann 	}
263259d14a7SThomas Zimmermann }
264259d14a7SThomas Zimmermann 
265ae37025dSThomas Zimmermann static void ast_set_vbios_mode_reg(struct ast_private *ast,
266259d14a7SThomas Zimmermann 				   const struct drm_display_mode *adjusted_mode,
267259d14a7SThomas Zimmermann 				   const struct ast_vbios_mode_info *vbios_mode)
268259d14a7SThomas Zimmermann {
269259d14a7SThomas Zimmermann 	u32 refresh_rate_index, mode_id;
270259d14a7SThomas Zimmermann 
271312fec14SDave Airlie 	refresh_rate_index = vbios_mode->enh_table->refresh_rate_index;
272312fec14SDave Airlie 	mode_id = vbios_mode->enh_table->mode_id;
273312fec14SDave Airlie 
274312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8d, refresh_rate_index & 0xff);
275312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8e, mode_id & 0xff);
276312fec14SDave Airlie 
277f1f62f2cSDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0x00);
278259d14a7SThomas Zimmermann 
279f1f62f2cSDave Airlie 	if (vbios_mode->enh_table->flags & NewModeInfo) {
280312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0xa8);
281312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x93, adjusted_mode->clock / 1000);
282312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x94, adjusted_mode->crtc_hdisplay);
283312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x95, adjusted_mode->crtc_hdisplay >> 8);
284312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x96, adjusted_mode->crtc_vdisplay);
285312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x97, adjusted_mode->crtc_vdisplay >> 8);
286312fec14SDave Airlie 	}
287f1f62f2cSDave Airlie }
288312fec14SDave Airlie 
289ae37025dSThomas Zimmermann static void ast_set_std_reg(struct ast_private *ast,
290ae37025dSThomas Zimmermann 			    struct drm_display_mode *mode,
291312fec14SDave Airlie 			    struct ast_vbios_mode_info *vbios_mode)
292312fec14SDave Airlie {
29322acdbb1SBenjamin Herrenschmidt 	const struct ast_vbios_stdtable *stdtable;
294312fec14SDave Airlie 	u32 i;
295312fec14SDave Airlie 	u8 jreg;
296312fec14SDave Airlie 
297312fec14SDave Airlie 	stdtable = vbios_mode->std_table;
298312fec14SDave Airlie 
299312fec14SDave Airlie 	jreg = stdtable->misc;
300312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
301312fec14SDave Airlie 
3022fbeec03SThomas Zimmermann 	/* Set SEQ; except Screen Disable field */
303312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_SEQ_PORT, 0x00, 0x03);
3042fbeec03SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x01, 0xdf, stdtable->seq[0]);
3052fbeec03SThomas Zimmermann 	for (i = 1; i < 4; i++) {
306312fec14SDave Airlie 		jreg = stdtable->seq[i];
307312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_SEQ_PORT, (i + 1), jreg);
308312fec14SDave Airlie 	}
309312fec14SDave Airlie 
310a21fdd7aSThomas Zimmermann 	/* Set CRTC; except base address and offset */
311312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
312a21fdd7aSThomas Zimmermann 	for (i = 0; i < 12; i++)
313a21fdd7aSThomas Zimmermann 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
314a21fdd7aSThomas Zimmermann 	for (i = 14; i < 19; i++)
315a21fdd7aSThomas Zimmermann 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
316a21fdd7aSThomas Zimmermann 	for (i = 20; i < 25; i++)
317312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
318312fec14SDave Airlie 
319312fec14SDave Airlie 	/* set AR */
320312fec14SDave Airlie 	jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
321312fec14SDave Airlie 	for (i = 0; i < 20; i++) {
322312fec14SDave Airlie 		jreg = stdtable->ar[i];
323312fec14SDave Airlie 		ast_io_write8(ast, AST_IO_AR_PORT_WRITE, (u8)i);
324312fec14SDave Airlie 		ast_io_write8(ast, AST_IO_AR_PORT_WRITE, jreg);
325312fec14SDave Airlie 	}
326312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x14);
327312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x00);
328312fec14SDave Airlie 
329312fec14SDave Airlie 	jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
330312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x20);
331312fec14SDave Airlie 
332312fec14SDave Airlie 	/* Set GR */
333312fec14SDave Airlie 	for (i = 0; i < 9; i++)
334312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_GR_PORT, i, stdtable->gr[i]);
335312fec14SDave Airlie }
336312fec14SDave Airlie 
337ae37025dSThomas Zimmermann static void ast_set_crtc_reg(struct ast_private *ast,
338ae37025dSThomas Zimmermann 			     struct drm_display_mode *mode,
339312fec14SDave Airlie 			     struct ast_vbios_mode_info *vbios_mode)
340312fec14SDave Airlie {
341312fec14SDave Airlie 	u8 jreg05 = 0, jreg07 = 0, jreg09 = 0, jregAC = 0, jregAD = 0, jregAE = 0;
3429f93c8b3SY.C. Chen 	u16 temp, precache = 0;
3439f93c8b3SY.C. Chen 
344d6cbe630SJammy Huang 	if ((ast->chip == AST2500 || ast->chip == AST2600) &&
3459f93c8b3SY.C. Chen 	    (vbios_mode->enh_table->flags & AST2500PreCatchCRT))
3469f93c8b3SY.C. Chen 		precache = 40;
347312fec14SDave Airlie 
348312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
349312fec14SDave Airlie 
350312fec14SDave Airlie 	temp = (mode->crtc_htotal >> 3) - 5;
351312fec14SDave Airlie 	if (temp & 0x100)
352312fec14SDave Airlie 		jregAC |= 0x01; /* HT D[8] */
353312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x00, 0x00, temp);
354312fec14SDave Airlie 
355312fec14SDave Airlie 	temp = (mode->crtc_hdisplay >> 3) - 1;
356312fec14SDave Airlie 	if (temp & 0x100)
357312fec14SDave Airlie 		jregAC |= 0x04; /* HDE D[8] */
358312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x01, 0x00, temp);
359312fec14SDave Airlie 
360312fec14SDave Airlie 	temp = (mode->crtc_hblank_start >> 3) - 1;
361312fec14SDave Airlie 	if (temp & 0x100)
362312fec14SDave Airlie 		jregAC |= 0x10; /* HBS D[8] */
363312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x02, 0x00, temp);
364312fec14SDave Airlie 
365312fec14SDave Airlie 	temp = ((mode->crtc_hblank_end >> 3) - 1) & 0x7f;
366312fec14SDave Airlie 	if (temp & 0x20)
367312fec14SDave Airlie 		jreg05 |= 0x80;  /* HBE D[5] */
368312fec14SDave Airlie 	if (temp & 0x40)
369312fec14SDave Airlie 		jregAD |= 0x01;  /* HBE D[5] */
370312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x03, 0xE0, (temp & 0x1f));
371312fec14SDave Airlie 
3729f93c8b3SY.C. Chen 	temp = ((mode->crtc_hsync_start-precache) >> 3) - 1;
373312fec14SDave Airlie 	if (temp & 0x100)
374312fec14SDave Airlie 		jregAC |= 0x40; /* HRS D[5] */
375312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x04, 0x00, temp);
376312fec14SDave Airlie 
3779f93c8b3SY.C. Chen 	temp = (((mode->crtc_hsync_end-precache) >> 3) - 1) & 0x3f;
378312fec14SDave Airlie 	if (temp & 0x20)
379312fec14SDave Airlie 		jregAD |= 0x04; /* HRE D[5] */
380312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x05, 0x60, (u8)((temp & 0x1f) | jreg05));
381312fec14SDave Airlie 
382312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAC, 0x00, jregAC);
383312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAD, 0x00, jregAD);
384312fec14SDave Airlie 
385d6cbe630SJammy Huang 	// Workaround for HSync Time non octave pixels (1920x1080@60Hz HSync 44 pixels);
386d6cbe630SJammy Huang 	if ((ast->chip == AST2600) && (mode->crtc_vdisplay == 1080))
387d6cbe630SJammy Huang 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xFC, 0xFD, 0x02);
388d6cbe630SJammy Huang 	else
389d6cbe630SJammy Huang 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xFC, 0xFD, 0x00);
390d6cbe630SJammy Huang 
391312fec14SDave Airlie 	/* vert timings */
392312fec14SDave Airlie 	temp = (mode->crtc_vtotal) - 2;
393312fec14SDave Airlie 	if (temp & 0x100)
394312fec14SDave Airlie 		jreg07 |= 0x01;
395312fec14SDave Airlie 	if (temp & 0x200)
396312fec14SDave Airlie 		jreg07 |= 0x20;
397312fec14SDave Airlie 	if (temp & 0x400)
398312fec14SDave Airlie 		jregAE |= 0x01;
399312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x06, 0x00, temp);
400312fec14SDave Airlie 
401312fec14SDave Airlie 	temp = (mode->crtc_vsync_start) - 1;
402312fec14SDave Airlie 	if (temp & 0x100)
403312fec14SDave Airlie 		jreg07 |= 0x04;
404312fec14SDave Airlie 	if (temp & 0x200)
405312fec14SDave Airlie 		jreg07 |= 0x80;
406312fec14SDave Airlie 	if (temp & 0x400)
407312fec14SDave Airlie 		jregAE |= 0x08;
408312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x10, 0x00, temp);
409312fec14SDave Airlie 
410312fec14SDave Airlie 	temp = (mode->crtc_vsync_end - 1) & 0x3f;
411312fec14SDave Airlie 	if (temp & 0x10)
412312fec14SDave Airlie 		jregAE |= 0x20;
413312fec14SDave Airlie 	if (temp & 0x20)
414312fec14SDave Airlie 		jregAE |= 0x40;
415312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x70, temp & 0xf);
416312fec14SDave Airlie 
417312fec14SDave Airlie 	temp = mode->crtc_vdisplay - 1;
418312fec14SDave Airlie 	if (temp & 0x100)
419312fec14SDave Airlie 		jreg07 |= 0x02;
420312fec14SDave Airlie 	if (temp & 0x200)
421312fec14SDave Airlie 		jreg07 |= 0x40;
422312fec14SDave Airlie 	if (temp & 0x400)
423312fec14SDave Airlie 		jregAE |= 0x02;
424312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x12, 0x00, temp);
425312fec14SDave Airlie 
426312fec14SDave Airlie 	temp = mode->crtc_vblank_start - 1;
427312fec14SDave Airlie 	if (temp & 0x100)
428312fec14SDave Airlie 		jreg07 |= 0x08;
429312fec14SDave Airlie 	if (temp & 0x200)
430312fec14SDave Airlie 		jreg09 |= 0x20;
431312fec14SDave Airlie 	if (temp & 0x400)
432312fec14SDave Airlie 		jregAE |= 0x04;
433312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x15, 0x00, temp);
434312fec14SDave Airlie 
435312fec14SDave Airlie 	temp = mode->crtc_vblank_end - 1;
436312fec14SDave Airlie 	if (temp & 0x100)
437312fec14SDave Airlie 		jregAE |= 0x10;
438312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x16, 0x00, temp);
439312fec14SDave Airlie 
440312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x07, 0x00, jreg07);
441312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x09, 0xdf, jreg09);
442312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAE, 0x00, (jregAE | 0x80));
443312fec14SDave Airlie 
4449f93c8b3SY.C. Chen 	if (precache)
4459f93c8b3SY.C. Chen 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0x3f, 0x80);
4469f93c8b3SY.C. Chen 	else
4479f93c8b3SY.C. Chen 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0x3f, 0x00);
4489f93c8b3SY.C. Chen 
449312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x80);
450312fec14SDave Airlie }
451312fec14SDave Airlie 
452ae37025dSThomas Zimmermann static void ast_set_offset_reg(struct ast_private *ast,
453ae37025dSThomas Zimmermann 			       struct drm_framebuffer *fb)
454312fec14SDave Airlie {
455312fec14SDave Airlie 	u16 offset;
456312fec14SDave Airlie 
4577445283aSVille Syrjälä 	offset = fb->pitches[0] >> 3;
458312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x13, (offset & 0xff));
459312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xb0, (offset >> 8) & 0x3f);
460312fec14SDave Airlie }
461312fec14SDave Airlie 
462ae37025dSThomas Zimmermann static void ast_set_dclk_reg(struct ast_private *ast,
463ae37025dSThomas Zimmermann 			     struct drm_display_mode *mode,
464312fec14SDave Airlie 			     struct ast_vbios_mode_info *vbios_mode)
465312fec14SDave Airlie {
46622acdbb1SBenjamin Herrenschmidt 	const struct ast_vbios_dclk_info *clk_info;
467312fec14SDave Airlie 
468d6cbe630SJammy Huang 	if ((ast->chip == AST2500) || (ast->chip == AST2600))
4699f93c8b3SY.C. Chen 		clk_info = &dclk_table_ast2500[vbios_mode->enh_table->dclk_index];
4709f93c8b3SY.C. Chen 	else
471312fec14SDave Airlie 		clk_info = &dclk_table[vbios_mode->enh_table->dclk_index];
472312fec14SDave Airlie 
473312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc0, 0x00, clk_info->param1);
474312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc1, 0x00, clk_info->param2);
475312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xbb, 0x0f,
4769f93c8b3SY.C. Chen 			       (clk_info->param3 & 0xc0) |
4779f93c8b3SY.C. Chen 			       ((clk_info->param3 & 0x3) << 4));
478312fec14SDave Airlie }
479312fec14SDave Airlie 
480ae37025dSThomas Zimmermann static void ast_set_color_reg(struct ast_private *ast,
481ae37025dSThomas Zimmermann 			      const struct drm_format_info *format)
482312fec14SDave Airlie {
483312fec14SDave Airlie 	u8 jregA0 = 0, jregA3 = 0, jregA8 = 0;
484312fec14SDave Airlie 
485ae37025dSThomas Zimmermann 	switch (format->cpp[0] * 8) {
486312fec14SDave Airlie 	case 8:
487312fec14SDave Airlie 		jregA0 = 0x70;
488312fec14SDave Airlie 		jregA3 = 0x01;
489312fec14SDave Airlie 		jregA8 = 0x00;
490312fec14SDave Airlie 		break;
491312fec14SDave Airlie 	case 15:
492312fec14SDave Airlie 	case 16:
493312fec14SDave Airlie 		jregA0 = 0x70;
494312fec14SDave Airlie 		jregA3 = 0x04;
495312fec14SDave Airlie 		jregA8 = 0x02;
496312fec14SDave Airlie 		break;
497312fec14SDave Airlie 	case 32:
498312fec14SDave Airlie 		jregA0 = 0x70;
499312fec14SDave Airlie 		jregA3 = 0x08;
500312fec14SDave Airlie 		jregA8 = 0x02;
501312fec14SDave Airlie 		break;
502312fec14SDave Airlie 	}
503312fec14SDave Airlie 
504312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa0, 0x8f, jregA0);
505312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xf0, jregA3);
506312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa8, 0xfd, jregA8);
5070d45ad98SThomas Zimmermann }
5080d45ad98SThomas Zimmermann 
509ae37025dSThomas Zimmermann static void ast_set_crtthd_reg(struct ast_private *ast)
5100d45ad98SThomas Zimmermann {
511312fec14SDave Airlie 	/* Set Threshold */
512bcc77411SKuoHsiang Chou 	if (ast->chip == AST2600) {
513bcc77411SKuoHsiang Chou 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0xe0);
514bcc77411SKuoHsiang Chou 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0xa0);
515bcc77411SKuoHsiang Chou 	} else if (ast->chip == AST2300 || ast->chip == AST2400 ||
5169f93c8b3SY.C. Chen 	    ast->chip == AST2500) {
517312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x78);
518312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x60);
519312fec14SDave Airlie 	} else if (ast->chip == AST2100 ||
520312fec14SDave Airlie 		   ast->chip == AST1100 ||
521312fec14SDave Airlie 		   ast->chip == AST2200 ||
522312fec14SDave Airlie 		   ast->chip == AST2150) {
523312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x3f);
524312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x2f);
525312fec14SDave Airlie 	} else {
526312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x2f);
527312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x1f);
528312fec14SDave Airlie 	}
529312fec14SDave Airlie }
530312fec14SDave Airlie 
531ae37025dSThomas Zimmermann static void ast_set_sync_reg(struct ast_private *ast,
532ae37025dSThomas Zimmermann 			     struct drm_display_mode *mode,
533312fec14SDave Airlie 			     struct ast_vbios_mode_info *vbios_mode)
534312fec14SDave Airlie {
535312fec14SDave Airlie 	u8 jreg;
536312fec14SDave Airlie 
537312fec14SDave Airlie 	jreg  = ast_io_read8(ast, AST_IO_MISC_PORT_READ);
53894d12b13SY.C. Chen 	jreg &= ~0xC0;
5396c9bd443SGregory Williams 	if (vbios_mode->enh_table->flags & NVSync)
5406c9bd443SGregory Williams 		jreg |= 0x80;
5416c9bd443SGregory Williams 	if (vbios_mode->enh_table->flags & NHSync)
5426c9bd443SGregory Williams 		jreg |= 0x40;
543312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
544312fec14SDave Airlie }
545312fec14SDave Airlie 
546ae37025dSThomas Zimmermann static void ast_set_start_address_crt1(struct ast_private *ast,
5476c9bd443SGregory Williams 				       unsigned int offset)
548312fec14SDave Airlie {
549312fec14SDave Airlie 	u32 addr;
550312fec14SDave Airlie 
551312fec14SDave Airlie 	addr = offset >> 2;
552312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0d, (u8)(addr & 0xff));
553312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0c, (u8)((addr >> 8) & 0xff));
554312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xaf, (u8)((addr >> 16) & 0xff));
555312fec14SDave Airlie 
556312fec14SDave Airlie }
557312fec14SDave Airlie 
55839edb287SThomas Zimmermann static void ast_wait_for_vretrace(struct ast_private *ast)
55939edb287SThomas Zimmermann {
56039edb287SThomas Zimmermann 	unsigned long timeout = jiffies + HZ;
56139edb287SThomas Zimmermann 	u8 vgair1;
56239edb287SThomas Zimmermann 
56339edb287SThomas Zimmermann 	do {
56439edb287SThomas Zimmermann 		vgair1 = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
56539edb287SThomas Zimmermann 	} while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout));
56639edb287SThomas Zimmermann }
56739edb287SThomas Zimmermann 
568a6ff807bSThomas Zimmermann /*
569a6ff807bSThomas Zimmermann  * Primary plane
570a6ff807bSThomas Zimmermann  */
571a6ff807bSThomas Zimmermann 
572a6ff807bSThomas Zimmermann static const uint32_t ast_primary_plane_formats[] = {
573a6ff807bSThomas Zimmermann 	DRM_FORMAT_XRGB8888,
574a6ff807bSThomas Zimmermann 	DRM_FORMAT_RGB565,
575a6ff807bSThomas Zimmermann 	DRM_FORMAT_C8,
576a6ff807bSThomas Zimmermann };
577a6ff807bSThomas Zimmermann 
578ae46a57dSThomas Zimmermann static int ast_primary_plane_helper_atomic_check(struct drm_plane *plane,
5797c11b99aSMaxime Ripard 						 struct drm_atomic_state *state)
580a6ff807bSThomas Zimmermann {
5810432a504SThomas Zimmermann 	struct drm_device *dev = plane->dev;
5827c11b99aSMaxime Ripard 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
5837c11b99aSMaxime Ripard 										 plane);
5840432a504SThomas Zimmermann 	struct drm_crtc_state *crtc_state = NULL;
5853339fdf5SThomas Zimmermann 	struct ast_crtc_state *ast_crtc_state;
586ae46a57dSThomas Zimmermann 	int ret;
587ae46a57dSThomas Zimmermann 
5880432a504SThomas Zimmermann 	if (new_plane_state->crtc)
5890432a504SThomas Zimmermann 		crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
590ae46a57dSThomas Zimmermann 
591ba5c1649SMaxime Ripard 	ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
592cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
593cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
594ae46a57dSThomas Zimmermann 						  false, true);
5950432a504SThomas Zimmermann 	if (ret) {
596ae46a57dSThomas Zimmermann 		return ret;
5970432a504SThomas Zimmermann 	} else if (!new_plane_state->visible) {
5980432a504SThomas Zimmermann 		if (drm_WARN_ON(dev, new_plane_state->crtc)) /* cannot legally happen */
5990432a504SThomas Zimmermann 			return -EINVAL;
6000432a504SThomas Zimmermann 		else
6013339fdf5SThomas Zimmermann 			return 0;
6020432a504SThomas Zimmermann 	}
6033339fdf5SThomas Zimmermann 
6043339fdf5SThomas Zimmermann 	ast_crtc_state = to_ast_crtc_state(crtc_state);
6053339fdf5SThomas Zimmermann 
606ba5c1649SMaxime Ripard 	ast_crtc_state->format = new_plane_state->fb->format;
6073339fdf5SThomas Zimmermann 
608a6ff807bSThomas Zimmermann 	return 0;
609a6ff807bSThomas Zimmermann }
610a6ff807bSThomas Zimmermann 
6113a53230eSSamuel Zou static void
6123a53230eSSamuel Zou ast_primary_plane_helper_atomic_update(struct drm_plane *plane,
613977697e2SMaxime Ripard 				       struct drm_atomic_state *state)
614a6ff807bSThomas Zimmermann {
615977697e2SMaxime Ripard 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
616977697e2SMaxime Ripard 									   plane);
6171a19b4cbSThomas Zimmermann 	struct drm_device *dev = plane->dev;
6181a19b4cbSThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
61937418bf1SMaxime Ripard 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
62037418bf1SMaxime Ripard 									   plane);
621a6ff807bSThomas Zimmermann 	struct drm_gem_vram_object *gbo;
622a6ff807bSThomas Zimmermann 	s64 gpu_addr;
62341016fe1SMaxime Ripard 	struct drm_framebuffer *fb = new_state->fb;
6245638c82cSThomas Zimmermann 	struct drm_framebuffer *old_fb = old_state->fb;
625a6ff807bSThomas Zimmermann 
6265638c82cSThomas Zimmermann 	if (!old_fb || (fb->format != old_fb->format)) {
62741016fe1SMaxime Ripard 		struct drm_crtc_state *crtc_state = new_state->crtc->state;
6285638c82cSThomas Zimmermann 		struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
6295638c82cSThomas Zimmermann 		struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
6305638c82cSThomas Zimmermann 
6315638c82cSThomas Zimmermann 		ast_set_color_reg(ast, fb->format);
6325638c82cSThomas Zimmermann 		ast_set_vbios_color_reg(ast, fb->format, vbios_mode_info);
6335638c82cSThomas Zimmermann 	}
6345638c82cSThomas Zimmermann 
6355638c82cSThomas Zimmermann 	gbo = drm_gem_vram_of_gem(fb->obj[0]);
636a6ff807bSThomas Zimmermann 	gpu_addr = drm_gem_vram_offset(gbo);
6371a19b4cbSThomas Zimmermann 	if (drm_WARN_ON_ONCE(dev, gpu_addr < 0))
638a6ff807bSThomas Zimmermann 		return; /* Bug: we didn't pin the BO to VRAM in prepare_fb. */
639a6ff807bSThomas Zimmermann 
6405638c82cSThomas Zimmermann 	ast_set_offset_reg(ast, fb);
641ae37025dSThomas Zimmermann 	ast_set_start_address_crt1(ast, (u32)gpu_addr);
6422fbeec03SThomas Zimmermann 
6432fbeec03SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x00);
6442fbeec03SThomas Zimmermann }
6452fbeec03SThomas Zimmermann 
6462fbeec03SThomas Zimmermann static void
6472fbeec03SThomas Zimmermann ast_primary_plane_helper_atomic_disable(struct drm_plane *plane,
648977697e2SMaxime Ripard 					struct drm_atomic_state *state)
6492fbeec03SThomas Zimmermann {
650fa7dbd76SThomas Zimmermann 	struct ast_private *ast = to_ast_private(plane->dev);
6512fbeec03SThomas Zimmermann 
6522fbeec03SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x20);
653a6ff807bSThomas Zimmermann }
654a6ff807bSThomas Zimmermann 
655a6ff807bSThomas Zimmermann static const struct drm_plane_helper_funcs ast_primary_plane_helper_funcs = {
656f8bd3dbbSDaniel Vetter 	DRM_GEM_VRAM_PLANE_HELPER_FUNCS,
657a6ff807bSThomas Zimmermann 	.atomic_check = ast_primary_plane_helper_atomic_check,
658a6ff807bSThomas Zimmermann 	.atomic_update = ast_primary_plane_helper_atomic_update,
6592fbeec03SThomas Zimmermann 	.atomic_disable = ast_primary_plane_helper_atomic_disable,
660a6ff807bSThomas Zimmermann };
661a6ff807bSThomas Zimmermann 
662a6ff807bSThomas Zimmermann static const struct drm_plane_funcs ast_primary_plane_funcs = {
663a6ff807bSThomas Zimmermann 	.update_plane = drm_atomic_helper_update_plane,
664a6ff807bSThomas Zimmermann 	.disable_plane = drm_atomic_helper_disable_plane,
665a6ff807bSThomas Zimmermann 	.destroy = drm_plane_cleanup,
666a6ff807bSThomas Zimmermann 	.reset = drm_atomic_helper_plane_reset,
667a6ff807bSThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
668a6ff807bSThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
669a6ff807bSThomas Zimmermann };
670a6ff807bSThomas Zimmermann 
671616048afSThomas Zimmermann static int ast_primary_plane_init(struct ast_private *ast)
672616048afSThomas Zimmermann {
673616048afSThomas Zimmermann 	struct drm_device *dev = &ast->base;
674616048afSThomas Zimmermann 	struct drm_plane *primary_plane = &ast->primary_plane;
675616048afSThomas Zimmermann 	int ret;
676616048afSThomas Zimmermann 
677616048afSThomas Zimmermann 	ret = drm_universal_plane_init(dev, primary_plane, 0x01,
678616048afSThomas Zimmermann 				       &ast_primary_plane_funcs,
679616048afSThomas Zimmermann 				       ast_primary_plane_formats,
680616048afSThomas Zimmermann 				       ARRAY_SIZE(ast_primary_plane_formats),
681616048afSThomas Zimmermann 				       NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
682616048afSThomas Zimmermann 	if (ret) {
683616048afSThomas Zimmermann 		drm_err(dev, "drm_universal_plane_init() failed: %d\n", ret);
684616048afSThomas Zimmermann 		return ret;
685616048afSThomas Zimmermann 	}
686616048afSThomas Zimmermann 	drm_plane_helper_add(primary_plane, &ast_primary_plane_helper_funcs);
687616048afSThomas Zimmermann 
688616048afSThomas Zimmermann 	return 0;
689616048afSThomas Zimmermann }
690616048afSThomas Zimmermann 
691a6ff807bSThomas Zimmermann /*
69202f3bb75SThomas Zimmermann  * Cursor plane
69302f3bb75SThomas Zimmermann  */
69402f3bb75SThomas Zimmermann 
695718c2286SThomas Zimmermann static void ast_update_cursor_image(u8 __iomem *dst, const u8 *src, int width, int height)
696718c2286SThomas Zimmermann {
697718c2286SThomas Zimmermann 	union {
698718c2286SThomas Zimmermann 		u32 ul;
699718c2286SThomas Zimmermann 		u8 b[4];
700718c2286SThomas Zimmermann 	} srcdata32[2], data32;
701718c2286SThomas Zimmermann 	union {
702718c2286SThomas Zimmermann 		u16 us;
703718c2286SThomas Zimmermann 		u8 b[2];
704718c2286SThomas Zimmermann 	} data16;
705718c2286SThomas Zimmermann 	u32 csum = 0;
706718c2286SThomas Zimmermann 	s32 alpha_dst_delta, last_alpha_dst_delta;
707718c2286SThomas Zimmermann 	u8 __iomem *dstxor;
708718c2286SThomas Zimmermann 	const u8 *srcxor;
709718c2286SThomas Zimmermann 	int i, j;
710718c2286SThomas Zimmermann 	u32 per_pixel_copy, two_pixel_copy;
711718c2286SThomas Zimmermann 
712718c2286SThomas Zimmermann 	alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
713718c2286SThomas Zimmermann 	last_alpha_dst_delta = alpha_dst_delta - (width << 1);
714718c2286SThomas Zimmermann 
715718c2286SThomas Zimmermann 	srcxor = src;
716718c2286SThomas Zimmermann 	dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
717718c2286SThomas Zimmermann 	per_pixel_copy = width & 1;
718718c2286SThomas Zimmermann 	two_pixel_copy = width >> 1;
719718c2286SThomas Zimmermann 
720718c2286SThomas Zimmermann 	for (j = 0; j < height; j++) {
721718c2286SThomas Zimmermann 		for (i = 0; i < two_pixel_copy; i++) {
722718c2286SThomas Zimmermann 			srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
723718c2286SThomas Zimmermann 			srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
724718c2286SThomas Zimmermann 			data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
725718c2286SThomas Zimmermann 			data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
726718c2286SThomas Zimmermann 			data32.b[2] = srcdata32[1].b[1] | (srcdata32[1].b[0] >> 4);
727718c2286SThomas Zimmermann 			data32.b[3] = srcdata32[1].b[3] | (srcdata32[1].b[2] >> 4);
728718c2286SThomas Zimmermann 
729718c2286SThomas Zimmermann 			writel(data32.ul, dstxor);
730718c2286SThomas Zimmermann 			csum += data32.ul;
731718c2286SThomas Zimmermann 
732718c2286SThomas Zimmermann 			dstxor += 4;
733718c2286SThomas Zimmermann 			srcxor += 8;
734718c2286SThomas Zimmermann 
735718c2286SThomas Zimmermann 		}
736718c2286SThomas Zimmermann 
737718c2286SThomas Zimmermann 		for (i = 0; i < per_pixel_copy; i++) {
738718c2286SThomas Zimmermann 			srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
739718c2286SThomas Zimmermann 			data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
740718c2286SThomas Zimmermann 			data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
741718c2286SThomas Zimmermann 			writew(data16.us, dstxor);
742718c2286SThomas Zimmermann 			csum += (u32)data16.us;
743718c2286SThomas Zimmermann 
744718c2286SThomas Zimmermann 			dstxor += 2;
745718c2286SThomas Zimmermann 			srcxor += 4;
746718c2286SThomas Zimmermann 		}
747718c2286SThomas Zimmermann 		dstxor += last_alpha_dst_delta;
748718c2286SThomas Zimmermann 	}
749718c2286SThomas Zimmermann 
750718c2286SThomas Zimmermann 	/* write checksum + signature */
751718c2286SThomas Zimmermann 	dst += AST_HWC_SIZE;
752718c2286SThomas Zimmermann 	writel(csum, dst);
753718c2286SThomas Zimmermann 	writel(width, dst + AST_HWC_SIGNATURE_SizeX);
754718c2286SThomas Zimmermann 	writel(height, dst + AST_HWC_SIGNATURE_SizeY);
755718c2286SThomas Zimmermann 	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
756718c2286SThomas Zimmermann 	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
757718c2286SThomas Zimmermann }
758718c2286SThomas Zimmermann 
759718c2286SThomas Zimmermann static void ast_set_cursor_base(struct ast_private *ast, u64 address)
760718c2286SThomas Zimmermann {
761718c2286SThomas Zimmermann 	u8 addr0 = (address >> 3) & 0xff;
762718c2286SThomas Zimmermann 	u8 addr1 = (address >> 11) & 0xff;
763718c2286SThomas Zimmermann 	u8 addr2 = (address >> 19) & 0xff;
764718c2286SThomas Zimmermann 
765718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc8, addr0);
766718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc9, addr1);
767718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xca, addr2);
768718c2286SThomas Zimmermann }
769718c2286SThomas Zimmermann 
770718c2286SThomas Zimmermann static void ast_set_cursor_location(struct ast_private *ast, u16 x, u16 y,
771718c2286SThomas Zimmermann 				    u8 x_offset, u8 y_offset)
772718c2286SThomas Zimmermann {
773718c2286SThomas Zimmermann 	u8 x0 = (x & 0x00ff);
774718c2286SThomas Zimmermann 	u8 x1 = (x & 0x0f00) >> 8;
775718c2286SThomas Zimmermann 	u8 y0 = (y & 0x00ff);
776718c2286SThomas Zimmermann 	u8 y1 = (y & 0x0700) >> 8;
777718c2286SThomas Zimmermann 
778718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc2, x_offset);
779718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc3, y_offset);
780718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc4, x0);
781718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc5, x1);
782718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc6, y0);
783718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc7, y1);
784718c2286SThomas Zimmermann }
785718c2286SThomas Zimmermann 
786718c2286SThomas Zimmermann static void ast_set_cursor_enabled(struct ast_private *ast, bool enabled)
787718c2286SThomas Zimmermann {
788718c2286SThomas Zimmermann 	static const u8 mask = (u8)~(AST_IO_VGACRCB_HWC_16BPP |
789718c2286SThomas Zimmermann 				     AST_IO_VGACRCB_HWC_ENABLED);
790718c2286SThomas Zimmermann 
791718c2286SThomas Zimmermann 	u8 vgacrcb = AST_IO_VGACRCB_HWC_16BPP;
792718c2286SThomas Zimmermann 
793718c2286SThomas Zimmermann 	if (enabled)
794718c2286SThomas Zimmermann 		vgacrcb |= AST_IO_VGACRCB_HWC_ENABLED;
795718c2286SThomas Zimmermann 
796718c2286SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, mask, vgacrcb);
797718c2286SThomas Zimmermann }
798718c2286SThomas Zimmermann 
79902f3bb75SThomas Zimmermann static const uint32_t ast_cursor_plane_formats[] = {
80002f3bb75SThomas Zimmermann 	DRM_FORMAT_ARGB8888,
80102f3bb75SThomas Zimmermann };
80202f3bb75SThomas Zimmermann 
80302f3bb75SThomas Zimmermann static int ast_cursor_plane_helper_atomic_check(struct drm_plane *plane,
8047c11b99aSMaxime Ripard 						struct drm_atomic_state *state)
80502f3bb75SThomas Zimmermann {
8067c11b99aSMaxime Ripard 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
8077c11b99aSMaxime Ripard 										 plane);
808ba5c1649SMaxime Ripard 	struct drm_framebuffer *fb = new_plane_state->fb;
8090432a504SThomas Zimmermann 	struct drm_crtc_state *crtc_state = NULL;
810ae46a57dSThomas Zimmermann 	int ret;
811ae46a57dSThomas Zimmermann 
8120432a504SThomas Zimmermann 	if (new_plane_state->crtc)
8130432a504SThomas Zimmermann 		crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
814ae46a57dSThomas Zimmermann 
815ba5c1649SMaxime Ripard 	ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
816cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
817cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
818ae46a57dSThomas Zimmermann 						  true, true);
8190432a504SThomas Zimmermann 	if (ret || !new_plane_state->visible)
820ae46a57dSThomas Zimmermann 		return ret;
821ae46a57dSThomas Zimmermann 
822ae46a57dSThomas Zimmermann 	if (fb->width > AST_MAX_HWC_WIDTH || fb->height > AST_MAX_HWC_HEIGHT)
823ae46a57dSThomas Zimmermann 		return -EINVAL;
824ae46a57dSThomas Zimmermann 
82502f3bb75SThomas Zimmermann 	return 0;
82602f3bb75SThomas Zimmermann }
82702f3bb75SThomas Zimmermann 
82802f3bb75SThomas Zimmermann static void
82902f3bb75SThomas Zimmermann ast_cursor_plane_helper_atomic_update(struct drm_plane *plane,
830977697e2SMaxime Ripard 				      struct drm_atomic_state *state)
83102f3bb75SThomas Zimmermann {
832afee7e95SThomas Zimmermann 	struct ast_cursor_plane *ast_cursor_plane = to_ast_cursor_plane(plane);
833977697e2SMaxime Ripard 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
834977697e2SMaxime Ripard 									   plane);
83537418bf1SMaxime Ripard 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
83637418bf1SMaxime Ripard 									   plane);
83741016fe1SMaxime Ripard 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(new_state);
83841016fe1SMaxime Ripard 	struct drm_framebuffer *fb = new_state->fb;
839365c0e70SThomas Zimmermann 	struct ast_private *ast = to_ast_private(plane->dev);
8407938f421SLucas De Marchi 	struct iosys_map dst_map =
8414d36cf07SThomas Zimmermann 		ast_cursor_plane->hwc[ast_cursor_plane->next_hwc_index].map;
842385131f3SThomas Zimmermann 	u64 dst_off =
843385131f3SThomas Zimmermann 		ast_cursor_plane->hwc[ast_cursor_plane->next_hwc_index].off;
8447938f421SLucas De Marchi 	struct iosys_map src_map = shadow_plane_state->data[0];
84581039adcSThomas Zimmermann 	unsigned int offset_x, offset_y;
846718c2286SThomas Zimmermann 	u16 x, y;
847718c2286SThomas Zimmermann 	u8 x_offset, y_offset;
848718c2286SThomas Zimmermann 	u8 __iomem *dst;
849718c2286SThomas Zimmermann 	u8 __iomem *sig;
8504d36cf07SThomas Zimmermann 	const u8 *src;
851718c2286SThomas Zimmermann 
8524d36cf07SThomas Zimmermann 	src = src_map.vaddr; /* TODO: Use mapping abstraction properly */
8534d36cf07SThomas Zimmermann 	dst = dst_map.vaddr_iomem; /* TODO: Use mapping abstraction properly */
8544d36cf07SThomas Zimmermann 	sig = dst + AST_HWC_SIZE; /* TODO: Use mapping abstraction properly */
8554d36cf07SThomas Zimmermann 
8564d36cf07SThomas Zimmermann 	/*
8574d36cf07SThomas Zimmermann 	 * Do data transfer to HW cursor BO. If a new cursor image was installed,
8584d36cf07SThomas Zimmermann 	 * point the scanout engine to dst_gbo's offset and page-flip the HWC buffers.
8594d36cf07SThomas Zimmermann 	 */
8604d36cf07SThomas Zimmermann 
8614d36cf07SThomas Zimmermann 	ast_update_cursor_image(dst, src, fb->width, fb->height);
862718c2286SThomas Zimmermann 
86341016fe1SMaxime Ripard 	if (new_state->fb != old_state->fb) {
864385131f3SThomas Zimmermann 		ast_set_cursor_base(ast, dst_off);
865718c2286SThomas Zimmermann 
866afee7e95SThomas Zimmermann 		++ast_cursor_plane->next_hwc_index;
867afee7e95SThomas Zimmermann 		ast_cursor_plane->next_hwc_index %= ARRAY_SIZE(ast_cursor_plane->hwc);
868718c2286SThomas Zimmermann 	}
869718c2286SThomas Zimmermann 
8704d36cf07SThomas Zimmermann 	/*
8714d36cf07SThomas Zimmermann 	 * Update location in HWC signature and registers.
8724d36cf07SThomas Zimmermann 	 */
873718c2286SThomas Zimmermann 
87441016fe1SMaxime Ripard 	writel(new_state->crtc_x, sig + AST_HWC_SIGNATURE_X);
87541016fe1SMaxime Ripard 	writel(new_state->crtc_y, sig + AST_HWC_SIGNATURE_Y);
876718c2286SThomas Zimmermann 
87781039adcSThomas Zimmermann 	offset_x = AST_MAX_HWC_WIDTH - fb->width;
878ee4a92d6SThomas Zimmermann 	offset_y = AST_MAX_HWC_HEIGHT - fb->height;
87902f3bb75SThomas Zimmermann 
88041016fe1SMaxime Ripard 	if (new_state->crtc_x < 0) {
88141016fe1SMaxime Ripard 		x_offset = (-new_state->crtc_x) + offset_x;
882718c2286SThomas Zimmermann 		x = 0;
883718c2286SThomas Zimmermann 	} else {
884718c2286SThomas Zimmermann 		x_offset = offset_x;
88541016fe1SMaxime Ripard 		x = new_state->crtc_x;
886718c2286SThomas Zimmermann 	}
88741016fe1SMaxime Ripard 	if (new_state->crtc_y < 0) {
88841016fe1SMaxime Ripard 		y_offset = (-new_state->crtc_y) + offset_y;
889718c2286SThomas Zimmermann 		y = 0;
890718c2286SThomas Zimmermann 	} else {
891718c2286SThomas Zimmermann 		y_offset = offset_y;
89241016fe1SMaxime Ripard 		y = new_state->crtc_y;
89302f3bb75SThomas Zimmermann 	}
89402f3bb75SThomas Zimmermann 
895718c2286SThomas Zimmermann 	ast_set_cursor_location(ast, x, y, x_offset, y_offset);
896718c2286SThomas Zimmermann 
8974d36cf07SThomas Zimmermann 	/* Dummy write to enable HWC and make the HW pick-up the changes. */
898718c2286SThomas Zimmermann 	ast_set_cursor_enabled(ast, true);
89902f3bb75SThomas Zimmermann }
90002f3bb75SThomas Zimmermann 
90102f3bb75SThomas Zimmermann static void
90202f3bb75SThomas Zimmermann ast_cursor_plane_helper_atomic_disable(struct drm_plane *plane,
903977697e2SMaxime Ripard 				       struct drm_atomic_state *state)
90402f3bb75SThomas Zimmermann {
905fa7dbd76SThomas Zimmermann 	struct ast_private *ast = to_ast_private(plane->dev);
90602f3bb75SThomas Zimmermann 
907718c2286SThomas Zimmermann 	ast_set_cursor_enabled(ast, false);
90802f3bb75SThomas Zimmermann }
90902f3bb75SThomas Zimmermann 
91002f3bb75SThomas Zimmermann static const struct drm_plane_helper_funcs ast_cursor_plane_helper_funcs = {
9114d36cf07SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
91202f3bb75SThomas Zimmermann 	.atomic_check = ast_cursor_plane_helper_atomic_check,
91302f3bb75SThomas Zimmermann 	.atomic_update = ast_cursor_plane_helper_atomic_update,
91402f3bb75SThomas Zimmermann 	.atomic_disable = ast_cursor_plane_helper_atomic_disable,
91502f3bb75SThomas Zimmermann };
91602f3bb75SThomas Zimmermann 
91722b6591fSThomas Zimmermann static void ast_cursor_plane_destroy(struct drm_plane *plane)
91822b6591fSThomas Zimmermann {
919afee7e95SThomas Zimmermann 	struct ast_cursor_plane *ast_cursor_plane = to_ast_cursor_plane(plane);
92022b6591fSThomas Zimmermann 	size_t i;
92122b6591fSThomas Zimmermann 	struct drm_gem_vram_object *gbo;
9227938f421SLucas De Marchi 	struct iosys_map map;
92322b6591fSThomas Zimmermann 
924afee7e95SThomas Zimmermann 	for (i = 0; i < ARRAY_SIZE(ast_cursor_plane->hwc); ++i) {
925afee7e95SThomas Zimmermann 		gbo = ast_cursor_plane->hwc[i].gbo;
92684810d6aSThomas Zimmermann 		map = ast_cursor_plane->hwc[i].map;
92784810d6aSThomas Zimmermann 		drm_gem_vram_vunmap(gbo, &map);
92822b6591fSThomas Zimmermann 		drm_gem_vram_unpin(gbo);
92922b6591fSThomas Zimmermann 		drm_gem_vram_put(gbo);
93022b6591fSThomas Zimmermann 	}
93122b6591fSThomas Zimmermann 
93222b6591fSThomas Zimmermann 	drm_plane_cleanup(plane);
93322b6591fSThomas Zimmermann }
93422b6591fSThomas Zimmermann 
93502f3bb75SThomas Zimmermann static const struct drm_plane_funcs ast_cursor_plane_funcs = {
93602f3bb75SThomas Zimmermann 	.update_plane = drm_atomic_helper_update_plane,
93702f3bb75SThomas Zimmermann 	.disable_plane = drm_atomic_helper_disable_plane,
93822b6591fSThomas Zimmermann 	.destroy = ast_cursor_plane_destroy,
9394d36cf07SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_FUNCS,
94002f3bb75SThomas Zimmermann };
94102f3bb75SThomas Zimmermann 
942616048afSThomas Zimmermann static int ast_cursor_plane_init(struct ast_private *ast)
943616048afSThomas Zimmermann {
944616048afSThomas Zimmermann 	struct drm_device *dev = &ast->base;
945a0ba992dSThomas Zimmermann 	struct ast_cursor_plane *ast_cursor_plane = &ast->cursor_plane;
946a0ba992dSThomas Zimmermann 	struct drm_plane *cursor_plane = &ast_cursor_plane->base;
94722b6591fSThomas Zimmermann 	size_t size, i;
94822b6591fSThomas Zimmermann 	struct drm_gem_vram_object *gbo;
9497938f421SLucas De Marchi 	struct iosys_map map;
950616048afSThomas Zimmermann 	int ret;
951385131f3SThomas Zimmermann 	s64 off;
952616048afSThomas Zimmermann 
95322b6591fSThomas Zimmermann 	/*
95422b6591fSThomas Zimmermann 	 * Allocate backing storage for cursors. The BOs are permanently
95522b6591fSThomas Zimmermann 	 * pinned to the top end of the VRAM.
95622b6591fSThomas Zimmermann 	 */
95722b6591fSThomas Zimmermann 
95822b6591fSThomas Zimmermann 	size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
95922b6591fSThomas Zimmermann 
960afee7e95SThomas Zimmermann 	for (i = 0; i < ARRAY_SIZE(ast_cursor_plane->hwc); ++i) {
96122b6591fSThomas Zimmermann 		gbo = drm_gem_vram_create(dev, size, 0);
96222b6591fSThomas Zimmermann 		if (IS_ERR(gbo)) {
96322b6591fSThomas Zimmermann 			ret = PTR_ERR(gbo);
96422b6591fSThomas Zimmermann 			goto err_hwc;
96522b6591fSThomas Zimmermann 		}
96622b6591fSThomas Zimmermann 		ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM |
96722b6591fSThomas Zimmermann 					    DRM_GEM_VRAM_PL_FLAG_TOPDOWN);
96822b6591fSThomas Zimmermann 		if (ret)
96922b6591fSThomas Zimmermann 			goto err_drm_gem_vram_put;
97084810d6aSThomas Zimmermann 		ret = drm_gem_vram_vmap(gbo, &map);
97184810d6aSThomas Zimmermann 		if (ret)
97284810d6aSThomas Zimmermann 			goto err_drm_gem_vram_unpin;
973385131f3SThomas Zimmermann 		off = drm_gem_vram_offset(gbo);
974385131f3SThomas Zimmermann 		if (off < 0) {
975385131f3SThomas Zimmermann 			ret = off;
976385131f3SThomas Zimmermann 			goto err_drm_gem_vram_vunmap;
977385131f3SThomas Zimmermann 		}
978afee7e95SThomas Zimmermann 		ast_cursor_plane->hwc[i].gbo = gbo;
97984810d6aSThomas Zimmermann 		ast_cursor_plane->hwc[i].map = map;
980385131f3SThomas Zimmermann 		ast_cursor_plane->hwc[i].off = off;
98122b6591fSThomas Zimmermann 	}
98222b6591fSThomas Zimmermann 
98322b6591fSThomas Zimmermann 	/*
98422b6591fSThomas Zimmermann 	 * Create the cursor plane. The plane's destroy callback will release
98522b6591fSThomas Zimmermann 	 * the backing storages' BO memory.
98622b6591fSThomas Zimmermann 	 */
98722b6591fSThomas Zimmermann 
988616048afSThomas Zimmermann 	ret = drm_universal_plane_init(dev, cursor_plane, 0x01,
989616048afSThomas Zimmermann 				       &ast_cursor_plane_funcs,
990616048afSThomas Zimmermann 				       ast_cursor_plane_formats,
991616048afSThomas Zimmermann 				       ARRAY_SIZE(ast_cursor_plane_formats),
992616048afSThomas Zimmermann 				       NULL, DRM_PLANE_TYPE_CURSOR, NULL);
993616048afSThomas Zimmermann 	if (ret) {
99422b6591fSThomas Zimmermann 		drm_err(dev, "drm_universal_plane failed(): %d\n", ret);
99522b6591fSThomas Zimmermann 		goto err_hwc;
996616048afSThomas Zimmermann 	}
997616048afSThomas Zimmermann 	drm_plane_helper_add(cursor_plane, &ast_cursor_plane_helper_funcs);
998616048afSThomas Zimmermann 
999616048afSThomas Zimmermann 	return 0;
100022b6591fSThomas Zimmermann 
100122b6591fSThomas Zimmermann err_hwc:
100222b6591fSThomas Zimmermann 	while (i) {
100322b6591fSThomas Zimmermann 		--i;
1004afee7e95SThomas Zimmermann 		gbo = ast_cursor_plane->hwc[i].gbo;
100584810d6aSThomas Zimmermann 		map = ast_cursor_plane->hwc[i].map;
1006385131f3SThomas Zimmermann err_drm_gem_vram_vunmap:
100784810d6aSThomas Zimmermann 		drm_gem_vram_vunmap(gbo, &map);
100884810d6aSThomas Zimmermann err_drm_gem_vram_unpin:
100922b6591fSThomas Zimmermann 		drm_gem_vram_unpin(gbo);
101022b6591fSThomas Zimmermann err_drm_gem_vram_put:
101122b6591fSThomas Zimmermann 		drm_gem_vram_put(gbo);
101222b6591fSThomas Zimmermann 	}
101322b6591fSThomas Zimmermann 	return ret;
1014616048afSThomas Zimmermann }
1015616048afSThomas Zimmermann 
101602f3bb75SThomas Zimmermann /*
1017a6ff807bSThomas Zimmermann  * CRTC
1018a6ff807bSThomas Zimmermann  */
1019a6ff807bSThomas Zimmermann 
1020312fec14SDave Airlie static void ast_crtc_dpms(struct drm_crtc *crtc, int mode)
1021312fec14SDave Airlie {
1022fa7dbd76SThomas Zimmermann 	struct ast_private *ast = to_ast_private(crtc->dev);
1023594e9c04SKuoHsiang Chou 	u8 ch = AST_DPMS_VSYNC_OFF | AST_DPMS_HSYNC_OFF;
10245171660cSJocelyn Falempe 	struct ast_crtc_state *ast_state;
10255171660cSJocelyn Falempe 	const struct drm_format_info *format;
10265171660cSJocelyn Falempe 	struct ast_vbios_mode_info *vbios_mode_info;
1027312fec14SDave Airlie 
10282fbeec03SThomas Zimmermann 	/* TODO: Maybe control display signal generation with
10292fbeec03SThomas Zimmermann 	 *       Sync Enable (bit CR17.7).
10302fbeec03SThomas Zimmermann 	 */
1031312fec14SDave Airlie 	switch (mode) {
1032312fec14SDave Airlie 	case DRM_MODE_DPMS_ON:
1033594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT,  0x01, 0xdf, 0);
1034594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0xfc, 0);
10357f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_DP501_BIT)
103683c6620bSDave Airlie 			ast_set_dp501_video_output(crtc->dev, 1);
1037594e9c04SKuoHsiang Chou 
10387f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
1039594e9c04SKuoHsiang Chou 			ast_dp_power_on_off(crtc->dev, AST_DP_POWER_ON);
1040594e9c04SKuoHsiang Chou 			ast_wait_for_vretrace(ast);
1041594e9c04SKuoHsiang Chou 			ast_dp_set_on_off(crtc->dev, 1);
1042594e9c04SKuoHsiang Chou 		}
1043594e9c04SKuoHsiang Chou 
10445171660cSJocelyn Falempe 		ast_state = to_ast_crtc_state(crtc->state);
10455171660cSJocelyn Falempe 		format = ast_state->format;
10465171660cSJocelyn Falempe 
10475171660cSJocelyn Falempe 		if (format) {
10485171660cSJocelyn Falempe 			vbios_mode_info = &ast_state->vbios_mode_info;
10495171660cSJocelyn Falempe 
10505171660cSJocelyn Falempe 			ast_set_color_reg(ast, format);
10515171660cSJocelyn Falempe 			ast_set_vbios_color_reg(ast, format, vbios_mode_info);
1052ce7fcf70SJocelyn Falempe 			if (crtc->state->gamma_lut)
1053ce7fcf70SJocelyn Falempe 				ast_crtc_set_gamma(ast, format, crtc->state->gamma_lut->data);
1054ce7fcf70SJocelyn Falempe 			else
1055ce7fcf70SJocelyn Falempe 				ast_crtc_set_gamma_linear(ast, format);
10565171660cSJocelyn Falempe 		}
1057312fec14SDave Airlie 		break;
1058594e9c04SKuoHsiang Chou 	case DRM_MODE_DPMS_STANDBY:
1059594e9c04SKuoHsiang Chou 	case DRM_MODE_DPMS_SUSPEND:
1060312fec14SDave Airlie 	case DRM_MODE_DPMS_OFF:
1061594e9c04SKuoHsiang Chou 		ch = mode;
10627f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_DP501_BIT)
106383c6620bSDave Airlie 			ast_set_dp501_video_output(crtc->dev, 0);
1064594e9c04SKuoHsiang Chou 
10657f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
1066594e9c04SKuoHsiang Chou 			ast_dp_set_on_off(crtc->dev, 0);
1067594e9c04SKuoHsiang Chou 			ast_dp_power_on_off(crtc->dev, AST_DP_POWER_OFF);
1068594e9c04SKuoHsiang Chou 		}
1069594e9c04SKuoHsiang Chou 
1070594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT,  0x01, 0xdf, 0x20);
1071594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0xfc, ch);
10727f35680aSThomas Zimmermann 		break;
1073312fec14SDave Airlie 	}
1074312fec14SDave Airlie }
1075312fec14SDave Airlie 
10766abbad2cSThomas Zimmermann static enum drm_mode_status
10776abbad2cSThomas Zimmermann ast_crtc_helper_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode)
10786abbad2cSThomas Zimmermann {
10796abbad2cSThomas Zimmermann 	struct ast_private *ast = to_ast_private(crtc->dev);
10806abbad2cSThomas Zimmermann 	enum drm_mode_status status;
10816abbad2cSThomas Zimmermann 	uint32_t jtemp;
10826abbad2cSThomas Zimmermann 
10836abbad2cSThomas Zimmermann 	if (ast->support_wide_screen) {
10846abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
10856abbad2cSThomas Zimmermann 			return MODE_OK;
10866abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
10876abbad2cSThomas Zimmermann 			return MODE_OK;
10886abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
10896abbad2cSThomas Zimmermann 			return MODE_OK;
10906abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
10916abbad2cSThomas Zimmermann 			return MODE_OK;
10926abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
10936abbad2cSThomas Zimmermann 			return MODE_OK;
109471dee036SJammy Huang 		if ((mode->hdisplay == 1152) && (mode->vdisplay == 864))
109571dee036SJammy Huang 			return MODE_OK;
10966abbad2cSThomas Zimmermann 
10976abbad2cSThomas Zimmermann 		if ((ast->chip == AST2100) || (ast->chip == AST2200) ||
10986abbad2cSThomas Zimmermann 		    (ast->chip == AST2300) || (ast->chip == AST2400) ||
1099594e9c04SKuoHsiang Chou 		    (ast->chip == AST2500) || (ast->chip == AST2600)) {
11006abbad2cSThomas Zimmermann 			if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
11016abbad2cSThomas Zimmermann 				return MODE_OK;
11026abbad2cSThomas Zimmermann 
11036abbad2cSThomas Zimmermann 			if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
11046abbad2cSThomas Zimmermann 				jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
11056abbad2cSThomas Zimmermann 				if (jtemp & 0x01)
11066abbad2cSThomas Zimmermann 					return MODE_NOMODE;
11076abbad2cSThomas Zimmermann 				else
11086abbad2cSThomas Zimmermann 					return MODE_OK;
11096abbad2cSThomas Zimmermann 			}
11106abbad2cSThomas Zimmermann 		}
11116abbad2cSThomas Zimmermann 	}
11126abbad2cSThomas Zimmermann 
11136abbad2cSThomas Zimmermann 	status = MODE_NOMODE;
11146abbad2cSThomas Zimmermann 
11156abbad2cSThomas Zimmermann 	switch (mode->hdisplay) {
11166abbad2cSThomas Zimmermann 	case 640:
11176abbad2cSThomas Zimmermann 		if (mode->vdisplay == 480)
11186abbad2cSThomas Zimmermann 			status = MODE_OK;
11196abbad2cSThomas Zimmermann 		break;
11206abbad2cSThomas Zimmermann 	case 800:
11216abbad2cSThomas Zimmermann 		if (mode->vdisplay == 600)
11226abbad2cSThomas Zimmermann 			status = MODE_OK;
11236abbad2cSThomas Zimmermann 		break;
11246abbad2cSThomas Zimmermann 	case 1024:
11256abbad2cSThomas Zimmermann 		if (mode->vdisplay == 768)
11266abbad2cSThomas Zimmermann 			status = MODE_OK;
11276abbad2cSThomas Zimmermann 		break;
112871dee036SJammy Huang 	case 1152:
112971dee036SJammy Huang 		if (mode->vdisplay == 864)
113071dee036SJammy Huang 			status = MODE_OK;
113171dee036SJammy Huang 		break;
11326abbad2cSThomas Zimmermann 	case 1280:
11336abbad2cSThomas Zimmermann 		if (mode->vdisplay == 1024)
11346abbad2cSThomas Zimmermann 			status = MODE_OK;
11356abbad2cSThomas Zimmermann 		break;
11366abbad2cSThomas Zimmermann 	case 1600:
11376abbad2cSThomas Zimmermann 		if (mode->vdisplay == 1200)
11386abbad2cSThomas Zimmermann 			status = MODE_OK;
11396abbad2cSThomas Zimmermann 		break;
11406abbad2cSThomas Zimmermann 	default:
11416abbad2cSThomas Zimmermann 		break;
11426abbad2cSThomas Zimmermann 	}
11436abbad2cSThomas Zimmermann 
11446abbad2cSThomas Zimmermann 	return status;
11456abbad2cSThomas Zimmermann }
11466abbad2cSThomas Zimmermann 
1147b48e1b6fSThomas Zimmermann static int ast_crtc_helper_atomic_check(struct drm_crtc *crtc,
114829b77ad7SMaxime Ripard 					struct drm_atomic_state *state)
1149b48e1b6fSThomas Zimmermann {
1150016a14beSThomas Zimmermann 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1151ce7fcf70SJocelyn Falempe 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
1152ce7fcf70SJocelyn Falempe 	struct ast_crtc_state *old_ast_crtc_state = to_ast_crtc_state(old_crtc_state);
11535638c82cSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
1154e7d70cd4SThomas Zimmermann 	struct ast_crtc_state *ast_state;
11553339fdf5SThomas Zimmermann 	const struct drm_format_info *format;
1156b48e1b6fSThomas Zimmermann 	bool succ;
1157016a14beSThomas Zimmermann 	int ret;
1158016a14beSThomas Zimmermann 
115929b77ad7SMaxime Ripard 	if (!crtc_state->enable)
1160*963a2ba2SThomas Zimmermann 		return 0;
1161d6ddbd5cSThomas Zimmermann 
11628f2fd57dSThomas Zimmermann 	ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state);
11638f2fd57dSThomas Zimmermann 	if (ret)
11648f2fd57dSThomas Zimmermann 		return ret;
11658f2fd57dSThomas Zimmermann 
116629b77ad7SMaxime Ripard 	ast_state = to_ast_crtc_state(crtc_state);
1167b48e1b6fSThomas Zimmermann 
11683339fdf5SThomas Zimmermann 	format = ast_state->format;
11695638c82cSThomas Zimmermann 	if (drm_WARN_ON_ONCE(dev, !format))
11705638c82cSThomas Zimmermann 		return -EINVAL; /* BUG: We didn't set format in primary check(). */
1171e7d70cd4SThomas Zimmermann 
1172ce7fcf70SJocelyn Falempe 	/*
1173ce7fcf70SJocelyn Falempe 	 * The gamma LUT has to be reloaded after changing the primary
1174ce7fcf70SJocelyn Falempe 	 * plane's color format.
1175ce7fcf70SJocelyn Falempe 	 */
1176ce7fcf70SJocelyn Falempe 	if (old_ast_crtc_state->format != format)
1177ce7fcf70SJocelyn Falempe 		crtc_state->color_mgmt_changed = true;
1178ce7fcf70SJocelyn Falempe 
1179ce7fcf70SJocelyn Falempe 	if (crtc_state->color_mgmt_changed && crtc_state->gamma_lut) {
1180ce7fcf70SJocelyn Falempe 		if (crtc_state->gamma_lut->length !=
1181ce7fcf70SJocelyn Falempe 		    AST_LUT_SIZE * sizeof(struct drm_color_lut)) {
1182ce7fcf70SJocelyn Falempe 			drm_err(dev, "Wrong size for gamma_lut %zu\n",
1183ce7fcf70SJocelyn Falempe 				crtc_state->gamma_lut->length);
1184ce7fcf70SJocelyn Falempe 			return -EINVAL;
1185ce7fcf70SJocelyn Falempe 		}
1186ce7fcf70SJocelyn Falempe 	}
1187ce7fcf70SJocelyn Falempe 
118829b77ad7SMaxime Ripard 	succ = ast_get_vbios_mode_info(format, &crtc_state->mode,
118929b77ad7SMaxime Ripard 				       &crtc_state->adjusted_mode,
1190e7d70cd4SThomas Zimmermann 				       &ast_state->vbios_mode_info);
1191b48e1b6fSThomas Zimmermann 	if (!succ)
1192b48e1b6fSThomas Zimmermann 		return -EINVAL;
1193b48e1b6fSThomas Zimmermann 
1194*963a2ba2SThomas Zimmermann 	return 0;
1195b48e1b6fSThomas Zimmermann }
1196b48e1b6fSThomas Zimmermann 
1197f3901b5fSThomas Zimmermann static void
1198f6ebe9f9SMaxime Ripard ast_crtc_helper_atomic_flush(struct drm_crtc *crtc,
1199f6ebe9f9SMaxime Ripard 			     struct drm_atomic_state *state)
12008e3784dfSThomas Zimmermann {
1201253f28b6SMaxime Ripard 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
1202253f28b6SMaxime Ripard 									  crtc);
1203f870231fSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
1204f870231fSThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
1205253f28b6SMaxime Ripard 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
1206594e9c04SKuoHsiang Chou 	struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
12078e3784dfSThomas Zimmermann 
12088e3784dfSThomas Zimmermann 	/*
12098e3784dfSThomas Zimmermann 	 * The gamma LUT has to be reloaded after changing the primary
12108e3784dfSThomas Zimmermann 	 * plane's color format.
12118e3784dfSThomas Zimmermann 	 */
1212ce7fcf70SJocelyn Falempe 	if (crtc_state->enable && crtc_state->color_mgmt_changed) {
1213ce7fcf70SJocelyn Falempe 		if (crtc_state->gamma_lut)
1214ce7fcf70SJocelyn Falempe 			ast_crtc_set_gamma(ast,
1215ce7fcf70SJocelyn Falempe 					   ast_crtc_state->format,
1216ce7fcf70SJocelyn Falempe 					   crtc_state->gamma_lut->data);
1217ce7fcf70SJocelyn Falempe 		else
1218ce7fcf70SJocelyn Falempe 			ast_crtc_set_gamma_linear(ast, ast_crtc_state->format);
1219ce7fcf70SJocelyn Falempe 	}
1220594e9c04SKuoHsiang Chou 
1221594e9c04SKuoHsiang Chou 	//Set Aspeed Display-Port
12227f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_ASTDP_BIT)
1223594e9c04SKuoHsiang Chou 		ast_dp_set_mode(crtc, vbios_mode_info);
12248e3784dfSThomas Zimmermann }
12258e3784dfSThomas Zimmermann 
12268e3784dfSThomas Zimmermann static void
1227f3901b5fSThomas Zimmermann ast_crtc_helper_atomic_enable(struct drm_crtc *crtc,
1228351f950dSMaxime Ripard 			      struct drm_atomic_state *state)
1229b48e1b6fSThomas Zimmermann {
123071d873ccSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
1231fa7dbd76SThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
12325638c82cSThomas Zimmermann 	struct drm_crtc_state *crtc_state = crtc->state;
12335638c82cSThomas Zimmermann 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
12345638c82cSThomas Zimmermann 	struct ast_vbios_mode_info *vbios_mode_info =
12355638c82cSThomas Zimmermann 		&ast_crtc_state->vbios_mode_info;
12365638c82cSThomas Zimmermann 	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
1237b48e1b6fSThomas Zimmermann 
1238e7d70cd4SThomas Zimmermann 	ast_set_vbios_mode_reg(ast, adjusted_mode, vbios_mode_info);
1239b48e1b6fSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa1, 0x06);
1240e7d70cd4SThomas Zimmermann 	ast_set_std_reg(ast, adjusted_mode, vbios_mode_info);
1241e7d70cd4SThomas Zimmermann 	ast_set_crtc_reg(ast, adjusted_mode, vbios_mode_info);
1242e7d70cd4SThomas Zimmermann 	ast_set_dclk_reg(ast, adjusted_mode, vbios_mode_info);
1243ae37025dSThomas Zimmermann 	ast_set_crtthd_reg(ast);
1244e7d70cd4SThomas Zimmermann 	ast_set_sync_reg(ast, adjusted_mode, vbios_mode_info);
1245b48e1b6fSThomas Zimmermann 
1246b48e1b6fSThomas Zimmermann 	ast_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
1247b48e1b6fSThomas Zimmermann }
1248b48e1b6fSThomas Zimmermann 
1249b48e1b6fSThomas Zimmermann static void
1250b48e1b6fSThomas Zimmermann ast_crtc_helper_atomic_disable(struct drm_crtc *crtc,
1251351f950dSMaxime Ripard 			       struct drm_atomic_state *state)
1252b48e1b6fSThomas Zimmermann {
1253351f950dSMaxime Ripard 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state,
1254351f950dSMaxime Ripard 									      crtc);
125539edb287SThomas Zimmermann 	struct drm_device *dev = crtc->dev;
125639edb287SThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
125739edb287SThomas Zimmermann 
1258b48e1b6fSThomas Zimmermann 	ast_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
125939edb287SThomas Zimmermann 
126039edb287SThomas Zimmermann 	/*
126139edb287SThomas Zimmermann 	 * HW cursors require the underlying primary plane and CRTC to
126239edb287SThomas Zimmermann 	 * display a valid mode and image. This is not the case during
126339edb287SThomas Zimmermann 	 * full modeset operations. So we temporarily disable any active
126439edb287SThomas Zimmermann 	 * plane, including the HW cursor. Each plane's atomic_update()
126539edb287SThomas Zimmermann 	 * helper will re-enable it if necessary.
126639edb287SThomas Zimmermann 	 *
126739edb287SThomas Zimmermann 	 * We only do this during *full* modesets. It does not affect
126839edb287SThomas Zimmermann 	 * simple pageflips on the planes.
126939edb287SThomas Zimmermann 	 */
127039edb287SThomas Zimmermann 	drm_atomic_helper_disable_planes_on_crtc(old_crtc_state, false);
127139edb287SThomas Zimmermann 
127239edb287SThomas Zimmermann 	/*
127339edb287SThomas Zimmermann 	 * Ensure that no scanout takes place before reprogramming mode
127439edb287SThomas Zimmermann 	 * and format registers.
127539edb287SThomas Zimmermann 	 */
127639edb287SThomas Zimmermann 	ast_wait_for_vretrace(ast);
1277b48e1b6fSThomas Zimmermann }
1278312fec14SDave Airlie 
1279312fec14SDave Airlie static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
12806abbad2cSThomas Zimmermann 	.mode_valid = ast_crtc_helper_mode_valid,
1281b48e1b6fSThomas Zimmermann 	.atomic_check = ast_crtc_helper_atomic_check,
12828e3784dfSThomas Zimmermann 	.atomic_flush = ast_crtc_helper_atomic_flush,
1283b48e1b6fSThomas Zimmermann 	.atomic_enable = ast_crtc_helper_atomic_enable,
1284b48e1b6fSThomas Zimmermann 	.atomic_disable = ast_crtc_helper_atomic_disable,
1285312fec14SDave Airlie };
1286312fec14SDave Airlie 
1287f0adbc38SThomas Zimmermann static void ast_crtc_reset(struct drm_crtc *crtc)
1288f0adbc38SThomas Zimmermann {
1289f0adbc38SThomas Zimmermann 	struct ast_crtc_state *ast_state =
1290f0adbc38SThomas Zimmermann 		kzalloc(sizeof(*ast_state), GFP_KERNEL);
1291f0adbc38SThomas Zimmermann 
1292f0adbc38SThomas Zimmermann 	if (crtc->state)
1293f0adbc38SThomas Zimmermann 		crtc->funcs->atomic_destroy_state(crtc, crtc->state);
1294f0adbc38SThomas Zimmermann 
1295fea3fdf9SJiasheng Jiang 	if (ast_state)
1296f0adbc38SThomas Zimmermann 		__drm_atomic_helper_crtc_reset(crtc, &ast_state->base);
1297fea3fdf9SJiasheng Jiang 	else
1298fea3fdf9SJiasheng Jiang 		__drm_atomic_helper_crtc_reset(crtc, NULL);
1299f0adbc38SThomas Zimmermann }
1300f0adbc38SThomas Zimmermann 
130183be6a3cSThomas Zimmermann static struct drm_crtc_state *
130283be6a3cSThomas Zimmermann ast_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
130383be6a3cSThomas Zimmermann {
1304e7d70cd4SThomas Zimmermann 	struct ast_crtc_state *new_ast_state, *ast_state;
13051a19b4cbSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
130683be6a3cSThomas Zimmermann 
13071a19b4cbSThomas Zimmermann 	if (drm_WARN_ON(dev, !crtc->state))
130883be6a3cSThomas Zimmermann 		return NULL;
130983be6a3cSThomas Zimmermann 
131083be6a3cSThomas Zimmermann 	new_ast_state = kmalloc(sizeof(*new_ast_state), GFP_KERNEL);
131183be6a3cSThomas Zimmermann 	if (!new_ast_state)
131283be6a3cSThomas Zimmermann 		return NULL;
131383be6a3cSThomas Zimmermann 	__drm_atomic_helper_crtc_duplicate_state(crtc, &new_ast_state->base);
131483be6a3cSThomas Zimmermann 
1315e7d70cd4SThomas Zimmermann 	ast_state = to_ast_crtc_state(crtc->state);
1316e7d70cd4SThomas Zimmermann 
13173339fdf5SThomas Zimmermann 	new_ast_state->format = ast_state->format;
1318e7d70cd4SThomas Zimmermann 	memcpy(&new_ast_state->vbios_mode_info, &ast_state->vbios_mode_info,
1319e7d70cd4SThomas Zimmermann 	       sizeof(new_ast_state->vbios_mode_info));
1320e7d70cd4SThomas Zimmermann 
132183be6a3cSThomas Zimmermann 	return &new_ast_state->base;
132283be6a3cSThomas Zimmermann }
132383be6a3cSThomas Zimmermann 
132483be6a3cSThomas Zimmermann static void ast_crtc_atomic_destroy_state(struct drm_crtc *crtc,
132583be6a3cSThomas Zimmermann 					  struct drm_crtc_state *state)
132683be6a3cSThomas Zimmermann {
132783be6a3cSThomas Zimmermann 	struct ast_crtc_state *ast_state = to_ast_crtc_state(state);
132883be6a3cSThomas Zimmermann 
132983be6a3cSThomas Zimmermann 	__drm_atomic_helper_crtc_destroy_state(&ast_state->base);
133083be6a3cSThomas Zimmermann 	kfree(ast_state);
133183be6a3cSThomas Zimmermann }
133283be6a3cSThomas Zimmermann 
1333312fec14SDave Airlie static const struct drm_crtc_funcs ast_crtc_funcs = {
1334f0adbc38SThomas Zimmermann 	.reset = ast_crtc_reset,
13356a470dc2SThomas Zimmermann 	.destroy = drm_crtc_cleanup,
13364961eb60SThomas Zimmermann 	.set_config = drm_atomic_helper_set_config,
13374961eb60SThomas Zimmermann 	.page_flip = drm_atomic_helper_page_flip,
133883be6a3cSThomas Zimmermann 	.atomic_duplicate_state = ast_crtc_atomic_duplicate_state,
133983be6a3cSThomas Zimmermann 	.atomic_destroy_state = ast_crtc_atomic_destroy_state,
1340312fec14SDave Airlie };
1341312fec14SDave Airlie 
13427f5ccd44SRashika static int ast_crtc_init(struct drm_device *dev)
1343312fec14SDave Airlie {
1344fa7dbd76SThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
13456a470dc2SThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
1346a6ff807bSThomas Zimmermann 	int ret;
1347312fec14SDave Airlie 
1348c35da0edSThomas Zimmermann 	ret = drm_crtc_init_with_planes(dev, crtc, &ast->primary_plane,
1349a0ba992dSThomas Zimmermann 					&ast->cursor_plane.base, &ast_crtc_funcs,
135002f3bb75SThomas Zimmermann 					NULL);
1351a6ff807bSThomas Zimmermann 	if (ret)
13526a470dc2SThomas Zimmermann 		return ret;
1353a6ff807bSThomas Zimmermann 
1354ce7fcf70SJocelyn Falempe 	drm_mode_crtc_set_gamma_size(crtc, AST_LUT_SIZE);
1355ce7fcf70SJocelyn Falempe 	drm_crtc_enable_color_mgmt(crtc, 0, false, AST_LUT_SIZE);
1356ce7fcf70SJocelyn Falempe 
1357c35da0edSThomas Zimmermann 	drm_crtc_helper_add(crtc, &ast_crtc_helper_funcs);
1358c35da0edSThomas Zimmermann 
1359312fec14SDave Airlie 	return 0;
1360312fec14SDave Airlie }
1361312fec14SDave Airlie 
13624961eb60SThomas Zimmermann /*
1363b20384d9SThomas Zimmermann  * VGA Connector
13644961eb60SThomas Zimmermann  */
13654961eb60SThomas Zimmermann 
1366b20384d9SThomas Zimmermann static int ast_vga_connector_helper_get_modes(struct drm_connector *connector)
1367312fec14SDave Airlie {
1368b20384d9SThomas Zimmermann 	struct ast_vga_connector *ast_vga_connector = to_ast_vga_connector(connector);
1369f870231fSThomas Zimmermann 	struct drm_device *dev = connector->dev;
1370f870231fSThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
13713ab26eddSThomas Zimmermann 	struct edid *edid;
13723ab26eddSThomas Zimmermann 	int count;
1373312fec14SDave Airlie 
13743ab26eddSThomas Zimmermann 	if (!ast_vga_connector->i2c)
13753ab26eddSThomas Zimmermann 		goto err_drm_connector_update_edid_property;
1376312fec14SDave Airlie 
1377f870231fSThomas Zimmermann 	/*
1378f870231fSThomas Zimmermann 	 * Protect access to I/O registers from concurrent modesetting
1379f870231fSThomas Zimmermann 	 * by acquiring the I/O-register lock.
1380f870231fSThomas Zimmermann 	 */
1381f870231fSThomas Zimmermann 	mutex_lock(&ast->ioregs_lock);
1382f870231fSThomas Zimmermann 
1383b20384d9SThomas Zimmermann 	edid = drm_get_edid(connector, &ast_vga_connector->i2c->adapter);
138483c6620bSDave Airlie 	if (!edid)
1385f870231fSThomas Zimmermann 		goto err_mutex_unlock;
1386f870231fSThomas Zimmermann 
1387f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
1388312fec14SDave Airlie 
13893ab26eddSThomas Zimmermann 	count = drm_add_edid_modes(connector, edid);
139083c6620bSDave Airlie 	kfree(edid);
13913ab26eddSThomas Zimmermann 
13923ab26eddSThomas Zimmermann 	return count;
13933ab26eddSThomas Zimmermann 
1394f870231fSThomas Zimmermann err_mutex_unlock:
1395f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
13963ab26eddSThomas Zimmermann err_drm_connector_update_edid_property:
1397b20384d9SThomas Zimmermann 	drm_connector_update_edid_property(connector, NULL);
1398312fec14SDave Airlie 	return 0;
1399312fec14SDave Airlie }
1400312fec14SDave Airlie 
1401b20384d9SThomas Zimmermann static const struct drm_connector_helper_funcs ast_vga_connector_helper_funcs = {
1402b20384d9SThomas Zimmermann 	.get_modes = ast_vga_connector_helper_get_modes,
1403312fec14SDave Airlie };
1404312fec14SDave Airlie 
1405b20384d9SThomas Zimmermann static const struct drm_connector_funcs ast_vga_connector_funcs = {
14064961eb60SThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
1407312fec14SDave Airlie 	.fill_modes = drm_helper_probe_single_connector_modes,
1408a2cce09cSThomas Zimmermann 	.destroy = drm_connector_cleanup,
14094961eb60SThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
14104961eb60SThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1411312fec14SDave Airlie };
1412312fec14SDave Airlie 
1413a59b0264SThomas Zimmermann static int ast_vga_connector_init(struct drm_device *dev,
1414a59b0264SThomas Zimmermann 				  struct ast_vga_connector *ast_vga_connector)
1415312fec14SDave Airlie {
1416b20384d9SThomas Zimmermann 	struct drm_connector *connector = &ast_vga_connector->base;
14179285f09eSThomas Zimmermann 	int ret;
1418312fec14SDave Airlie 
1419b20384d9SThomas Zimmermann 	ast_vga_connector->i2c = ast_i2c_create(dev);
1420b20384d9SThomas Zimmermann 	if (!ast_vga_connector->i2c)
14211a19b4cbSThomas Zimmermann 		drm_err(dev, "failed to add ddc bus for connector\n");
1422350fd554SAndrzej Pietrasiewicz 
1423b20384d9SThomas Zimmermann 	if (ast_vga_connector->i2c)
1424b20384d9SThomas Zimmermann 		ret = drm_connector_init_with_ddc(dev, connector, &ast_vga_connector_funcs,
1425350fd554SAndrzej Pietrasiewicz 						  DRM_MODE_CONNECTOR_VGA,
1426b20384d9SThomas Zimmermann 						  &ast_vga_connector->i2c->adapter);
142755dc449aSThomas Zimmermann 	else
1428b20384d9SThomas Zimmermann 		ret = drm_connector_init(dev, connector, &ast_vga_connector_funcs,
142955dc449aSThomas Zimmermann 					 DRM_MODE_CONNECTOR_VGA);
14309285f09eSThomas Zimmermann 	if (ret)
14319285f09eSThomas Zimmermann 		return ret;
1432312fec14SDave Airlie 
1433b20384d9SThomas Zimmermann 	drm_connector_helper_add(connector, &ast_vga_connector_helper_funcs);
1434312fec14SDave Airlie 
1435312fec14SDave Airlie 	connector->interlace_allowed = 0;
1436312fec14SDave Airlie 	connector->doublescan_allowed = 0;
1437312fec14SDave Airlie 
1438595cb5e0SKim Phillips 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1439312fec14SDave Airlie 
1440a59b0264SThomas Zimmermann 	return 0;
1441a59b0264SThomas Zimmermann }
1442a59b0264SThomas Zimmermann 
1443a59b0264SThomas Zimmermann static int ast_vga_output_init(struct ast_private *ast)
1444a59b0264SThomas Zimmermann {
1445a59b0264SThomas Zimmermann 	struct drm_device *dev = &ast->base;
1446a59b0264SThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
1447a59b0264SThomas Zimmermann 	struct drm_encoder *encoder = &ast->output.vga.encoder;
1448a59b0264SThomas Zimmermann 	struct ast_vga_connector *ast_vga_connector = &ast->output.vga.vga_connector;
1449a59b0264SThomas Zimmermann 	struct drm_connector *connector = &ast_vga_connector->base;
1450a59b0264SThomas Zimmermann 	int ret;
1451a59b0264SThomas Zimmermann 
1452a59b0264SThomas Zimmermann 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_DAC);
1453a59b0264SThomas Zimmermann 	if (ret)
1454a59b0264SThomas Zimmermann 		return ret;
1455f665147cSThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1456a59b0264SThomas Zimmermann 
1457a59b0264SThomas Zimmermann 	ret = ast_vga_connector_init(dev, ast_vga_connector);
1458a59b0264SThomas Zimmermann 	if (ret)
1459a59b0264SThomas Zimmermann 		return ret;
1460a59b0264SThomas Zimmermann 
1461a59b0264SThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
1462a59b0264SThomas Zimmermann 	if (ret)
1463a59b0264SThomas Zimmermann 		return ret;
1464312fec14SDave Airlie 
1465312fec14SDave Airlie 	return 0;
1466312fec14SDave Airlie }
1467312fec14SDave Airlie 
1468e6949ff3SThomas Zimmermann /*
14695e78d59aSThomas Zimmermann  * SIL164 Connector
14705e78d59aSThomas Zimmermann  */
14715e78d59aSThomas Zimmermann 
14725e78d59aSThomas Zimmermann static int ast_sil164_connector_helper_get_modes(struct drm_connector *connector)
14735e78d59aSThomas Zimmermann {
14745e78d59aSThomas Zimmermann 	struct ast_sil164_connector *ast_sil164_connector = to_ast_sil164_connector(connector);
1475f870231fSThomas Zimmermann 	struct drm_device *dev = connector->dev;
1476f870231fSThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
14775e78d59aSThomas Zimmermann 	struct edid *edid;
14785e78d59aSThomas Zimmermann 	int count;
14795e78d59aSThomas Zimmermann 
14805e78d59aSThomas Zimmermann 	if (!ast_sil164_connector->i2c)
14815e78d59aSThomas Zimmermann 		goto err_drm_connector_update_edid_property;
14825e78d59aSThomas Zimmermann 
1483f870231fSThomas Zimmermann 	/*
1484f870231fSThomas Zimmermann 	 * Protect access to I/O registers from concurrent modesetting
1485f870231fSThomas Zimmermann 	 * by acquiring the I/O-register lock.
1486f870231fSThomas Zimmermann 	 */
1487f870231fSThomas Zimmermann 	mutex_lock(&ast->ioregs_lock);
1488f870231fSThomas Zimmermann 
14895e78d59aSThomas Zimmermann 	edid = drm_get_edid(connector, &ast_sil164_connector->i2c->adapter);
14905e78d59aSThomas Zimmermann 	if (!edid)
1491f870231fSThomas Zimmermann 		goto err_mutex_unlock;
1492f870231fSThomas Zimmermann 
1493f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
14945e78d59aSThomas Zimmermann 
14955e78d59aSThomas Zimmermann 	count = drm_add_edid_modes(connector, edid);
14965e78d59aSThomas Zimmermann 	kfree(edid);
14975e78d59aSThomas Zimmermann 
14985e78d59aSThomas Zimmermann 	return count;
14995e78d59aSThomas Zimmermann 
1500f870231fSThomas Zimmermann err_mutex_unlock:
1501f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
15025e78d59aSThomas Zimmermann err_drm_connector_update_edid_property:
15035e78d59aSThomas Zimmermann 	drm_connector_update_edid_property(connector, NULL);
15045e78d59aSThomas Zimmermann 	return 0;
15055e78d59aSThomas Zimmermann }
15065e78d59aSThomas Zimmermann 
15075e78d59aSThomas Zimmermann static const struct drm_connector_helper_funcs ast_sil164_connector_helper_funcs = {
15085e78d59aSThomas Zimmermann 	.get_modes = ast_sil164_connector_helper_get_modes,
15095e78d59aSThomas Zimmermann };
15105e78d59aSThomas Zimmermann 
15115e78d59aSThomas Zimmermann static const struct drm_connector_funcs ast_sil164_connector_funcs = {
15125e78d59aSThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
15135e78d59aSThomas Zimmermann 	.fill_modes = drm_helper_probe_single_connector_modes,
15145e78d59aSThomas Zimmermann 	.destroy = drm_connector_cleanup,
15155e78d59aSThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
15165e78d59aSThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
15175e78d59aSThomas Zimmermann };
15185e78d59aSThomas Zimmermann 
15195e78d59aSThomas Zimmermann static int ast_sil164_connector_init(struct drm_device *dev,
15205e78d59aSThomas Zimmermann 				     struct ast_sil164_connector *ast_sil164_connector)
15215e78d59aSThomas Zimmermann {
15225e78d59aSThomas Zimmermann 	struct drm_connector *connector = &ast_sil164_connector->base;
15235e78d59aSThomas Zimmermann 	int ret;
15245e78d59aSThomas Zimmermann 
15255e78d59aSThomas Zimmermann 	ast_sil164_connector->i2c = ast_i2c_create(dev);
15265e78d59aSThomas Zimmermann 	if (!ast_sil164_connector->i2c)
15275e78d59aSThomas Zimmermann 		drm_err(dev, "failed to add ddc bus for connector\n");
15285e78d59aSThomas Zimmermann 
15295e78d59aSThomas Zimmermann 	if (ast_sil164_connector->i2c)
15305e78d59aSThomas Zimmermann 		ret = drm_connector_init_with_ddc(dev, connector, &ast_sil164_connector_funcs,
15315e78d59aSThomas Zimmermann 						  DRM_MODE_CONNECTOR_DVII,
15325e78d59aSThomas Zimmermann 						  &ast_sil164_connector->i2c->adapter);
15335e78d59aSThomas Zimmermann 	else
15345e78d59aSThomas Zimmermann 		ret = drm_connector_init(dev, connector, &ast_sil164_connector_funcs,
15355e78d59aSThomas Zimmermann 					 DRM_MODE_CONNECTOR_DVII);
15365e78d59aSThomas Zimmermann 	if (ret)
15375e78d59aSThomas Zimmermann 		return ret;
15385e78d59aSThomas Zimmermann 
15395e78d59aSThomas Zimmermann 	drm_connector_helper_add(connector, &ast_sil164_connector_helper_funcs);
15405e78d59aSThomas Zimmermann 
15415e78d59aSThomas Zimmermann 	connector->interlace_allowed = 0;
15425e78d59aSThomas Zimmermann 	connector->doublescan_allowed = 0;
15435e78d59aSThomas Zimmermann 
15445e78d59aSThomas Zimmermann 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
15455e78d59aSThomas Zimmermann 
15465e78d59aSThomas Zimmermann 	return 0;
15475e78d59aSThomas Zimmermann }
15485e78d59aSThomas Zimmermann 
15495e78d59aSThomas Zimmermann static int ast_sil164_output_init(struct ast_private *ast)
15505e78d59aSThomas Zimmermann {
15515e78d59aSThomas Zimmermann 	struct drm_device *dev = &ast->base;
15525e78d59aSThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
15535e78d59aSThomas Zimmermann 	struct drm_encoder *encoder = &ast->output.sil164.encoder;
15545e78d59aSThomas Zimmermann 	struct ast_sil164_connector *ast_sil164_connector = &ast->output.sil164.sil164_connector;
15555e78d59aSThomas Zimmermann 	struct drm_connector *connector = &ast_sil164_connector->base;
15565e78d59aSThomas Zimmermann 	int ret;
15575e78d59aSThomas Zimmermann 
15585e78d59aSThomas Zimmermann 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
15595e78d59aSThomas Zimmermann 	if (ret)
15605e78d59aSThomas Zimmermann 		return ret;
15615e78d59aSThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
15625e78d59aSThomas Zimmermann 
15635e78d59aSThomas Zimmermann 	ret = ast_sil164_connector_init(dev, ast_sil164_connector);
15645e78d59aSThomas Zimmermann 	if (ret)
15655e78d59aSThomas Zimmermann 		return ret;
15665e78d59aSThomas Zimmermann 
15675e78d59aSThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
15685e78d59aSThomas Zimmermann 	if (ret)
15695e78d59aSThomas Zimmermann 		return ret;
15705e78d59aSThomas Zimmermann 
15715e78d59aSThomas Zimmermann 	return 0;
15725e78d59aSThomas Zimmermann }
15735e78d59aSThomas Zimmermann 
15745e78d59aSThomas Zimmermann /*
15753ab26eddSThomas Zimmermann  * DP501 Connector
15763ab26eddSThomas Zimmermann  */
15773ab26eddSThomas Zimmermann 
15783ab26eddSThomas Zimmermann static int ast_dp501_connector_helper_get_modes(struct drm_connector *connector)
15793ab26eddSThomas Zimmermann {
15803ab26eddSThomas Zimmermann 	void *edid;
15813ab26eddSThomas Zimmermann 	bool succ;
15823ab26eddSThomas Zimmermann 	int count;
15833ab26eddSThomas Zimmermann 
15843ab26eddSThomas Zimmermann 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
15853ab26eddSThomas Zimmermann 	if (!edid)
15863ab26eddSThomas Zimmermann 		goto err_drm_connector_update_edid_property;
15873ab26eddSThomas Zimmermann 
15883ab26eddSThomas Zimmermann 	succ = ast_dp501_read_edid(connector->dev, edid);
15893ab26eddSThomas Zimmermann 	if (!succ)
15903ab26eddSThomas Zimmermann 		goto err_kfree;
15913ab26eddSThomas Zimmermann 
15923ab26eddSThomas Zimmermann 	drm_connector_update_edid_property(connector, edid);
15933ab26eddSThomas Zimmermann 	count = drm_add_edid_modes(connector, edid);
15943ab26eddSThomas Zimmermann 	kfree(edid);
15953ab26eddSThomas Zimmermann 
15963ab26eddSThomas Zimmermann 	return count;
15973ab26eddSThomas Zimmermann 
15983ab26eddSThomas Zimmermann err_kfree:
15993ab26eddSThomas Zimmermann 	kfree(edid);
16003ab26eddSThomas Zimmermann err_drm_connector_update_edid_property:
16013ab26eddSThomas Zimmermann 	drm_connector_update_edid_property(connector, NULL);
16023ab26eddSThomas Zimmermann 	return 0;
16033ab26eddSThomas Zimmermann }
16043ab26eddSThomas Zimmermann 
16053ab26eddSThomas Zimmermann static const struct drm_connector_helper_funcs ast_dp501_connector_helper_funcs = {
16063ab26eddSThomas Zimmermann 	.get_modes = ast_dp501_connector_helper_get_modes,
16073ab26eddSThomas Zimmermann };
16083ab26eddSThomas Zimmermann 
16093ab26eddSThomas Zimmermann static const struct drm_connector_funcs ast_dp501_connector_funcs = {
16103ab26eddSThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
16113ab26eddSThomas Zimmermann 	.fill_modes = drm_helper_probe_single_connector_modes,
16123ab26eddSThomas Zimmermann 	.destroy = drm_connector_cleanup,
16133ab26eddSThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
16143ab26eddSThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
16153ab26eddSThomas Zimmermann };
16163ab26eddSThomas Zimmermann 
16173ab26eddSThomas Zimmermann static int ast_dp501_connector_init(struct drm_device *dev, struct drm_connector *connector)
16183ab26eddSThomas Zimmermann {
16193ab26eddSThomas Zimmermann 	int ret;
16203ab26eddSThomas Zimmermann 
16213ab26eddSThomas Zimmermann 	ret = drm_connector_init(dev, connector, &ast_dp501_connector_funcs,
16223ab26eddSThomas Zimmermann 				 DRM_MODE_CONNECTOR_DisplayPort);
16233ab26eddSThomas Zimmermann 	if (ret)
16243ab26eddSThomas Zimmermann 		return ret;
16253ab26eddSThomas Zimmermann 
16263ab26eddSThomas Zimmermann 	drm_connector_helper_add(connector, &ast_dp501_connector_helper_funcs);
16273ab26eddSThomas Zimmermann 
16283ab26eddSThomas Zimmermann 	connector->interlace_allowed = 0;
16293ab26eddSThomas Zimmermann 	connector->doublescan_allowed = 0;
16303ab26eddSThomas Zimmermann 
16313ab26eddSThomas Zimmermann 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
16323ab26eddSThomas Zimmermann 
16333ab26eddSThomas Zimmermann 	return 0;
16343ab26eddSThomas Zimmermann }
16353ab26eddSThomas Zimmermann 
16363ab26eddSThomas Zimmermann static int ast_dp501_output_init(struct ast_private *ast)
16373ab26eddSThomas Zimmermann {
16383ab26eddSThomas Zimmermann 	struct drm_device *dev = &ast->base;
16393ab26eddSThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
16403ab26eddSThomas Zimmermann 	struct drm_encoder *encoder = &ast->output.dp501.encoder;
16413ab26eddSThomas Zimmermann 	struct drm_connector *connector = &ast->output.dp501.connector;
16423ab26eddSThomas Zimmermann 	int ret;
16433ab26eddSThomas Zimmermann 
16443ab26eddSThomas Zimmermann 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
16453ab26eddSThomas Zimmermann 	if (ret)
16463ab26eddSThomas Zimmermann 		return ret;
16473ab26eddSThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
16483ab26eddSThomas Zimmermann 
16493ab26eddSThomas Zimmermann 	ret = ast_dp501_connector_init(dev, connector);
16503ab26eddSThomas Zimmermann 	if (ret)
16513ab26eddSThomas Zimmermann 		return ret;
16523ab26eddSThomas Zimmermann 
16533ab26eddSThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
16543ab26eddSThomas Zimmermann 	if (ret)
16553ab26eddSThomas Zimmermann 		return ret;
1656312fec14SDave Airlie 
1657312fec14SDave Airlie 	return 0;
16587f5ccd44SRashika }
1659312fec14SDave Airlie 
1660e6949ff3SThomas Zimmermann /*
1661594e9c04SKuoHsiang Chou  * ASPEED Display-Port Connector
1662594e9c04SKuoHsiang Chou  */
1663594e9c04SKuoHsiang Chou 
1664594e9c04SKuoHsiang Chou static int ast_astdp_connector_helper_get_modes(struct drm_connector *connector)
1665594e9c04SKuoHsiang Chou {
1666594e9c04SKuoHsiang Chou 	void *edid;
1667594e9c04SKuoHsiang Chou 
1668594e9c04SKuoHsiang Chou 	int succ;
1669594e9c04SKuoHsiang Chou 	int count;
1670594e9c04SKuoHsiang Chou 
1671594e9c04SKuoHsiang Chou 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1672594e9c04SKuoHsiang Chou 	if (!edid)
1673594e9c04SKuoHsiang Chou 		goto err_drm_connector_update_edid_property;
1674594e9c04SKuoHsiang Chou 
1675594e9c04SKuoHsiang Chou 	succ = ast_astdp_read_edid(connector->dev, edid);
1676594e9c04SKuoHsiang Chou 	if (succ < 0)
1677594e9c04SKuoHsiang Chou 		goto err_kfree;
1678594e9c04SKuoHsiang Chou 
1679594e9c04SKuoHsiang Chou 	drm_connector_update_edid_property(connector, edid);
1680594e9c04SKuoHsiang Chou 	count = drm_add_edid_modes(connector, edid);
1681594e9c04SKuoHsiang Chou 	kfree(edid);
1682594e9c04SKuoHsiang Chou 
1683594e9c04SKuoHsiang Chou 	return count;
1684594e9c04SKuoHsiang Chou 
1685594e9c04SKuoHsiang Chou err_kfree:
1686594e9c04SKuoHsiang Chou 	kfree(edid);
1687594e9c04SKuoHsiang Chou err_drm_connector_update_edid_property:
1688594e9c04SKuoHsiang Chou 	drm_connector_update_edid_property(connector, NULL);
1689594e9c04SKuoHsiang Chou 	return 0;
1690594e9c04SKuoHsiang Chou }
1691594e9c04SKuoHsiang Chou 
1692594e9c04SKuoHsiang Chou static const struct drm_connector_helper_funcs ast_astdp_connector_helper_funcs = {
1693594e9c04SKuoHsiang Chou 	.get_modes = ast_astdp_connector_helper_get_modes,
1694594e9c04SKuoHsiang Chou };
1695594e9c04SKuoHsiang Chou 
1696594e9c04SKuoHsiang Chou static const struct drm_connector_funcs ast_astdp_connector_funcs = {
1697594e9c04SKuoHsiang Chou 	.reset = drm_atomic_helper_connector_reset,
1698594e9c04SKuoHsiang Chou 	.fill_modes = drm_helper_probe_single_connector_modes,
1699594e9c04SKuoHsiang Chou 	.destroy = drm_connector_cleanup,
1700594e9c04SKuoHsiang Chou 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1701594e9c04SKuoHsiang Chou 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1702594e9c04SKuoHsiang Chou };
1703594e9c04SKuoHsiang Chou 
1704594e9c04SKuoHsiang Chou static int ast_astdp_connector_init(struct drm_device *dev, struct drm_connector *connector)
1705594e9c04SKuoHsiang Chou {
1706594e9c04SKuoHsiang Chou 	int ret;
1707594e9c04SKuoHsiang Chou 
1708594e9c04SKuoHsiang Chou 	ret = drm_connector_init(dev, connector, &ast_astdp_connector_funcs,
1709594e9c04SKuoHsiang Chou 				 DRM_MODE_CONNECTOR_DisplayPort);
1710594e9c04SKuoHsiang Chou 	if (ret)
1711594e9c04SKuoHsiang Chou 		return ret;
1712594e9c04SKuoHsiang Chou 
1713594e9c04SKuoHsiang Chou 	drm_connector_helper_add(connector, &ast_astdp_connector_helper_funcs);
1714594e9c04SKuoHsiang Chou 
1715594e9c04SKuoHsiang Chou 	connector->interlace_allowed = 0;
1716594e9c04SKuoHsiang Chou 	connector->doublescan_allowed = 0;
1717594e9c04SKuoHsiang Chou 
1718594e9c04SKuoHsiang Chou 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1719594e9c04SKuoHsiang Chou 
1720594e9c04SKuoHsiang Chou 	return 0;
1721594e9c04SKuoHsiang Chou }
1722594e9c04SKuoHsiang Chou 
1723594e9c04SKuoHsiang Chou static int ast_astdp_output_init(struct ast_private *ast)
1724594e9c04SKuoHsiang Chou {
1725594e9c04SKuoHsiang Chou 	struct drm_device *dev = &ast->base;
1726594e9c04SKuoHsiang Chou 	struct drm_crtc *crtc = &ast->crtc;
1727594e9c04SKuoHsiang Chou 	struct drm_encoder *encoder = &ast->output.astdp.encoder;
1728594e9c04SKuoHsiang Chou 	struct drm_connector *connector = &ast->output.astdp.connector;
1729594e9c04SKuoHsiang Chou 	int ret;
1730594e9c04SKuoHsiang Chou 
1731594e9c04SKuoHsiang Chou 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
1732594e9c04SKuoHsiang Chou 	if (ret)
1733594e9c04SKuoHsiang Chou 		return ret;
1734594e9c04SKuoHsiang Chou 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1735594e9c04SKuoHsiang Chou 
1736594e9c04SKuoHsiang Chou 	ret = ast_astdp_connector_init(dev, connector);
1737594e9c04SKuoHsiang Chou 	if (ret)
1738594e9c04SKuoHsiang Chou 		return ret;
1739594e9c04SKuoHsiang Chou 
1740594e9c04SKuoHsiang Chou 	ret = drm_connector_attach_encoder(connector, encoder);
1741594e9c04SKuoHsiang Chou 	if (ret)
1742594e9c04SKuoHsiang Chou 		return ret;
1743594e9c04SKuoHsiang Chou 
1744594e9c04SKuoHsiang Chou 	return 0;
1745594e9c04SKuoHsiang Chou }
1746594e9c04SKuoHsiang Chou 
1747594e9c04SKuoHsiang Chou /*
1748e6949ff3SThomas Zimmermann  * Mode config
1749e6949ff3SThomas Zimmermann  */
1750e6949ff3SThomas Zimmermann 
17511fe18215SThomas Zimmermann static void ast_mode_config_helper_atomic_commit_tail(struct drm_atomic_state *state)
17521fe18215SThomas Zimmermann {
17531fe18215SThomas Zimmermann 	struct ast_private *ast = to_ast_private(state->dev);
17541fe18215SThomas Zimmermann 
17551fe18215SThomas Zimmermann 	/*
17561fe18215SThomas Zimmermann 	 * Concurrent operations could possibly trigger a call to
17571fe18215SThomas Zimmermann 	 * drm_connector_helper_funcs.get_modes by trying to read the
17581fe18215SThomas Zimmermann 	 * display modes. Protect access to I/O registers by acquiring
17591fe18215SThomas Zimmermann 	 * the I/O-register lock. Released in atomic_flush().
17601fe18215SThomas Zimmermann 	 */
17611fe18215SThomas Zimmermann 	mutex_lock(&ast->ioregs_lock);
17621fe18215SThomas Zimmermann 	drm_atomic_helper_commit_tail_rpm(state);
17631fe18215SThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
17641fe18215SThomas Zimmermann }
17651fe18215SThomas Zimmermann 
1766b20384d9SThomas Zimmermann static const struct drm_mode_config_helper_funcs ast_mode_config_helper_funcs = {
17671fe18215SThomas Zimmermann 	.atomic_commit_tail = ast_mode_config_helper_atomic_commit_tail,
17682f0ddd89SThomas Zimmermann };
17692f0ddd89SThomas Zimmermann 
1770e6949ff3SThomas Zimmermann static const struct drm_mode_config_funcs ast_mode_config_funcs = {
1771e6949ff3SThomas Zimmermann 	.fb_create = drm_gem_fb_create,
1772e6949ff3SThomas Zimmermann 	.mode_valid = drm_vram_helper_mode_valid,
1773e6949ff3SThomas Zimmermann 	.atomic_check = drm_atomic_helper_check,
1774e6949ff3SThomas Zimmermann 	.atomic_commit = drm_atomic_helper_commit,
1775e6949ff3SThomas Zimmermann };
1776e6949ff3SThomas Zimmermann 
1777e6949ff3SThomas Zimmermann int ast_mode_config_init(struct ast_private *ast)
1778312fec14SDave Airlie {
1779e0f5a738SThomas Zimmermann 	struct drm_device *dev = &ast->base;
178046fb883cSThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(dev->dev);
1781a6ff807bSThomas Zimmermann 	int ret;
1782a6ff807bSThomas Zimmermann 
1783e6949ff3SThomas Zimmermann 	ret = drmm_mode_config_init(dev);
1784e6949ff3SThomas Zimmermann 	if (ret)
1785e6949ff3SThomas Zimmermann 		return ret;
1786e6949ff3SThomas Zimmermann 
1787e6949ff3SThomas Zimmermann 	dev->mode_config.funcs = &ast_mode_config_funcs;
1788e6949ff3SThomas Zimmermann 	dev->mode_config.min_width = 0;
1789e6949ff3SThomas Zimmermann 	dev->mode_config.min_height = 0;
1790e6949ff3SThomas Zimmermann 	dev->mode_config.preferred_depth = 24;
1791e6949ff3SThomas Zimmermann 	dev->mode_config.prefer_shadow = 1;
179246fb883cSThomas Zimmermann 	dev->mode_config.fb_base = pci_resource_start(pdev, 0);
1793e6949ff3SThomas Zimmermann 
1794e6949ff3SThomas Zimmermann 	if (ast->chip == AST2100 ||
1795e6949ff3SThomas Zimmermann 	    ast->chip == AST2200 ||
1796e6949ff3SThomas Zimmermann 	    ast->chip == AST2300 ||
1797e6949ff3SThomas Zimmermann 	    ast->chip == AST2400 ||
1798594e9c04SKuoHsiang Chou 	    ast->chip == AST2500 ||
1799594e9c04SKuoHsiang Chou 	    ast->chip == AST2600) {
1800e6949ff3SThomas Zimmermann 		dev->mode_config.max_width = 1920;
1801e6949ff3SThomas Zimmermann 		dev->mode_config.max_height = 2048;
1802e6949ff3SThomas Zimmermann 	} else {
1803e6949ff3SThomas Zimmermann 		dev->mode_config.max_width = 1600;
1804e6949ff3SThomas Zimmermann 		dev->mode_config.max_height = 1200;
1805e6949ff3SThomas Zimmermann 	}
1806e6949ff3SThomas Zimmermann 
18072f0ddd89SThomas Zimmermann 	dev->mode_config.helper_private = &ast_mode_config_helper_funcs;
18082f0ddd89SThomas Zimmermann 
1809a6ff807bSThomas Zimmermann 
1810616048afSThomas Zimmermann 	ret = ast_primary_plane_init(ast);
1811616048afSThomas Zimmermann 	if (ret)
181202f3bb75SThomas Zimmermann 		return ret;
1813616048afSThomas Zimmermann 
1814616048afSThomas Zimmermann 	ret = ast_cursor_plane_init(ast);
1815616048afSThomas Zimmermann 	if (ret)
1816616048afSThomas Zimmermann 		return ret;
181702f3bb75SThomas Zimmermann 
1818312fec14SDave Airlie 	ast_crtc_init(dev);
1819a59b0264SThomas Zimmermann 
18207f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_NONE_BIT) {
1821a59b0264SThomas Zimmermann 		ret = ast_vga_output_init(ast);
1822a59b0264SThomas Zimmermann 		if (ret)
1823a59b0264SThomas Zimmermann 			return ret;
18247f35680aSThomas Zimmermann 	}
18257f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_SIL164_BIT) {
18267f35680aSThomas Zimmermann 		ret = ast_sil164_output_init(ast);
18277f35680aSThomas Zimmermann 		if (ret)
18287f35680aSThomas Zimmermann 			return ret;
18297f35680aSThomas Zimmermann 	}
18307f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_DP501_BIT) {
18317f35680aSThomas Zimmermann 		ret = ast_dp501_output_init(ast);
18327f35680aSThomas Zimmermann 		if (ret)
18337f35680aSThomas Zimmermann 			return ret;
18347f35680aSThomas Zimmermann 	}
18357f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
18367f35680aSThomas Zimmermann 		ret = ast_astdp_output_init(ast);
18377f35680aSThomas Zimmermann 		if (ret)
18387f35680aSThomas Zimmermann 			return ret;
18397f35680aSThomas Zimmermann 	}
1840a6ff807bSThomas Zimmermann 
1841e6949ff3SThomas Zimmermann 	drm_mode_config_reset(dev);
1842e6949ff3SThomas Zimmermann 
1843312fec14SDave Airlie 	return 0;
1844312fec14SDave Airlie }
1845