xref: /openbmc/linux/drivers/gpu/drm/ast/ast_mode.c (revision 8f85e4a1)
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>
38f2fa5a99SThomas Zimmermann #include <drm/drm_damage_helper.h>
39255490f9SVille Syrjälä #include <drm/drm_edid.h>
40f2fa5a99SThomas Zimmermann #include <drm/drm_format_helper.h>
41fbbbd160SSam Ravnborg #include <drm/drm_fourcc.h>
424d36cf07SThomas Zimmermann #include <drm/drm_gem_atomic_helper.h>
43e6949ff3SThomas Zimmermann #include <drm/drm_gem_framebuffer_helper.h>
44f2fa5a99SThomas Zimmermann #include <drm/drm_gem_shmem_helper.h>
453ab26eddSThomas Zimmermann #include <drm/drm_managed.h>
46fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
474220fdf0SThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
48312fec14SDave Airlie 
49fbbbd160SSam Ravnborg #include "ast_drv.h"
50312fec14SDave Airlie #include "ast_tables.h"
51312fec14SDave Airlie 
52ce7fcf70SJocelyn Falempe #define AST_LUT_SIZE 256
53ce7fcf70SJocelyn Falempe 
54312fec14SDave Airlie static inline void ast_load_palette_index(struct ast_private *ast,
55312fec14SDave Airlie 				     u8 index, u8 red, u8 green,
56312fec14SDave Airlie 				     u8 blue)
57312fec14SDave Airlie {
58312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_DAC_INDEX_WRITE, index);
59312fec14SDave Airlie 	ast_io_read8(ast, AST_IO_SEQ_PORT);
60312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_DAC_DATA, red);
61312fec14SDave Airlie 	ast_io_read8(ast, AST_IO_SEQ_PORT);
62312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_DAC_DATA, green);
63312fec14SDave Airlie 	ast_io_read8(ast, AST_IO_SEQ_PORT);
64312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_DAC_DATA, blue);
65312fec14SDave Airlie 	ast_io_read8(ast, AST_IO_SEQ_PORT);
66312fec14SDave Airlie }
67312fec14SDave Airlie 
68ce7fcf70SJocelyn Falempe static void ast_crtc_set_gamma_linear(struct ast_private *ast,
69ce7fcf70SJocelyn Falempe 				      const struct drm_format_info *format)
70312fec14SDave Airlie {
71312fec14SDave Airlie 	int i;
72312fec14SDave Airlie 
73ce7fcf70SJocelyn Falempe 	switch (format->format) {
74ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_C8: /* In this case, gamma table is used as color palette */
75ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_RGB565:
76ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_XRGB8888:
77ce7fcf70SJocelyn Falempe 		for (i = 0; i < AST_LUT_SIZE; i++)
78ce7fcf70SJocelyn Falempe 			ast_load_palette_index(ast, i, i, i, i);
79ce7fcf70SJocelyn Falempe 		break;
80ce7fcf70SJocelyn Falempe 	default:
81ce7fcf70SJocelyn Falempe 		drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n",
82ce7fcf70SJocelyn Falempe 			      &format->format);
83ce7fcf70SJocelyn Falempe 		break;
84ce7fcf70SJocelyn Falempe 	}
85ce7fcf70SJocelyn Falempe }
86312fec14SDave Airlie 
87ce7fcf70SJocelyn Falempe static void ast_crtc_set_gamma(struct ast_private *ast,
88ce7fcf70SJocelyn Falempe 			       const struct drm_format_info *format,
89ce7fcf70SJocelyn Falempe 			       struct drm_color_lut *lut)
90ce7fcf70SJocelyn Falempe {
91ce7fcf70SJocelyn Falempe 	int i;
923bffd962SPeter Rosin 
93ce7fcf70SJocelyn Falempe 	switch (format->format) {
94ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_C8: /* In this case, gamma table is used as color palette */
95ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_RGB565:
96ce7fcf70SJocelyn Falempe 	case DRM_FORMAT_XRGB8888:
97ce7fcf70SJocelyn Falempe 		for (i = 0; i < AST_LUT_SIZE; i++)
98ce7fcf70SJocelyn Falempe 			ast_load_palette_index(ast, i,
99ce7fcf70SJocelyn Falempe 					       lut[i].red >> 8,
100ce7fcf70SJocelyn Falempe 					       lut[i].green >> 8,
101ce7fcf70SJocelyn Falempe 					       lut[i].blue >> 8);
102ce7fcf70SJocelyn Falempe 		break;
103ce7fcf70SJocelyn Falempe 	default:
104ce7fcf70SJocelyn Falempe 		drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n",
105ce7fcf70SJocelyn Falempe 			      &format->format);
106ce7fcf70SJocelyn Falempe 		break;
107ce7fcf70SJocelyn Falempe 	}
108312fec14SDave Airlie }
109312fec14SDave Airlie 
110ae37025dSThomas Zimmermann static bool ast_get_vbios_mode_info(const struct drm_format_info *format,
111259d14a7SThomas Zimmermann 				    const struct drm_display_mode *mode,
112312fec14SDave Airlie 				    struct drm_display_mode *adjusted_mode,
113312fec14SDave Airlie 				    struct ast_vbios_mode_info *vbios_mode)
114312fec14SDave Airlie {
115259d14a7SThomas Zimmermann 	u32 refresh_rate_index = 0, refresh_rate;
11622acdbb1SBenjamin Herrenschmidt 	const struct ast_vbios_enhtable *best = NULL;
117312fec14SDave Airlie 	u32 hborder, vborder;
11894d12b13SY.C. Chen 	bool check_sync;
119312fec14SDave Airlie 
120ae37025dSThomas Zimmermann 	switch (format->cpp[0] * 8) {
121312fec14SDave Airlie 	case 8:
122312fec14SDave Airlie 		vbios_mode->std_table = &vbios_stdtable[VGAModeIndex];
123312fec14SDave Airlie 		break;
124312fec14SDave Airlie 	case 16:
125312fec14SDave Airlie 		vbios_mode->std_table = &vbios_stdtable[HiCModeIndex];
126312fec14SDave Airlie 		break;
127312fec14SDave Airlie 	case 24:
128312fec14SDave Airlie 	case 32:
129312fec14SDave Airlie 		vbios_mode->std_table = &vbios_stdtable[TrueCModeIndex];
130312fec14SDave Airlie 		break;
131312fec14SDave Airlie 	default:
132312fec14SDave Airlie 		return false;
133312fec14SDave Airlie 	}
134312fec14SDave Airlie 
135259d14a7SThomas Zimmermann 	switch (mode->crtc_hdisplay) {
136312fec14SDave Airlie 	case 640:
137312fec14SDave Airlie 		vbios_mode->enh_table = &res_640x480[refresh_rate_index];
138312fec14SDave Airlie 		break;
139312fec14SDave Airlie 	case 800:
140312fec14SDave Airlie 		vbios_mode->enh_table = &res_800x600[refresh_rate_index];
141312fec14SDave Airlie 		break;
142312fec14SDave Airlie 	case 1024:
143312fec14SDave Airlie 		vbios_mode->enh_table = &res_1024x768[refresh_rate_index];
144312fec14SDave Airlie 		break;
14571dee036SJammy Huang 	case 1152:
14671dee036SJammy Huang 		vbios_mode->enh_table = &res_1152x864[refresh_rate_index];
14771dee036SJammy Huang 		break;
148312fec14SDave Airlie 	case 1280:
149259d14a7SThomas Zimmermann 		if (mode->crtc_vdisplay == 800)
150312fec14SDave Airlie 			vbios_mode->enh_table = &res_1280x800[refresh_rate_index];
151312fec14SDave Airlie 		else
152312fec14SDave Airlie 			vbios_mode->enh_table = &res_1280x1024[refresh_rate_index];
153312fec14SDave Airlie 		break;
154f1f62f2cSDave Airlie 	case 1360:
155f1f62f2cSDave Airlie 		vbios_mode->enh_table = &res_1360x768[refresh_rate_index];
156f1f62f2cSDave Airlie 		break;
157312fec14SDave Airlie 	case 1440:
158312fec14SDave Airlie 		vbios_mode->enh_table = &res_1440x900[refresh_rate_index];
159312fec14SDave Airlie 		break;
160312fec14SDave Airlie 	case 1600:
161259d14a7SThomas Zimmermann 		if (mode->crtc_vdisplay == 900)
162f1f62f2cSDave Airlie 			vbios_mode->enh_table = &res_1600x900[refresh_rate_index];
163f1f62f2cSDave Airlie 		else
164312fec14SDave Airlie 			vbios_mode->enh_table = &res_1600x1200[refresh_rate_index];
165312fec14SDave Airlie 		break;
166312fec14SDave Airlie 	case 1680:
167312fec14SDave Airlie 		vbios_mode->enh_table = &res_1680x1050[refresh_rate_index];
168312fec14SDave Airlie 		break;
169312fec14SDave Airlie 	case 1920:
170259d14a7SThomas Zimmermann 		if (mode->crtc_vdisplay == 1080)
171312fec14SDave Airlie 			vbios_mode->enh_table = &res_1920x1080[refresh_rate_index];
172312fec14SDave Airlie 		else
173312fec14SDave Airlie 			vbios_mode->enh_table = &res_1920x1200[refresh_rate_index];
174312fec14SDave Airlie 		break;
175312fec14SDave Airlie 	default:
176312fec14SDave Airlie 		return false;
177312fec14SDave Airlie 	}
178312fec14SDave Airlie 
179312fec14SDave Airlie 	refresh_rate = drm_mode_vrefresh(mode);
18094d12b13SY.C. Chen 	check_sync = vbios_mode->enh_table->flags & WideScreenMode;
181259d14a7SThomas Zimmermann 
182259d14a7SThomas Zimmermann 	while (1) {
18322acdbb1SBenjamin Herrenschmidt 		const struct ast_vbios_enhtable *loop = vbios_mode->enh_table;
18494d12b13SY.C. Chen 
18594d12b13SY.C. Chen 		while (loop->refresh_rate != 0xff) {
18694d12b13SY.C. Chen 			if ((check_sync) &&
18794d12b13SY.C. Chen 			    (((mode->flags & DRM_MODE_FLAG_NVSYNC)  &&
18894d12b13SY.C. Chen 			      (loop->flags & PVSync))  ||
18994d12b13SY.C. Chen 			     ((mode->flags & DRM_MODE_FLAG_PVSYNC)  &&
19094d12b13SY.C. Chen 			      (loop->flags & NVSync))  ||
19194d12b13SY.C. Chen 			     ((mode->flags & DRM_MODE_FLAG_NHSYNC)  &&
19294d12b13SY.C. Chen 			      (loop->flags & PHSync))  ||
19394d12b13SY.C. Chen 			     ((mode->flags & DRM_MODE_FLAG_PHSYNC)  &&
19494d12b13SY.C. Chen 			      (loop->flags & NHSync)))) {
19594d12b13SY.C. Chen 				loop++;
19694d12b13SY.C. Chen 				continue;
19794d12b13SY.C. Chen 			}
19894d12b13SY.C. Chen 			if (loop->refresh_rate <= refresh_rate
19994d12b13SY.C. Chen 			    && (!best || loop->refresh_rate > best->refresh_rate))
20094d12b13SY.C. Chen 				best = loop;
20194d12b13SY.C. Chen 			loop++;
20294d12b13SY.C. Chen 		}
20394d12b13SY.C. Chen 		if (best || !check_sync)
204312fec14SDave Airlie 			break;
20594d12b13SY.C. Chen 		check_sync = 0;
206259d14a7SThomas Zimmermann 	}
207259d14a7SThomas Zimmermann 
20894d12b13SY.C. Chen 	if (best)
20994d12b13SY.C. Chen 		vbios_mode->enh_table = best;
210312fec14SDave Airlie 
211312fec14SDave Airlie 	hborder = (vbios_mode->enh_table->flags & HBorder) ? 8 : 0;
212312fec14SDave Airlie 	vborder = (vbios_mode->enh_table->flags & VBorder) ? 8 : 0;
213312fec14SDave Airlie 
214312fec14SDave Airlie 	adjusted_mode->crtc_htotal = vbios_mode->enh_table->ht;
215312fec14SDave Airlie 	adjusted_mode->crtc_hblank_start = vbios_mode->enh_table->hde + hborder;
216312fec14SDave Airlie 	adjusted_mode->crtc_hblank_end = vbios_mode->enh_table->ht - hborder;
217312fec14SDave Airlie 	adjusted_mode->crtc_hsync_start = vbios_mode->enh_table->hde + hborder +
218312fec14SDave Airlie 		vbios_mode->enh_table->hfp;
219312fec14SDave Airlie 	adjusted_mode->crtc_hsync_end = (vbios_mode->enh_table->hde + hborder +
220312fec14SDave Airlie 					 vbios_mode->enh_table->hfp +
221312fec14SDave Airlie 					 vbios_mode->enh_table->hsync);
222312fec14SDave Airlie 
223312fec14SDave Airlie 	adjusted_mode->crtc_vtotal = vbios_mode->enh_table->vt;
224312fec14SDave Airlie 	adjusted_mode->crtc_vblank_start = vbios_mode->enh_table->vde + vborder;
225312fec14SDave Airlie 	adjusted_mode->crtc_vblank_end = vbios_mode->enh_table->vt - vborder;
226312fec14SDave Airlie 	adjusted_mode->crtc_vsync_start = vbios_mode->enh_table->vde + vborder +
227312fec14SDave Airlie 		vbios_mode->enh_table->vfp;
228312fec14SDave Airlie 	adjusted_mode->crtc_vsync_end = (vbios_mode->enh_table->vde + vborder +
229312fec14SDave Airlie 					 vbios_mode->enh_table->vfp +
230312fec14SDave Airlie 					 vbios_mode->enh_table->vsync);
231312fec14SDave Airlie 
232259d14a7SThomas Zimmermann 	return true;
233259d14a7SThomas Zimmermann }
234259d14a7SThomas Zimmermann 
235ae37025dSThomas Zimmermann static void ast_set_vbios_color_reg(struct ast_private *ast,
236ae37025dSThomas Zimmermann 				    const struct drm_format_info *format,
237259d14a7SThomas Zimmermann 				    const struct ast_vbios_mode_info *vbios_mode)
238259d14a7SThomas Zimmermann {
239259d14a7SThomas Zimmermann 	u32 color_index;
240259d14a7SThomas Zimmermann 
241ae37025dSThomas Zimmermann 	switch (format->cpp[0]) {
242259d14a7SThomas Zimmermann 	case 1:
243259d14a7SThomas Zimmermann 		color_index = VGAModeIndex - 1;
244259d14a7SThomas Zimmermann 		break;
245259d14a7SThomas Zimmermann 	case 2:
246259d14a7SThomas Zimmermann 		color_index = HiCModeIndex;
247259d14a7SThomas Zimmermann 		break;
248259d14a7SThomas Zimmermann 	case 3:
249259d14a7SThomas Zimmermann 	case 4:
250259d14a7SThomas Zimmermann 		color_index = TrueCModeIndex;
251291ddeb6SColin Ian King 		break;
252259d14a7SThomas Zimmermann 	default:
253259d14a7SThomas Zimmermann 		return;
254259d14a7SThomas Zimmermann 	}
255259d14a7SThomas Zimmermann 
256259d14a7SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8c, (u8)((color_index & 0x0f) << 4));
257259d14a7SThomas Zimmermann 
258259d14a7SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0x00);
259259d14a7SThomas Zimmermann 
260259d14a7SThomas Zimmermann 	if (vbios_mode->enh_table->flags & NewModeInfo) {
261259d14a7SThomas Zimmermann 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0xa8);
262ae37025dSThomas Zimmermann 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x92, format->cpp[0] * 8);
263259d14a7SThomas Zimmermann 	}
264259d14a7SThomas Zimmermann }
265259d14a7SThomas Zimmermann 
266ae37025dSThomas Zimmermann static void ast_set_vbios_mode_reg(struct ast_private *ast,
267259d14a7SThomas Zimmermann 				   const struct drm_display_mode *adjusted_mode,
268259d14a7SThomas Zimmermann 				   const struct ast_vbios_mode_info *vbios_mode)
269259d14a7SThomas Zimmermann {
270259d14a7SThomas Zimmermann 	u32 refresh_rate_index, mode_id;
271259d14a7SThomas Zimmermann 
272312fec14SDave Airlie 	refresh_rate_index = vbios_mode->enh_table->refresh_rate_index;
273312fec14SDave Airlie 	mode_id = vbios_mode->enh_table->mode_id;
274312fec14SDave Airlie 
275312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8d, refresh_rate_index & 0xff);
276312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8e, mode_id & 0xff);
277312fec14SDave Airlie 
278f1f62f2cSDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0x00);
279259d14a7SThomas Zimmermann 
280f1f62f2cSDave Airlie 	if (vbios_mode->enh_table->flags & NewModeInfo) {
281312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0xa8);
282312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x93, adjusted_mode->clock / 1000);
283312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x94, adjusted_mode->crtc_hdisplay);
284312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x95, adjusted_mode->crtc_hdisplay >> 8);
285312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x96, adjusted_mode->crtc_vdisplay);
286312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x97, adjusted_mode->crtc_vdisplay >> 8);
287312fec14SDave Airlie 	}
288f1f62f2cSDave Airlie }
289312fec14SDave Airlie 
290ae37025dSThomas Zimmermann static void ast_set_std_reg(struct ast_private *ast,
291ae37025dSThomas Zimmermann 			    struct drm_display_mode *mode,
292312fec14SDave Airlie 			    struct ast_vbios_mode_info *vbios_mode)
293312fec14SDave Airlie {
29422acdbb1SBenjamin Herrenschmidt 	const struct ast_vbios_stdtable *stdtable;
295312fec14SDave Airlie 	u32 i;
296312fec14SDave Airlie 	u8 jreg;
297312fec14SDave Airlie 
298312fec14SDave Airlie 	stdtable = vbios_mode->std_table;
299312fec14SDave Airlie 
300312fec14SDave Airlie 	jreg = stdtable->misc;
301312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
302312fec14SDave Airlie 
3032fbeec03SThomas Zimmermann 	/* Set SEQ; except Screen Disable field */
304312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_SEQ_PORT, 0x00, 0x03);
3052fbeec03SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x01, 0xdf, stdtable->seq[0]);
3062fbeec03SThomas Zimmermann 	for (i = 1; i < 4; i++) {
307312fec14SDave Airlie 		jreg = stdtable->seq[i];
308312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_SEQ_PORT, (i + 1), jreg);
309312fec14SDave Airlie 	}
310312fec14SDave Airlie 
311a21fdd7aSThomas Zimmermann 	/* Set CRTC; except base address and offset */
312312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
313a21fdd7aSThomas Zimmermann 	for (i = 0; i < 12; i++)
314a21fdd7aSThomas Zimmermann 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
315a21fdd7aSThomas Zimmermann 	for (i = 14; i < 19; i++)
316a21fdd7aSThomas Zimmermann 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
317a21fdd7aSThomas Zimmermann 	for (i = 20; i < 25; i++)
318312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
319312fec14SDave Airlie 
320312fec14SDave Airlie 	/* set AR */
321312fec14SDave Airlie 	jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
322312fec14SDave Airlie 	for (i = 0; i < 20; i++) {
323312fec14SDave Airlie 		jreg = stdtable->ar[i];
324312fec14SDave Airlie 		ast_io_write8(ast, AST_IO_AR_PORT_WRITE, (u8)i);
325312fec14SDave Airlie 		ast_io_write8(ast, AST_IO_AR_PORT_WRITE, jreg);
326312fec14SDave Airlie 	}
327312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x14);
328312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x00);
329312fec14SDave Airlie 
330312fec14SDave Airlie 	jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
331312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x20);
332312fec14SDave Airlie 
333312fec14SDave Airlie 	/* Set GR */
334312fec14SDave Airlie 	for (i = 0; i < 9; i++)
335312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_GR_PORT, i, stdtable->gr[i]);
336312fec14SDave Airlie }
337312fec14SDave Airlie 
338ae37025dSThomas Zimmermann static void ast_set_crtc_reg(struct ast_private *ast,
339ae37025dSThomas Zimmermann 			     struct drm_display_mode *mode,
340312fec14SDave Airlie 			     struct ast_vbios_mode_info *vbios_mode)
341312fec14SDave Airlie {
342312fec14SDave Airlie 	u8 jreg05 = 0, jreg07 = 0, jreg09 = 0, jregAC = 0, jregAD = 0, jregAE = 0;
3439f93c8b3SY.C. Chen 	u16 temp, precache = 0;
3449f93c8b3SY.C. Chen 
345d6cbe630SJammy Huang 	if ((ast->chip == AST2500 || ast->chip == AST2600) &&
3469f93c8b3SY.C. Chen 	    (vbios_mode->enh_table->flags & AST2500PreCatchCRT))
3479f93c8b3SY.C. Chen 		precache = 40;
348312fec14SDave Airlie 
349312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
350312fec14SDave Airlie 
351312fec14SDave Airlie 	temp = (mode->crtc_htotal >> 3) - 5;
352312fec14SDave Airlie 	if (temp & 0x100)
353312fec14SDave Airlie 		jregAC |= 0x01; /* HT D[8] */
354312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x00, 0x00, temp);
355312fec14SDave Airlie 
356312fec14SDave Airlie 	temp = (mode->crtc_hdisplay >> 3) - 1;
357312fec14SDave Airlie 	if (temp & 0x100)
358312fec14SDave Airlie 		jregAC |= 0x04; /* HDE D[8] */
359312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x01, 0x00, temp);
360312fec14SDave Airlie 
361312fec14SDave Airlie 	temp = (mode->crtc_hblank_start >> 3) - 1;
362312fec14SDave Airlie 	if (temp & 0x100)
363312fec14SDave Airlie 		jregAC |= 0x10; /* HBS D[8] */
364312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x02, 0x00, temp);
365312fec14SDave Airlie 
366312fec14SDave Airlie 	temp = ((mode->crtc_hblank_end >> 3) - 1) & 0x7f;
367312fec14SDave Airlie 	if (temp & 0x20)
368312fec14SDave Airlie 		jreg05 |= 0x80;  /* HBE D[5] */
369312fec14SDave Airlie 	if (temp & 0x40)
370312fec14SDave Airlie 		jregAD |= 0x01;  /* HBE D[5] */
371312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x03, 0xE0, (temp & 0x1f));
372312fec14SDave Airlie 
3739f93c8b3SY.C. Chen 	temp = ((mode->crtc_hsync_start-precache) >> 3) - 1;
374312fec14SDave Airlie 	if (temp & 0x100)
375312fec14SDave Airlie 		jregAC |= 0x40; /* HRS D[5] */
376312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x04, 0x00, temp);
377312fec14SDave Airlie 
3789f93c8b3SY.C. Chen 	temp = (((mode->crtc_hsync_end-precache) >> 3) - 1) & 0x3f;
379312fec14SDave Airlie 	if (temp & 0x20)
380312fec14SDave Airlie 		jregAD |= 0x04; /* HRE D[5] */
381312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x05, 0x60, (u8)((temp & 0x1f) | jreg05));
382312fec14SDave Airlie 
383312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAC, 0x00, jregAC);
384312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAD, 0x00, jregAD);
385312fec14SDave Airlie 
386d6cbe630SJammy Huang 	// Workaround for HSync Time non octave pixels (1920x1080@60Hz HSync 44 pixels);
387d6cbe630SJammy Huang 	if ((ast->chip == AST2600) && (mode->crtc_vdisplay == 1080))
388d6cbe630SJammy Huang 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xFC, 0xFD, 0x02);
389d6cbe630SJammy Huang 	else
390d6cbe630SJammy Huang 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xFC, 0xFD, 0x00);
391d6cbe630SJammy Huang 
392312fec14SDave Airlie 	/* vert timings */
393312fec14SDave Airlie 	temp = (mode->crtc_vtotal) - 2;
394312fec14SDave Airlie 	if (temp & 0x100)
395312fec14SDave Airlie 		jreg07 |= 0x01;
396312fec14SDave Airlie 	if (temp & 0x200)
397312fec14SDave Airlie 		jreg07 |= 0x20;
398312fec14SDave Airlie 	if (temp & 0x400)
399312fec14SDave Airlie 		jregAE |= 0x01;
400312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x06, 0x00, temp);
401312fec14SDave Airlie 
402312fec14SDave Airlie 	temp = (mode->crtc_vsync_start) - 1;
403312fec14SDave Airlie 	if (temp & 0x100)
404312fec14SDave Airlie 		jreg07 |= 0x04;
405312fec14SDave Airlie 	if (temp & 0x200)
406312fec14SDave Airlie 		jreg07 |= 0x80;
407312fec14SDave Airlie 	if (temp & 0x400)
408312fec14SDave Airlie 		jregAE |= 0x08;
409312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x10, 0x00, temp);
410312fec14SDave Airlie 
411312fec14SDave Airlie 	temp = (mode->crtc_vsync_end - 1) & 0x3f;
412312fec14SDave Airlie 	if (temp & 0x10)
413312fec14SDave Airlie 		jregAE |= 0x20;
414312fec14SDave Airlie 	if (temp & 0x20)
415312fec14SDave Airlie 		jregAE |= 0x40;
416312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x70, temp & 0xf);
417312fec14SDave Airlie 
418312fec14SDave Airlie 	temp = mode->crtc_vdisplay - 1;
419312fec14SDave Airlie 	if (temp & 0x100)
420312fec14SDave Airlie 		jreg07 |= 0x02;
421312fec14SDave Airlie 	if (temp & 0x200)
422312fec14SDave Airlie 		jreg07 |= 0x40;
423312fec14SDave Airlie 	if (temp & 0x400)
424312fec14SDave Airlie 		jregAE |= 0x02;
425312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x12, 0x00, temp);
426312fec14SDave Airlie 
427312fec14SDave Airlie 	temp = mode->crtc_vblank_start - 1;
428312fec14SDave Airlie 	if (temp & 0x100)
429312fec14SDave Airlie 		jreg07 |= 0x08;
430312fec14SDave Airlie 	if (temp & 0x200)
431312fec14SDave Airlie 		jreg09 |= 0x20;
432312fec14SDave Airlie 	if (temp & 0x400)
433312fec14SDave Airlie 		jregAE |= 0x04;
434312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x15, 0x00, temp);
435312fec14SDave Airlie 
436312fec14SDave Airlie 	temp = mode->crtc_vblank_end - 1;
437312fec14SDave Airlie 	if (temp & 0x100)
438312fec14SDave Airlie 		jregAE |= 0x10;
439312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x16, 0x00, temp);
440312fec14SDave Airlie 
441312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x07, 0x00, jreg07);
442312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x09, 0xdf, jreg09);
443312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAE, 0x00, (jregAE | 0x80));
444312fec14SDave Airlie 
4459f93c8b3SY.C. Chen 	if (precache)
4469f93c8b3SY.C. Chen 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0x3f, 0x80);
4479f93c8b3SY.C. Chen 	else
4489f93c8b3SY.C. Chen 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0x3f, 0x00);
4499f93c8b3SY.C. Chen 
450312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x80);
451312fec14SDave Airlie }
452312fec14SDave Airlie 
453ae37025dSThomas Zimmermann static void ast_set_offset_reg(struct ast_private *ast,
454ae37025dSThomas Zimmermann 			       struct drm_framebuffer *fb)
455312fec14SDave Airlie {
456312fec14SDave Airlie 	u16 offset;
457312fec14SDave Airlie 
4587445283aSVille Syrjälä 	offset = fb->pitches[0] >> 3;
459312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x13, (offset & 0xff));
460312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xb0, (offset >> 8) & 0x3f);
461312fec14SDave Airlie }
462312fec14SDave Airlie 
463ae37025dSThomas Zimmermann static void ast_set_dclk_reg(struct ast_private *ast,
464ae37025dSThomas Zimmermann 			     struct drm_display_mode *mode,
465312fec14SDave Airlie 			     struct ast_vbios_mode_info *vbios_mode)
466312fec14SDave Airlie {
46722acdbb1SBenjamin Herrenschmidt 	const struct ast_vbios_dclk_info *clk_info;
468312fec14SDave Airlie 
469d6cbe630SJammy Huang 	if ((ast->chip == AST2500) || (ast->chip == AST2600))
4709f93c8b3SY.C. Chen 		clk_info = &dclk_table_ast2500[vbios_mode->enh_table->dclk_index];
4719f93c8b3SY.C. Chen 	else
472312fec14SDave Airlie 		clk_info = &dclk_table[vbios_mode->enh_table->dclk_index];
473312fec14SDave Airlie 
474312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc0, 0x00, clk_info->param1);
475312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc1, 0x00, clk_info->param2);
476312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xbb, 0x0f,
4779f93c8b3SY.C. Chen 			       (clk_info->param3 & 0xc0) |
4789f93c8b3SY.C. Chen 			       ((clk_info->param3 & 0x3) << 4));
479312fec14SDave Airlie }
480312fec14SDave Airlie 
481ae37025dSThomas Zimmermann static void ast_set_color_reg(struct ast_private *ast,
482ae37025dSThomas Zimmermann 			      const struct drm_format_info *format)
483312fec14SDave Airlie {
484312fec14SDave Airlie 	u8 jregA0 = 0, jregA3 = 0, jregA8 = 0;
485312fec14SDave Airlie 
486ae37025dSThomas Zimmermann 	switch (format->cpp[0] * 8) {
487312fec14SDave Airlie 	case 8:
488312fec14SDave Airlie 		jregA0 = 0x70;
489312fec14SDave Airlie 		jregA3 = 0x01;
490312fec14SDave Airlie 		jregA8 = 0x00;
491312fec14SDave Airlie 		break;
492312fec14SDave Airlie 	case 15:
493312fec14SDave Airlie 	case 16:
494312fec14SDave Airlie 		jregA0 = 0x70;
495312fec14SDave Airlie 		jregA3 = 0x04;
496312fec14SDave Airlie 		jregA8 = 0x02;
497312fec14SDave Airlie 		break;
498312fec14SDave Airlie 	case 32:
499312fec14SDave Airlie 		jregA0 = 0x70;
500312fec14SDave Airlie 		jregA3 = 0x08;
501312fec14SDave Airlie 		jregA8 = 0x02;
502312fec14SDave Airlie 		break;
503312fec14SDave Airlie 	}
504312fec14SDave Airlie 
505312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa0, 0x8f, jregA0);
506312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xf0, jregA3);
507312fec14SDave Airlie 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa8, 0xfd, jregA8);
5080d45ad98SThomas Zimmermann }
5090d45ad98SThomas Zimmermann 
510ae37025dSThomas Zimmermann static void ast_set_crtthd_reg(struct ast_private *ast)
5110d45ad98SThomas Zimmermann {
512312fec14SDave Airlie 	/* Set Threshold */
513bcc77411SKuoHsiang Chou 	if (ast->chip == AST2600) {
514bcc77411SKuoHsiang Chou 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0xe0);
515bcc77411SKuoHsiang Chou 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0xa0);
516bcc77411SKuoHsiang Chou 	} else if (ast->chip == AST2300 || ast->chip == AST2400 ||
5179f93c8b3SY.C. Chen 	    ast->chip == AST2500) {
518312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x78);
519312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x60);
520312fec14SDave Airlie 	} else if (ast->chip == AST2100 ||
521312fec14SDave Airlie 		   ast->chip == AST1100 ||
522312fec14SDave Airlie 		   ast->chip == AST2200 ||
523312fec14SDave Airlie 		   ast->chip == AST2150) {
524312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x3f);
525312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x2f);
526312fec14SDave Airlie 	} else {
527312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x2f);
528312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x1f);
529312fec14SDave Airlie 	}
530312fec14SDave Airlie }
531312fec14SDave Airlie 
532ae37025dSThomas Zimmermann static void ast_set_sync_reg(struct ast_private *ast,
533ae37025dSThomas Zimmermann 			     struct drm_display_mode *mode,
534312fec14SDave Airlie 			     struct ast_vbios_mode_info *vbios_mode)
535312fec14SDave Airlie {
536312fec14SDave Airlie 	u8 jreg;
537312fec14SDave Airlie 
538312fec14SDave Airlie 	jreg  = ast_io_read8(ast, AST_IO_MISC_PORT_READ);
53994d12b13SY.C. Chen 	jreg &= ~0xC0;
5406c9bd443SGregory Williams 	if (vbios_mode->enh_table->flags & NVSync)
5416c9bd443SGregory Williams 		jreg |= 0x80;
5426c9bd443SGregory Williams 	if (vbios_mode->enh_table->flags & NHSync)
5436c9bd443SGregory Williams 		jreg |= 0x40;
544312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
545312fec14SDave Airlie }
546312fec14SDave Airlie 
547ae37025dSThomas Zimmermann static void ast_set_start_address_crt1(struct ast_private *ast,
5486c9bd443SGregory Williams 				       unsigned int offset)
549312fec14SDave Airlie {
550312fec14SDave Airlie 	u32 addr;
551312fec14SDave Airlie 
552312fec14SDave Airlie 	addr = offset >> 2;
553312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0d, (u8)(addr & 0xff));
554312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0c, (u8)((addr >> 8) & 0xff));
555312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xaf, (u8)((addr >> 16) & 0xff));
556312fec14SDave Airlie 
557312fec14SDave Airlie }
558312fec14SDave Airlie 
55939edb287SThomas Zimmermann static void ast_wait_for_vretrace(struct ast_private *ast)
56039edb287SThomas Zimmermann {
56139edb287SThomas Zimmermann 	unsigned long timeout = jiffies + HZ;
56239edb287SThomas Zimmermann 	u8 vgair1;
56339edb287SThomas Zimmermann 
56439edb287SThomas Zimmermann 	do {
56539edb287SThomas Zimmermann 		vgair1 = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
56639edb287SThomas Zimmermann 	} while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout));
56739edb287SThomas Zimmermann }
56839edb287SThomas Zimmermann 
569a6ff807bSThomas Zimmermann /*
570f2fa5a99SThomas Zimmermann  * Planes
571f2fa5a99SThomas Zimmermann  */
572f2fa5a99SThomas Zimmermann 
573f2fa5a99SThomas Zimmermann static int ast_plane_init(struct drm_device *dev, struct ast_plane *ast_plane,
574f2fa5a99SThomas Zimmermann 			  void __iomem *vaddr, u64 offset, unsigned long size,
575f2fa5a99SThomas Zimmermann 			  uint32_t possible_crtcs,
576f2fa5a99SThomas Zimmermann 			  const struct drm_plane_funcs *funcs,
577f2fa5a99SThomas Zimmermann 			  const uint32_t *formats, unsigned int format_count,
578f2fa5a99SThomas Zimmermann 			  const uint64_t *format_modifiers,
579f2fa5a99SThomas Zimmermann 			  enum drm_plane_type type)
580f2fa5a99SThomas Zimmermann {
581f2fa5a99SThomas Zimmermann 	struct drm_plane *plane = &ast_plane->base;
582f2fa5a99SThomas Zimmermann 
583f2fa5a99SThomas Zimmermann 	ast_plane->vaddr = vaddr;
584f2fa5a99SThomas Zimmermann 	ast_plane->offset = offset;
585f2fa5a99SThomas Zimmermann 	ast_plane->size = size;
586f2fa5a99SThomas Zimmermann 
587f2fa5a99SThomas Zimmermann 	return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
588f2fa5a99SThomas Zimmermann 					formats, format_count, format_modifiers,
589f2fa5a99SThomas Zimmermann 					type, NULL);
590f2fa5a99SThomas Zimmermann }
591f2fa5a99SThomas Zimmermann 
592f2fa5a99SThomas Zimmermann /*
593a6ff807bSThomas Zimmermann  * Primary plane
594a6ff807bSThomas Zimmermann  */
595a6ff807bSThomas Zimmermann 
596a6ff807bSThomas Zimmermann static const uint32_t ast_primary_plane_formats[] = {
597a6ff807bSThomas Zimmermann 	DRM_FORMAT_XRGB8888,
598a6ff807bSThomas Zimmermann 	DRM_FORMAT_RGB565,
599a6ff807bSThomas Zimmermann 	DRM_FORMAT_C8,
600a6ff807bSThomas Zimmermann };
601a6ff807bSThomas Zimmermann 
602ae46a57dSThomas Zimmermann static int ast_primary_plane_helper_atomic_check(struct drm_plane *plane,
6037c11b99aSMaxime Ripard 						 struct drm_atomic_state *state)
604a6ff807bSThomas Zimmermann {
6050432a504SThomas Zimmermann 	struct drm_device *dev = plane->dev;
606d95dcfc4SThomas Zimmermann 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
607d95dcfc4SThomas Zimmermann 	struct drm_crtc_state *new_crtc_state = NULL;
608d95dcfc4SThomas Zimmermann 	struct ast_crtc_state *new_ast_crtc_state;
609ae46a57dSThomas Zimmermann 	int ret;
610ae46a57dSThomas Zimmermann 
6110432a504SThomas Zimmermann 	if (new_plane_state->crtc)
612d95dcfc4SThomas Zimmermann 		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
613ae46a57dSThomas Zimmermann 
614d95dcfc4SThomas Zimmermann 	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
615cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
616cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
617ae46a57dSThomas Zimmermann 						  false, true);
6180432a504SThomas Zimmermann 	if (ret) {
619ae46a57dSThomas Zimmermann 		return ret;
6200432a504SThomas Zimmermann 	} else if (!new_plane_state->visible) {
6210432a504SThomas Zimmermann 		if (drm_WARN_ON(dev, new_plane_state->crtc)) /* cannot legally happen */
6220432a504SThomas Zimmermann 			return -EINVAL;
6230432a504SThomas Zimmermann 		else
6243339fdf5SThomas Zimmermann 			return 0;
6250432a504SThomas Zimmermann 	}
6263339fdf5SThomas Zimmermann 
627d95dcfc4SThomas Zimmermann 	new_ast_crtc_state = to_ast_crtc_state(new_crtc_state);
6283339fdf5SThomas Zimmermann 
629d95dcfc4SThomas Zimmermann 	new_ast_crtc_state->format = new_plane_state->fb->format;
6303339fdf5SThomas Zimmermann 
631a6ff807bSThomas Zimmermann 	return 0;
632a6ff807bSThomas Zimmermann }
633a6ff807bSThomas Zimmermann 
634f2fa5a99SThomas Zimmermann static void ast_handle_damage(struct ast_plane *ast_plane, struct iosys_map *src,
635f2fa5a99SThomas Zimmermann 			      struct drm_framebuffer *fb,
636f2fa5a99SThomas Zimmermann 			      const struct drm_rect *clip)
637f2fa5a99SThomas Zimmermann {
638b1def7faSThomas Zimmermann 	struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(ast_plane->vaddr);
639f2fa5a99SThomas Zimmermann 
640f2fa5a99SThomas Zimmermann 	iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, clip));
641f2fa5a99SThomas Zimmermann 	drm_fb_memcpy(&dst, fb->pitches, src, fb, clip);
642f2fa5a99SThomas Zimmermann }
643f2fa5a99SThomas Zimmermann 
644d95dcfc4SThomas Zimmermann static void ast_primary_plane_helper_atomic_update(struct drm_plane *plane,
645977697e2SMaxime Ripard 						   struct drm_atomic_state *state)
646a6ff807bSThomas Zimmermann {
6471a19b4cbSThomas Zimmermann 	struct drm_device *dev = plane->dev;
6481a19b4cbSThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
649d95dcfc4SThomas Zimmermann 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
650f2fa5a99SThomas Zimmermann 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
651d95dcfc4SThomas Zimmermann 	struct drm_framebuffer *fb = plane_state->fb;
652d95dcfc4SThomas Zimmermann 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
653d95dcfc4SThomas Zimmermann 	struct drm_framebuffer *old_fb = old_plane_state->fb;
654f2fa5a99SThomas Zimmermann 	struct ast_plane *ast_plane = to_ast_plane(plane);
655f2fa5a99SThomas Zimmermann 	struct drm_rect damage;
656f2fa5a99SThomas Zimmermann 	struct drm_atomic_helper_damage_iter iter;
657a6ff807bSThomas Zimmermann 
6585638c82cSThomas Zimmermann 	if (!old_fb || (fb->format != old_fb->format)) {
659d95dcfc4SThomas Zimmermann 		struct drm_crtc *crtc = plane_state->crtc;
660d95dcfc4SThomas Zimmermann 		struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
6615638c82cSThomas Zimmermann 		struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
6625638c82cSThomas Zimmermann 		struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
6635638c82cSThomas Zimmermann 
6645638c82cSThomas Zimmermann 		ast_set_color_reg(ast, fb->format);
6655638c82cSThomas Zimmermann 		ast_set_vbios_color_reg(ast, fb->format, vbios_mode_info);
6665638c82cSThomas Zimmermann 	}
6675638c82cSThomas Zimmermann 
668f2fa5a99SThomas Zimmermann 	drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
669f2fa5a99SThomas Zimmermann 	drm_atomic_for_each_plane_damage(&iter, &damage) {
670f2fa5a99SThomas Zimmermann 		ast_handle_damage(ast_plane, shadow_plane_state->data, fb, &damage);
671f2fa5a99SThomas Zimmermann 	}
672a6ff807bSThomas Zimmermann 
673202fb33bSThomas Zimmermann 	/*
674202fb33bSThomas Zimmermann 	 * Some BMCs stop scanning out the video signal after the driver
675*8f85e4a1SThomas Zimmermann 	 * reprogrammed the offset. This stalls display output for several
676*8f85e4a1SThomas Zimmermann 	 * seconds and makes the display unusable. Therefore only update
677*8f85e4a1SThomas Zimmermann 	 * the offset if it changes.
678202fb33bSThomas Zimmermann 	 */
679202fb33bSThomas Zimmermann 	if (!old_fb || old_fb->pitches[0] != fb->pitches[0])
6805638c82cSThomas Zimmermann 		ast_set_offset_reg(ast, fb);
681*8f85e4a1SThomas Zimmermann }
682*8f85e4a1SThomas Zimmermann 
683*8f85e4a1SThomas Zimmermann static void ast_primary_plane_helper_atomic_enable(struct drm_plane *plane,
684*8f85e4a1SThomas Zimmermann 						   struct drm_atomic_state *state)
685*8f85e4a1SThomas Zimmermann {
686*8f85e4a1SThomas Zimmermann 	struct ast_private *ast = to_ast_private(plane->dev);
687*8f85e4a1SThomas Zimmermann 	struct ast_plane *ast_plane = to_ast_plane(plane);
688*8f85e4a1SThomas Zimmermann 
689*8f85e4a1SThomas Zimmermann 	/*
690*8f85e4a1SThomas Zimmermann 	 * Some BMCs stop scanning out the video signal after the driver
691*8f85e4a1SThomas Zimmermann 	 * reprogrammed the scanout address. This stalls display
692*8f85e4a1SThomas Zimmermann 	 * output for several seconds and makes the display unusable.
693*8f85e4a1SThomas Zimmermann 	 * Therefore only reprogram the address after enabling the plane.
694*8f85e4a1SThomas Zimmermann 	 */
695f2fa5a99SThomas Zimmermann 	ast_set_start_address_crt1(ast, (u32)ast_plane->offset);
6962fbeec03SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x00);
6972fbeec03SThomas Zimmermann }
6982fbeec03SThomas Zimmermann 
699d95dcfc4SThomas Zimmermann static void ast_primary_plane_helper_atomic_disable(struct drm_plane *plane,
700977697e2SMaxime Ripard 						    struct drm_atomic_state *state)
7012fbeec03SThomas Zimmermann {
702fa7dbd76SThomas Zimmermann 	struct ast_private *ast = to_ast_private(plane->dev);
7032fbeec03SThomas Zimmermann 
7042fbeec03SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x20);
705a6ff807bSThomas Zimmermann }
706a6ff807bSThomas Zimmermann 
707a6ff807bSThomas Zimmermann static const struct drm_plane_helper_funcs ast_primary_plane_helper_funcs = {
708f2fa5a99SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
709a6ff807bSThomas Zimmermann 	.atomic_check = ast_primary_plane_helper_atomic_check,
710a6ff807bSThomas Zimmermann 	.atomic_update = ast_primary_plane_helper_atomic_update,
711*8f85e4a1SThomas Zimmermann 	.atomic_enable = ast_primary_plane_helper_atomic_enable,
7122fbeec03SThomas Zimmermann 	.atomic_disable = ast_primary_plane_helper_atomic_disable,
713a6ff807bSThomas Zimmermann };
714a6ff807bSThomas Zimmermann 
715a6ff807bSThomas Zimmermann static const struct drm_plane_funcs ast_primary_plane_funcs = {
716a6ff807bSThomas Zimmermann 	.update_plane = drm_atomic_helper_update_plane,
717a6ff807bSThomas Zimmermann 	.disable_plane = drm_atomic_helper_disable_plane,
718a6ff807bSThomas Zimmermann 	.destroy = drm_plane_cleanup,
719f2fa5a99SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_FUNCS,
720a6ff807bSThomas Zimmermann };
721a6ff807bSThomas Zimmermann 
722616048afSThomas Zimmermann static int ast_primary_plane_init(struct ast_private *ast)
723616048afSThomas Zimmermann {
724616048afSThomas Zimmermann 	struct drm_device *dev = &ast->base;
725f2fa5a99SThomas Zimmermann 	struct ast_plane *ast_primary_plane = &ast->primary_plane;
726f2fa5a99SThomas Zimmermann 	struct drm_plane *primary_plane = &ast_primary_plane->base;
727f2fa5a99SThomas Zimmermann 	void __iomem *vaddr = ast->vram;
728f2fa5a99SThomas Zimmermann 	u64 offset = ast->vram_base;
729f2fa5a99SThomas Zimmermann 	unsigned long cursor_size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
730f2fa5a99SThomas Zimmermann 	unsigned long size = ast->vram_fb_available - cursor_size;
731616048afSThomas Zimmermann 	int ret;
732616048afSThomas Zimmermann 
733f2fa5a99SThomas Zimmermann 	ret = ast_plane_init(dev, ast_primary_plane, vaddr, offset, size,
734f2fa5a99SThomas Zimmermann 			     0x01, &ast_primary_plane_funcs,
735f2fa5a99SThomas Zimmermann 			     ast_primary_plane_formats, ARRAY_SIZE(ast_primary_plane_formats),
736f2fa5a99SThomas Zimmermann 			     NULL, DRM_PLANE_TYPE_PRIMARY);
737616048afSThomas Zimmermann 	if (ret) {
738f2fa5a99SThomas Zimmermann 		drm_err(dev, "ast_plane_init() failed: %d\n", ret);
739616048afSThomas Zimmermann 		return ret;
740616048afSThomas Zimmermann 	}
741616048afSThomas Zimmermann 	drm_plane_helper_add(primary_plane, &ast_primary_plane_helper_funcs);
742f2fa5a99SThomas Zimmermann 	drm_plane_enable_fb_damage_clips(primary_plane);
743616048afSThomas Zimmermann 
744616048afSThomas Zimmermann 	return 0;
745616048afSThomas Zimmermann }
746616048afSThomas Zimmermann 
747a6ff807bSThomas Zimmermann /*
74802f3bb75SThomas Zimmermann  * Cursor plane
74902f3bb75SThomas Zimmermann  */
75002f3bb75SThomas Zimmermann 
751718c2286SThomas Zimmermann static void ast_update_cursor_image(u8 __iomem *dst, const u8 *src, int width, int height)
752718c2286SThomas Zimmermann {
753718c2286SThomas Zimmermann 	union {
754718c2286SThomas Zimmermann 		u32 ul;
755718c2286SThomas Zimmermann 		u8 b[4];
756718c2286SThomas Zimmermann 	} srcdata32[2], data32;
757718c2286SThomas Zimmermann 	union {
758718c2286SThomas Zimmermann 		u16 us;
759718c2286SThomas Zimmermann 		u8 b[2];
760718c2286SThomas Zimmermann 	} data16;
761718c2286SThomas Zimmermann 	u32 csum = 0;
762718c2286SThomas Zimmermann 	s32 alpha_dst_delta, last_alpha_dst_delta;
763718c2286SThomas Zimmermann 	u8 __iomem *dstxor;
764718c2286SThomas Zimmermann 	const u8 *srcxor;
765718c2286SThomas Zimmermann 	int i, j;
766718c2286SThomas Zimmermann 	u32 per_pixel_copy, two_pixel_copy;
767718c2286SThomas Zimmermann 
768718c2286SThomas Zimmermann 	alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
769718c2286SThomas Zimmermann 	last_alpha_dst_delta = alpha_dst_delta - (width << 1);
770718c2286SThomas Zimmermann 
771718c2286SThomas Zimmermann 	srcxor = src;
772718c2286SThomas Zimmermann 	dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
773718c2286SThomas Zimmermann 	per_pixel_copy = width & 1;
774718c2286SThomas Zimmermann 	two_pixel_copy = width >> 1;
775718c2286SThomas Zimmermann 
776718c2286SThomas Zimmermann 	for (j = 0; j < height; j++) {
777718c2286SThomas Zimmermann 		for (i = 0; i < two_pixel_copy; i++) {
778718c2286SThomas Zimmermann 			srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
779718c2286SThomas Zimmermann 			srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
780718c2286SThomas Zimmermann 			data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
781718c2286SThomas Zimmermann 			data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
782718c2286SThomas Zimmermann 			data32.b[2] = srcdata32[1].b[1] | (srcdata32[1].b[0] >> 4);
783718c2286SThomas Zimmermann 			data32.b[3] = srcdata32[1].b[3] | (srcdata32[1].b[2] >> 4);
784718c2286SThomas Zimmermann 
785718c2286SThomas Zimmermann 			writel(data32.ul, dstxor);
786718c2286SThomas Zimmermann 			csum += data32.ul;
787718c2286SThomas Zimmermann 
788718c2286SThomas Zimmermann 			dstxor += 4;
789718c2286SThomas Zimmermann 			srcxor += 8;
790718c2286SThomas Zimmermann 
791718c2286SThomas Zimmermann 		}
792718c2286SThomas Zimmermann 
793718c2286SThomas Zimmermann 		for (i = 0; i < per_pixel_copy; i++) {
794718c2286SThomas Zimmermann 			srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
795718c2286SThomas Zimmermann 			data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
796718c2286SThomas Zimmermann 			data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
797718c2286SThomas Zimmermann 			writew(data16.us, dstxor);
798718c2286SThomas Zimmermann 			csum += (u32)data16.us;
799718c2286SThomas Zimmermann 
800718c2286SThomas Zimmermann 			dstxor += 2;
801718c2286SThomas Zimmermann 			srcxor += 4;
802718c2286SThomas Zimmermann 		}
803718c2286SThomas Zimmermann 		dstxor += last_alpha_dst_delta;
804718c2286SThomas Zimmermann 	}
805718c2286SThomas Zimmermann 
806718c2286SThomas Zimmermann 	/* write checksum + signature */
807718c2286SThomas Zimmermann 	dst += AST_HWC_SIZE;
808718c2286SThomas Zimmermann 	writel(csum, dst);
809718c2286SThomas Zimmermann 	writel(width, dst + AST_HWC_SIGNATURE_SizeX);
810718c2286SThomas Zimmermann 	writel(height, dst + AST_HWC_SIGNATURE_SizeY);
811718c2286SThomas Zimmermann 	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
812718c2286SThomas Zimmermann 	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
813718c2286SThomas Zimmermann }
814718c2286SThomas Zimmermann 
815718c2286SThomas Zimmermann static void ast_set_cursor_base(struct ast_private *ast, u64 address)
816718c2286SThomas Zimmermann {
817718c2286SThomas Zimmermann 	u8 addr0 = (address >> 3) & 0xff;
818718c2286SThomas Zimmermann 	u8 addr1 = (address >> 11) & 0xff;
819718c2286SThomas Zimmermann 	u8 addr2 = (address >> 19) & 0xff;
820718c2286SThomas Zimmermann 
821718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc8, addr0);
822718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc9, addr1);
823718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xca, addr2);
824718c2286SThomas Zimmermann }
825718c2286SThomas Zimmermann 
826718c2286SThomas Zimmermann static void ast_set_cursor_location(struct ast_private *ast, u16 x, u16 y,
827718c2286SThomas Zimmermann 				    u8 x_offset, u8 y_offset)
828718c2286SThomas Zimmermann {
829718c2286SThomas Zimmermann 	u8 x0 = (x & 0x00ff);
830718c2286SThomas Zimmermann 	u8 x1 = (x & 0x0f00) >> 8;
831718c2286SThomas Zimmermann 	u8 y0 = (y & 0x00ff);
832718c2286SThomas Zimmermann 	u8 y1 = (y & 0x0700) >> 8;
833718c2286SThomas Zimmermann 
834718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc2, x_offset);
835718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc3, y_offset);
836718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc4, x0);
837718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc5, x1);
838718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc6, y0);
839718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc7, y1);
840718c2286SThomas Zimmermann }
841718c2286SThomas Zimmermann 
842718c2286SThomas Zimmermann static void ast_set_cursor_enabled(struct ast_private *ast, bool enabled)
843718c2286SThomas Zimmermann {
844718c2286SThomas Zimmermann 	static const u8 mask = (u8)~(AST_IO_VGACRCB_HWC_16BPP |
845718c2286SThomas Zimmermann 				     AST_IO_VGACRCB_HWC_ENABLED);
846718c2286SThomas Zimmermann 
847718c2286SThomas Zimmermann 	u8 vgacrcb = AST_IO_VGACRCB_HWC_16BPP;
848718c2286SThomas Zimmermann 
849718c2286SThomas Zimmermann 	if (enabled)
850718c2286SThomas Zimmermann 		vgacrcb |= AST_IO_VGACRCB_HWC_ENABLED;
851718c2286SThomas Zimmermann 
852718c2286SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, mask, vgacrcb);
853718c2286SThomas Zimmermann }
854718c2286SThomas Zimmermann 
85502f3bb75SThomas Zimmermann static const uint32_t ast_cursor_plane_formats[] = {
85602f3bb75SThomas Zimmermann 	DRM_FORMAT_ARGB8888,
85702f3bb75SThomas Zimmermann };
85802f3bb75SThomas Zimmermann 
85902f3bb75SThomas Zimmermann static int ast_cursor_plane_helper_atomic_check(struct drm_plane *plane,
8607c11b99aSMaxime Ripard 						struct drm_atomic_state *state)
86102f3bb75SThomas Zimmermann {
862d95dcfc4SThomas Zimmermann 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
863d95dcfc4SThomas Zimmermann 	struct drm_framebuffer *new_fb = new_plane_state->fb;
864d95dcfc4SThomas Zimmermann 	struct drm_crtc_state *new_crtc_state = NULL;
865ae46a57dSThomas Zimmermann 	int ret;
866ae46a57dSThomas Zimmermann 
8670432a504SThomas Zimmermann 	if (new_plane_state->crtc)
868d95dcfc4SThomas Zimmermann 		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
869ae46a57dSThomas Zimmermann 
870d95dcfc4SThomas Zimmermann 	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
871cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
872cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
873ae46a57dSThomas Zimmermann 						  true, true);
8740432a504SThomas Zimmermann 	if (ret || !new_plane_state->visible)
875ae46a57dSThomas Zimmermann 		return ret;
876ae46a57dSThomas Zimmermann 
877d95dcfc4SThomas Zimmermann 	if (new_fb->width > AST_MAX_HWC_WIDTH || new_fb->height > AST_MAX_HWC_HEIGHT)
878ae46a57dSThomas Zimmermann 		return -EINVAL;
879ae46a57dSThomas Zimmermann 
88002f3bb75SThomas Zimmermann 	return 0;
88102f3bb75SThomas Zimmermann }
88202f3bb75SThomas Zimmermann 
883d95dcfc4SThomas Zimmermann static void ast_cursor_plane_helper_atomic_update(struct drm_plane *plane,
884977697e2SMaxime Ripard 						  struct drm_atomic_state *state)
88502f3bb75SThomas Zimmermann {
886537a1db9SThomas Zimmermann 	struct ast_plane *ast_plane = to_ast_plane(plane);
887d95dcfc4SThomas Zimmermann 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
888d95dcfc4SThomas Zimmermann 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
889d95dcfc4SThomas Zimmermann 	struct drm_framebuffer *fb = plane_state->fb;
890d95dcfc4SThomas Zimmermann 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
891365c0e70SThomas Zimmermann 	struct ast_private *ast = to_ast_private(plane->dev);
8927938f421SLucas De Marchi 	struct iosys_map src_map = shadow_plane_state->data[0];
893f2fa5a99SThomas Zimmermann 	struct drm_rect damage;
894f2fa5a99SThomas Zimmermann 	const u8 *src = src_map.vaddr; /* TODO: Use mapping abstraction properly */
895f2fa5a99SThomas Zimmermann 	u64 dst_off = ast_plane->offset;
896f2fa5a99SThomas Zimmermann 	u8 __iomem *dst = ast_plane->vaddr; /* TODO: Use mapping abstraction properly */
897f2fa5a99SThomas Zimmermann 	u8 __iomem *sig = dst + AST_HWC_SIZE; /* TODO: Use mapping abstraction properly */
89881039adcSThomas Zimmermann 	unsigned int offset_x, offset_y;
899718c2286SThomas Zimmermann 	u16 x, y;
900718c2286SThomas Zimmermann 	u8 x_offset, y_offset;
9014d36cf07SThomas Zimmermann 
9024d36cf07SThomas Zimmermann 	/*
903f2fa5a99SThomas Zimmermann 	 * Do data transfer to hardware buffer and point the scanout
904f2fa5a99SThomas Zimmermann 	 * engine to the offset.
9054d36cf07SThomas Zimmermann 	 */
9064d36cf07SThomas Zimmermann 
907f2fa5a99SThomas Zimmermann 	if (drm_atomic_helper_damage_merged(old_plane_state, plane_state, &damage)) {
9084d36cf07SThomas Zimmermann 		ast_update_cursor_image(dst, src, fb->width, fb->height);
909385131f3SThomas Zimmermann 		ast_set_cursor_base(ast, dst_off);
910f2fa5a99SThomas Zimmermann 	}
911718c2286SThomas Zimmermann 
9124d36cf07SThomas Zimmermann 	/*
9134d36cf07SThomas Zimmermann 	 * Update location in HWC signature and registers.
9144d36cf07SThomas Zimmermann 	 */
915718c2286SThomas Zimmermann 
916d95dcfc4SThomas Zimmermann 	writel(plane_state->crtc_x, sig + AST_HWC_SIGNATURE_X);
917d95dcfc4SThomas Zimmermann 	writel(plane_state->crtc_y, sig + AST_HWC_SIGNATURE_Y);
918718c2286SThomas Zimmermann 
91981039adcSThomas Zimmermann 	offset_x = AST_MAX_HWC_WIDTH - fb->width;
920ee4a92d6SThomas Zimmermann 	offset_y = AST_MAX_HWC_HEIGHT - fb->height;
92102f3bb75SThomas Zimmermann 
922d95dcfc4SThomas Zimmermann 	if (plane_state->crtc_x < 0) {
923d95dcfc4SThomas Zimmermann 		x_offset = (-plane_state->crtc_x) + offset_x;
924718c2286SThomas Zimmermann 		x = 0;
925718c2286SThomas Zimmermann 	} else {
926718c2286SThomas Zimmermann 		x_offset = offset_x;
927d95dcfc4SThomas Zimmermann 		x = plane_state->crtc_x;
928718c2286SThomas Zimmermann 	}
929d95dcfc4SThomas Zimmermann 	if (plane_state->crtc_y < 0) {
930d95dcfc4SThomas Zimmermann 		y_offset = (-plane_state->crtc_y) + offset_y;
931718c2286SThomas Zimmermann 		y = 0;
932718c2286SThomas Zimmermann 	} else {
933718c2286SThomas Zimmermann 		y_offset = offset_y;
934d95dcfc4SThomas Zimmermann 		y = plane_state->crtc_y;
93502f3bb75SThomas Zimmermann 	}
93602f3bb75SThomas Zimmermann 
937718c2286SThomas Zimmermann 	ast_set_cursor_location(ast, x, y, x_offset, y_offset);
938718c2286SThomas Zimmermann 
9394d36cf07SThomas Zimmermann 	/* Dummy write to enable HWC and make the HW pick-up the changes. */
940718c2286SThomas Zimmermann 	ast_set_cursor_enabled(ast, true);
94102f3bb75SThomas Zimmermann }
94202f3bb75SThomas Zimmermann 
943d95dcfc4SThomas Zimmermann static void ast_cursor_plane_helper_atomic_disable(struct drm_plane *plane,
944977697e2SMaxime Ripard 						   struct drm_atomic_state *state)
94502f3bb75SThomas Zimmermann {
946fa7dbd76SThomas Zimmermann 	struct ast_private *ast = to_ast_private(plane->dev);
94702f3bb75SThomas Zimmermann 
948718c2286SThomas Zimmermann 	ast_set_cursor_enabled(ast, false);
94902f3bb75SThomas Zimmermann }
95002f3bb75SThomas Zimmermann 
95102f3bb75SThomas Zimmermann static const struct drm_plane_helper_funcs ast_cursor_plane_helper_funcs = {
9524d36cf07SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
95302f3bb75SThomas Zimmermann 	.atomic_check = ast_cursor_plane_helper_atomic_check,
95402f3bb75SThomas Zimmermann 	.atomic_update = ast_cursor_plane_helper_atomic_update,
95502f3bb75SThomas Zimmermann 	.atomic_disable = ast_cursor_plane_helper_atomic_disable,
95602f3bb75SThomas Zimmermann };
95702f3bb75SThomas Zimmermann 
95802f3bb75SThomas Zimmermann static const struct drm_plane_funcs ast_cursor_plane_funcs = {
95902f3bb75SThomas Zimmermann 	.update_plane = drm_atomic_helper_update_plane,
96002f3bb75SThomas Zimmermann 	.disable_plane = drm_atomic_helper_disable_plane,
961f2fa5a99SThomas Zimmermann 	.destroy = drm_plane_cleanup,
9624d36cf07SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_FUNCS,
96302f3bb75SThomas Zimmermann };
96402f3bb75SThomas Zimmermann 
965616048afSThomas Zimmermann static int ast_cursor_plane_init(struct ast_private *ast)
966616048afSThomas Zimmermann {
967616048afSThomas Zimmermann 	struct drm_device *dev = &ast->base;
968f2fa5a99SThomas Zimmermann 	struct ast_plane *ast_cursor_plane = &ast->cursor_plane;
969f2fa5a99SThomas Zimmermann 	struct drm_plane *cursor_plane = &ast_cursor_plane->base;
970aa7c8865SThomas Zimmermann 	size_t size;
971f2fa5a99SThomas Zimmermann 	void __iomem *vaddr;
972f2fa5a99SThomas Zimmermann 	u64 offset;
973616048afSThomas Zimmermann 	int ret;
974616048afSThomas Zimmermann 
97522b6591fSThomas Zimmermann 	/*
97622b6591fSThomas Zimmermann 	 * Allocate backing storage for cursors. The BOs are permanently
97722b6591fSThomas Zimmermann 	 * pinned to the top end of the VRAM.
97822b6591fSThomas Zimmermann 	 */
97922b6591fSThomas Zimmermann 
98022b6591fSThomas Zimmermann 	size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
98122b6591fSThomas Zimmermann 
982f2fa5a99SThomas Zimmermann 	if (ast->vram_fb_available < size)
983f2fa5a99SThomas Zimmermann 		return -ENOMEM;
984aa7c8865SThomas Zimmermann 
985f2fa5a99SThomas Zimmermann 	vaddr = ast->vram + ast->vram_fb_available - size;
986f2fa5a99SThomas Zimmermann 	offset = ast->vram_base + ast->vram_fb_available - size;
987aa7c8865SThomas Zimmermann 
988f2fa5a99SThomas Zimmermann 	ret = ast_plane_init(dev, ast_cursor_plane, vaddr, offset, size,
989f2fa5a99SThomas Zimmermann 			     0x01, &ast_cursor_plane_funcs,
990f2fa5a99SThomas Zimmermann 			     ast_cursor_plane_formats, ARRAY_SIZE(ast_cursor_plane_formats),
991f2fa5a99SThomas Zimmermann 			     NULL, DRM_PLANE_TYPE_CURSOR);
992616048afSThomas Zimmermann 	if (ret) {
993f2fa5a99SThomas Zimmermann 		drm_err(dev, "ast_plane_init() failed: %d\n", ret);
994f2fa5a99SThomas Zimmermann 		return ret;
995616048afSThomas Zimmermann 	}
996616048afSThomas Zimmermann 	drm_plane_helper_add(cursor_plane, &ast_cursor_plane_helper_funcs);
997f2fa5a99SThomas Zimmermann 	drm_plane_enable_fb_damage_clips(cursor_plane);
998f2fa5a99SThomas Zimmermann 
999f2fa5a99SThomas Zimmermann 	ast->vram_fb_available -= size;
1000616048afSThomas Zimmermann 
1001616048afSThomas Zimmermann 	return 0;
1002616048afSThomas Zimmermann }
1003616048afSThomas Zimmermann 
100402f3bb75SThomas Zimmermann /*
1005a6ff807bSThomas Zimmermann  * CRTC
1006a6ff807bSThomas Zimmermann  */
1007a6ff807bSThomas Zimmermann 
1008312fec14SDave Airlie static void ast_crtc_dpms(struct drm_crtc *crtc, int mode)
1009312fec14SDave Airlie {
1010fa7dbd76SThomas Zimmermann 	struct ast_private *ast = to_ast_private(crtc->dev);
1011594e9c04SKuoHsiang Chou 	u8 ch = AST_DPMS_VSYNC_OFF | AST_DPMS_HSYNC_OFF;
10125171660cSJocelyn Falempe 	struct ast_crtc_state *ast_state;
10135171660cSJocelyn Falempe 	const struct drm_format_info *format;
10145171660cSJocelyn Falempe 	struct ast_vbios_mode_info *vbios_mode_info;
1015312fec14SDave Airlie 
10162fbeec03SThomas Zimmermann 	/* TODO: Maybe control display signal generation with
10172fbeec03SThomas Zimmermann 	 *       Sync Enable (bit CR17.7).
10182fbeec03SThomas Zimmermann 	 */
1019312fec14SDave Airlie 	switch (mode) {
1020312fec14SDave Airlie 	case DRM_MODE_DPMS_ON:
1021594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT,  0x01, 0xdf, 0);
1022594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0xfc, 0);
10237f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_DP501_BIT)
102483c6620bSDave Airlie 			ast_set_dp501_video_output(crtc->dev, 1);
1025594e9c04SKuoHsiang Chou 
10267f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
1027594e9c04SKuoHsiang Chou 			ast_dp_power_on_off(crtc->dev, AST_DP_POWER_ON);
1028594e9c04SKuoHsiang Chou 			ast_wait_for_vretrace(ast);
1029594e9c04SKuoHsiang Chou 			ast_dp_set_on_off(crtc->dev, 1);
1030594e9c04SKuoHsiang Chou 		}
1031594e9c04SKuoHsiang Chou 
10325171660cSJocelyn Falempe 		ast_state = to_ast_crtc_state(crtc->state);
10335171660cSJocelyn Falempe 		format = ast_state->format;
10345171660cSJocelyn Falempe 
10355171660cSJocelyn Falempe 		if (format) {
10365171660cSJocelyn Falempe 			vbios_mode_info = &ast_state->vbios_mode_info;
10375171660cSJocelyn Falempe 
10385171660cSJocelyn Falempe 			ast_set_color_reg(ast, format);
10395171660cSJocelyn Falempe 			ast_set_vbios_color_reg(ast, format, vbios_mode_info);
1040ce7fcf70SJocelyn Falempe 			if (crtc->state->gamma_lut)
1041ce7fcf70SJocelyn Falempe 				ast_crtc_set_gamma(ast, format, crtc->state->gamma_lut->data);
1042ce7fcf70SJocelyn Falempe 			else
1043ce7fcf70SJocelyn Falempe 				ast_crtc_set_gamma_linear(ast, format);
10445171660cSJocelyn Falempe 		}
1045312fec14SDave Airlie 		break;
1046594e9c04SKuoHsiang Chou 	case DRM_MODE_DPMS_STANDBY:
1047594e9c04SKuoHsiang Chou 	case DRM_MODE_DPMS_SUSPEND:
1048312fec14SDave Airlie 	case DRM_MODE_DPMS_OFF:
1049594e9c04SKuoHsiang Chou 		ch = mode;
10507f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_DP501_BIT)
105183c6620bSDave Airlie 			ast_set_dp501_video_output(crtc->dev, 0);
1052594e9c04SKuoHsiang Chou 
10537f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
1054594e9c04SKuoHsiang Chou 			ast_dp_set_on_off(crtc->dev, 0);
1055594e9c04SKuoHsiang Chou 			ast_dp_power_on_off(crtc->dev, AST_DP_POWER_OFF);
1056594e9c04SKuoHsiang Chou 		}
1057594e9c04SKuoHsiang Chou 
1058594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT,  0x01, 0xdf, 0x20);
1059594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0xfc, ch);
10607f35680aSThomas Zimmermann 		break;
1061312fec14SDave Airlie 	}
1062312fec14SDave Airlie }
1063312fec14SDave Airlie 
10646abbad2cSThomas Zimmermann static enum drm_mode_status
10656abbad2cSThomas Zimmermann ast_crtc_helper_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode)
10666abbad2cSThomas Zimmermann {
10676abbad2cSThomas Zimmermann 	struct ast_private *ast = to_ast_private(crtc->dev);
10686abbad2cSThomas Zimmermann 	enum drm_mode_status status;
10696abbad2cSThomas Zimmermann 	uint32_t jtemp;
10706abbad2cSThomas Zimmermann 
10716abbad2cSThomas Zimmermann 	if (ast->support_wide_screen) {
10726abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
10736abbad2cSThomas Zimmermann 			return MODE_OK;
10746abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
10756abbad2cSThomas Zimmermann 			return MODE_OK;
10766abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
10776abbad2cSThomas Zimmermann 			return MODE_OK;
10786abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
10796abbad2cSThomas Zimmermann 			return MODE_OK;
10806abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
10816abbad2cSThomas Zimmermann 			return MODE_OK;
108271dee036SJammy Huang 		if ((mode->hdisplay == 1152) && (mode->vdisplay == 864))
108371dee036SJammy Huang 			return MODE_OK;
10846abbad2cSThomas Zimmermann 
10856abbad2cSThomas Zimmermann 		if ((ast->chip == AST2100) || (ast->chip == AST2200) ||
10866abbad2cSThomas Zimmermann 		    (ast->chip == AST2300) || (ast->chip == AST2400) ||
1087594e9c04SKuoHsiang Chou 		    (ast->chip == AST2500) || (ast->chip == AST2600)) {
10886abbad2cSThomas Zimmermann 			if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
10896abbad2cSThomas Zimmermann 				return MODE_OK;
10906abbad2cSThomas Zimmermann 
10916abbad2cSThomas Zimmermann 			if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
10926abbad2cSThomas Zimmermann 				jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
10936abbad2cSThomas Zimmermann 				if (jtemp & 0x01)
10946abbad2cSThomas Zimmermann 					return MODE_NOMODE;
10956abbad2cSThomas Zimmermann 				else
10966abbad2cSThomas Zimmermann 					return MODE_OK;
10976abbad2cSThomas Zimmermann 			}
10986abbad2cSThomas Zimmermann 		}
10996abbad2cSThomas Zimmermann 	}
11006abbad2cSThomas Zimmermann 
11016abbad2cSThomas Zimmermann 	status = MODE_NOMODE;
11026abbad2cSThomas Zimmermann 
11036abbad2cSThomas Zimmermann 	switch (mode->hdisplay) {
11046abbad2cSThomas Zimmermann 	case 640:
11056abbad2cSThomas Zimmermann 		if (mode->vdisplay == 480)
11066abbad2cSThomas Zimmermann 			status = MODE_OK;
11076abbad2cSThomas Zimmermann 		break;
11086abbad2cSThomas Zimmermann 	case 800:
11096abbad2cSThomas Zimmermann 		if (mode->vdisplay == 600)
11106abbad2cSThomas Zimmermann 			status = MODE_OK;
11116abbad2cSThomas Zimmermann 		break;
11126abbad2cSThomas Zimmermann 	case 1024:
11136abbad2cSThomas Zimmermann 		if (mode->vdisplay == 768)
11146abbad2cSThomas Zimmermann 			status = MODE_OK;
11156abbad2cSThomas Zimmermann 		break;
111671dee036SJammy Huang 	case 1152:
111771dee036SJammy Huang 		if (mode->vdisplay == 864)
111871dee036SJammy Huang 			status = MODE_OK;
111971dee036SJammy Huang 		break;
11206abbad2cSThomas Zimmermann 	case 1280:
11216abbad2cSThomas Zimmermann 		if (mode->vdisplay == 1024)
11226abbad2cSThomas Zimmermann 			status = MODE_OK;
11236abbad2cSThomas Zimmermann 		break;
11246abbad2cSThomas Zimmermann 	case 1600:
11256abbad2cSThomas Zimmermann 		if (mode->vdisplay == 1200)
11266abbad2cSThomas Zimmermann 			status = MODE_OK;
11276abbad2cSThomas Zimmermann 		break;
11286abbad2cSThomas Zimmermann 	default:
11296abbad2cSThomas Zimmermann 		break;
11306abbad2cSThomas Zimmermann 	}
11316abbad2cSThomas Zimmermann 
11326abbad2cSThomas Zimmermann 	return status;
11336abbad2cSThomas Zimmermann }
11346abbad2cSThomas Zimmermann 
1135b48e1b6fSThomas Zimmermann static int ast_crtc_helper_atomic_check(struct drm_crtc *crtc,
113629b77ad7SMaxime Ripard 					struct drm_atomic_state *state)
1137b48e1b6fSThomas Zimmermann {
1138016a14beSThomas Zimmermann 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1139ce7fcf70SJocelyn Falempe 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
1140ce7fcf70SJocelyn Falempe 	struct ast_crtc_state *old_ast_crtc_state = to_ast_crtc_state(old_crtc_state);
11415638c82cSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
1142e7d70cd4SThomas Zimmermann 	struct ast_crtc_state *ast_state;
11433339fdf5SThomas Zimmermann 	const struct drm_format_info *format;
1144b48e1b6fSThomas Zimmermann 	bool succ;
1145016a14beSThomas Zimmermann 	int ret;
1146016a14beSThomas Zimmermann 
114729b77ad7SMaxime Ripard 	if (!crtc_state->enable)
1148963a2ba2SThomas Zimmermann 		return 0;
1149d6ddbd5cSThomas Zimmermann 
11508f2fd57dSThomas Zimmermann 	ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state);
11518f2fd57dSThomas Zimmermann 	if (ret)
11528f2fd57dSThomas Zimmermann 		return ret;
11538f2fd57dSThomas Zimmermann 
115429b77ad7SMaxime Ripard 	ast_state = to_ast_crtc_state(crtc_state);
1155b48e1b6fSThomas Zimmermann 
11563339fdf5SThomas Zimmermann 	format = ast_state->format;
11575638c82cSThomas Zimmermann 	if (drm_WARN_ON_ONCE(dev, !format))
11585638c82cSThomas Zimmermann 		return -EINVAL; /* BUG: We didn't set format in primary check(). */
1159e7d70cd4SThomas Zimmermann 
1160ce7fcf70SJocelyn Falempe 	/*
1161ce7fcf70SJocelyn Falempe 	 * The gamma LUT has to be reloaded after changing the primary
1162ce7fcf70SJocelyn Falempe 	 * plane's color format.
1163ce7fcf70SJocelyn Falempe 	 */
1164ce7fcf70SJocelyn Falempe 	if (old_ast_crtc_state->format != format)
1165ce7fcf70SJocelyn Falempe 		crtc_state->color_mgmt_changed = true;
1166ce7fcf70SJocelyn Falempe 
1167ce7fcf70SJocelyn Falempe 	if (crtc_state->color_mgmt_changed && crtc_state->gamma_lut) {
1168ce7fcf70SJocelyn Falempe 		if (crtc_state->gamma_lut->length !=
1169ce7fcf70SJocelyn Falempe 		    AST_LUT_SIZE * sizeof(struct drm_color_lut)) {
1170ce7fcf70SJocelyn Falempe 			drm_err(dev, "Wrong size for gamma_lut %zu\n",
1171ce7fcf70SJocelyn Falempe 				crtc_state->gamma_lut->length);
1172ce7fcf70SJocelyn Falempe 			return -EINVAL;
1173ce7fcf70SJocelyn Falempe 		}
1174ce7fcf70SJocelyn Falempe 	}
1175ce7fcf70SJocelyn Falempe 
117629b77ad7SMaxime Ripard 	succ = ast_get_vbios_mode_info(format, &crtc_state->mode,
117729b77ad7SMaxime Ripard 				       &crtc_state->adjusted_mode,
1178e7d70cd4SThomas Zimmermann 				       &ast_state->vbios_mode_info);
1179b48e1b6fSThomas Zimmermann 	if (!succ)
1180b48e1b6fSThomas Zimmermann 		return -EINVAL;
1181b48e1b6fSThomas Zimmermann 
1182963a2ba2SThomas Zimmermann 	return 0;
1183b48e1b6fSThomas Zimmermann }
1184b48e1b6fSThomas Zimmermann 
1185f3901b5fSThomas Zimmermann static void
1186f6ebe9f9SMaxime Ripard ast_crtc_helper_atomic_flush(struct drm_crtc *crtc,
1187f6ebe9f9SMaxime Ripard 			     struct drm_atomic_state *state)
11888e3784dfSThomas Zimmermann {
1189253f28b6SMaxime Ripard 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
1190253f28b6SMaxime Ripard 									  crtc);
1191f870231fSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
1192f870231fSThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
1193253f28b6SMaxime Ripard 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
1194594e9c04SKuoHsiang Chou 	struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
11958e3784dfSThomas Zimmermann 
11968e3784dfSThomas Zimmermann 	/*
11978e3784dfSThomas Zimmermann 	 * The gamma LUT has to be reloaded after changing the primary
11988e3784dfSThomas Zimmermann 	 * plane's color format.
11998e3784dfSThomas Zimmermann 	 */
1200ce7fcf70SJocelyn Falempe 	if (crtc_state->enable && crtc_state->color_mgmt_changed) {
1201ce7fcf70SJocelyn Falempe 		if (crtc_state->gamma_lut)
1202ce7fcf70SJocelyn Falempe 			ast_crtc_set_gamma(ast,
1203ce7fcf70SJocelyn Falempe 					   ast_crtc_state->format,
1204ce7fcf70SJocelyn Falempe 					   crtc_state->gamma_lut->data);
1205ce7fcf70SJocelyn Falempe 		else
1206ce7fcf70SJocelyn Falempe 			ast_crtc_set_gamma_linear(ast, ast_crtc_state->format);
1207ce7fcf70SJocelyn Falempe 	}
1208594e9c04SKuoHsiang Chou 
1209594e9c04SKuoHsiang Chou 	//Set Aspeed Display-Port
12107f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_ASTDP_BIT)
1211594e9c04SKuoHsiang Chou 		ast_dp_set_mode(crtc, vbios_mode_info);
12128e3784dfSThomas Zimmermann }
12138e3784dfSThomas Zimmermann 
1214d95dcfc4SThomas Zimmermann static void ast_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state)
1215b48e1b6fSThomas Zimmermann {
121671d873ccSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
1217fa7dbd76SThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
1218d95dcfc4SThomas Zimmermann 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
12195638c82cSThomas Zimmermann 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
12205638c82cSThomas Zimmermann 	struct ast_vbios_mode_info *vbios_mode_info =
12215638c82cSThomas Zimmermann 		&ast_crtc_state->vbios_mode_info;
12225638c82cSThomas Zimmermann 	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
1223b48e1b6fSThomas Zimmermann 
1224e7d70cd4SThomas Zimmermann 	ast_set_vbios_mode_reg(ast, adjusted_mode, vbios_mode_info);
1225b48e1b6fSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa1, 0x06);
1226e7d70cd4SThomas Zimmermann 	ast_set_std_reg(ast, adjusted_mode, vbios_mode_info);
1227e7d70cd4SThomas Zimmermann 	ast_set_crtc_reg(ast, adjusted_mode, vbios_mode_info);
1228e7d70cd4SThomas Zimmermann 	ast_set_dclk_reg(ast, adjusted_mode, vbios_mode_info);
1229ae37025dSThomas Zimmermann 	ast_set_crtthd_reg(ast);
1230e7d70cd4SThomas Zimmermann 	ast_set_sync_reg(ast, adjusted_mode, vbios_mode_info);
1231b48e1b6fSThomas Zimmermann 
1232b48e1b6fSThomas Zimmermann 	ast_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
1233b48e1b6fSThomas Zimmermann }
1234b48e1b6fSThomas Zimmermann 
1235d95dcfc4SThomas Zimmermann static void ast_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state)
1236b48e1b6fSThomas Zimmermann {
1237d95dcfc4SThomas Zimmermann 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
123839edb287SThomas Zimmermann 	struct drm_device *dev = crtc->dev;
123939edb287SThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
124039edb287SThomas Zimmermann 
1241b48e1b6fSThomas Zimmermann 	ast_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
124239edb287SThomas Zimmermann 
124339edb287SThomas Zimmermann 	/*
124439edb287SThomas Zimmermann 	 * HW cursors require the underlying primary plane and CRTC to
124539edb287SThomas Zimmermann 	 * display a valid mode and image. This is not the case during
124639edb287SThomas Zimmermann 	 * full modeset operations. So we temporarily disable any active
124739edb287SThomas Zimmermann 	 * plane, including the HW cursor. Each plane's atomic_update()
124839edb287SThomas Zimmermann 	 * helper will re-enable it if necessary.
124939edb287SThomas Zimmermann 	 *
125039edb287SThomas Zimmermann 	 * We only do this during *full* modesets. It does not affect
125139edb287SThomas Zimmermann 	 * simple pageflips on the planes.
125239edb287SThomas Zimmermann 	 */
125339edb287SThomas Zimmermann 	drm_atomic_helper_disable_planes_on_crtc(old_crtc_state, false);
125439edb287SThomas Zimmermann 
125539edb287SThomas Zimmermann 	/*
125639edb287SThomas Zimmermann 	 * Ensure that no scanout takes place before reprogramming mode
125739edb287SThomas Zimmermann 	 * and format registers.
125839edb287SThomas Zimmermann 	 */
125939edb287SThomas Zimmermann 	ast_wait_for_vretrace(ast);
1260b48e1b6fSThomas Zimmermann }
1261312fec14SDave Airlie 
1262312fec14SDave Airlie static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
12636abbad2cSThomas Zimmermann 	.mode_valid = ast_crtc_helper_mode_valid,
1264b48e1b6fSThomas Zimmermann 	.atomic_check = ast_crtc_helper_atomic_check,
12658e3784dfSThomas Zimmermann 	.atomic_flush = ast_crtc_helper_atomic_flush,
1266b48e1b6fSThomas Zimmermann 	.atomic_enable = ast_crtc_helper_atomic_enable,
1267b48e1b6fSThomas Zimmermann 	.atomic_disable = ast_crtc_helper_atomic_disable,
1268312fec14SDave Airlie };
1269312fec14SDave Airlie 
1270f0adbc38SThomas Zimmermann static void ast_crtc_reset(struct drm_crtc *crtc)
1271f0adbc38SThomas Zimmermann {
1272f0adbc38SThomas Zimmermann 	struct ast_crtc_state *ast_state =
1273f0adbc38SThomas Zimmermann 		kzalloc(sizeof(*ast_state), GFP_KERNEL);
1274f0adbc38SThomas Zimmermann 
1275f0adbc38SThomas Zimmermann 	if (crtc->state)
1276f0adbc38SThomas Zimmermann 		crtc->funcs->atomic_destroy_state(crtc, crtc->state);
1277f0adbc38SThomas Zimmermann 
1278fea3fdf9SJiasheng Jiang 	if (ast_state)
1279f0adbc38SThomas Zimmermann 		__drm_atomic_helper_crtc_reset(crtc, &ast_state->base);
1280fea3fdf9SJiasheng Jiang 	else
1281fea3fdf9SJiasheng Jiang 		__drm_atomic_helper_crtc_reset(crtc, NULL);
1282f0adbc38SThomas Zimmermann }
1283f0adbc38SThomas Zimmermann 
128483be6a3cSThomas Zimmermann static struct drm_crtc_state *
128583be6a3cSThomas Zimmermann ast_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
128683be6a3cSThomas Zimmermann {
1287e7d70cd4SThomas Zimmermann 	struct ast_crtc_state *new_ast_state, *ast_state;
12881a19b4cbSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
128983be6a3cSThomas Zimmermann 
12901a19b4cbSThomas Zimmermann 	if (drm_WARN_ON(dev, !crtc->state))
129183be6a3cSThomas Zimmermann 		return NULL;
129283be6a3cSThomas Zimmermann 
129383be6a3cSThomas Zimmermann 	new_ast_state = kmalloc(sizeof(*new_ast_state), GFP_KERNEL);
129483be6a3cSThomas Zimmermann 	if (!new_ast_state)
129583be6a3cSThomas Zimmermann 		return NULL;
129683be6a3cSThomas Zimmermann 	__drm_atomic_helper_crtc_duplicate_state(crtc, &new_ast_state->base);
129783be6a3cSThomas Zimmermann 
1298e7d70cd4SThomas Zimmermann 	ast_state = to_ast_crtc_state(crtc->state);
1299e7d70cd4SThomas Zimmermann 
13003339fdf5SThomas Zimmermann 	new_ast_state->format = ast_state->format;
1301e7d70cd4SThomas Zimmermann 	memcpy(&new_ast_state->vbios_mode_info, &ast_state->vbios_mode_info,
1302e7d70cd4SThomas Zimmermann 	       sizeof(new_ast_state->vbios_mode_info));
1303e7d70cd4SThomas Zimmermann 
130483be6a3cSThomas Zimmermann 	return &new_ast_state->base;
130583be6a3cSThomas Zimmermann }
130683be6a3cSThomas Zimmermann 
130783be6a3cSThomas Zimmermann static void ast_crtc_atomic_destroy_state(struct drm_crtc *crtc,
130883be6a3cSThomas Zimmermann 					  struct drm_crtc_state *state)
130983be6a3cSThomas Zimmermann {
131083be6a3cSThomas Zimmermann 	struct ast_crtc_state *ast_state = to_ast_crtc_state(state);
131183be6a3cSThomas Zimmermann 
131283be6a3cSThomas Zimmermann 	__drm_atomic_helper_crtc_destroy_state(&ast_state->base);
131383be6a3cSThomas Zimmermann 	kfree(ast_state);
131483be6a3cSThomas Zimmermann }
131583be6a3cSThomas Zimmermann 
1316312fec14SDave Airlie static const struct drm_crtc_funcs ast_crtc_funcs = {
1317f0adbc38SThomas Zimmermann 	.reset = ast_crtc_reset,
13186a470dc2SThomas Zimmermann 	.destroy = drm_crtc_cleanup,
13194961eb60SThomas Zimmermann 	.set_config = drm_atomic_helper_set_config,
13204961eb60SThomas Zimmermann 	.page_flip = drm_atomic_helper_page_flip,
132183be6a3cSThomas Zimmermann 	.atomic_duplicate_state = ast_crtc_atomic_duplicate_state,
132283be6a3cSThomas Zimmermann 	.atomic_destroy_state = ast_crtc_atomic_destroy_state,
1323312fec14SDave Airlie };
1324312fec14SDave Airlie 
13257f5ccd44SRashika static int ast_crtc_init(struct drm_device *dev)
1326312fec14SDave Airlie {
1327fa7dbd76SThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
13286a470dc2SThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
1329a6ff807bSThomas Zimmermann 	int ret;
1330312fec14SDave Airlie 
1331f2fa5a99SThomas Zimmermann 	ret = drm_crtc_init_with_planes(dev, crtc, &ast->primary_plane.base,
1332a0ba992dSThomas Zimmermann 					&ast->cursor_plane.base, &ast_crtc_funcs,
133302f3bb75SThomas Zimmermann 					NULL);
1334a6ff807bSThomas Zimmermann 	if (ret)
13356a470dc2SThomas Zimmermann 		return ret;
1336a6ff807bSThomas Zimmermann 
1337ce7fcf70SJocelyn Falempe 	drm_mode_crtc_set_gamma_size(crtc, AST_LUT_SIZE);
1338ce7fcf70SJocelyn Falempe 	drm_crtc_enable_color_mgmt(crtc, 0, false, AST_LUT_SIZE);
1339ce7fcf70SJocelyn Falempe 
1340c35da0edSThomas Zimmermann 	drm_crtc_helper_add(crtc, &ast_crtc_helper_funcs);
1341c35da0edSThomas Zimmermann 
1342312fec14SDave Airlie 	return 0;
1343312fec14SDave Airlie }
1344312fec14SDave Airlie 
13454961eb60SThomas Zimmermann /*
1346b20384d9SThomas Zimmermann  * VGA Connector
13474961eb60SThomas Zimmermann  */
13484961eb60SThomas Zimmermann 
1349b20384d9SThomas Zimmermann static int ast_vga_connector_helper_get_modes(struct drm_connector *connector)
1350312fec14SDave Airlie {
1351b20384d9SThomas Zimmermann 	struct ast_vga_connector *ast_vga_connector = to_ast_vga_connector(connector);
1352f870231fSThomas Zimmermann 	struct drm_device *dev = connector->dev;
1353f870231fSThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
13543ab26eddSThomas Zimmermann 	struct edid *edid;
13553ab26eddSThomas Zimmermann 	int count;
1356312fec14SDave Airlie 
13573ab26eddSThomas Zimmermann 	if (!ast_vga_connector->i2c)
13583ab26eddSThomas Zimmermann 		goto err_drm_connector_update_edid_property;
1359312fec14SDave Airlie 
1360f870231fSThomas Zimmermann 	/*
1361f870231fSThomas Zimmermann 	 * Protect access to I/O registers from concurrent modesetting
1362f870231fSThomas Zimmermann 	 * by acquiring the I/O-register lock.
1363f870231fSThomas Zimmermann 	 */
1364f870231fSThomas Zimmermann 	mutex_lock(&ast->ioregs_lock);
1365f870231fSThomas Zimmermann 
1366b20384d9SThomas Zimmermann 	edid = drm_get_edid(connector, &ast_vga_connector->i2c->adapter);
136783c6620bSDave Airlie 	if (!edid)
1368f870231fSThomas Zimmermann 		goto err_mutex_unlock;
1369f870231fSThomas Zimmermann 
1370f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
1371312fec14SDave Airlie 
13723ab26eddSThomas Zimmermann 	count = drm_add_edid_modes(connector, edid);
137383c6620bSDave Airlie 	kfree(edid);
13743ab26eddSThomas Zimmermann 
13753ab26eddSThomas Zimmermann 	return count;
13763ab26eddSThomas Zimmermann 
1377f870231fSThomas Zimmermann err_mutex_unlock:
1378f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
13793ab26eddSThomas Zimmermann err_drm_connector_update_edid_property:
1380b20384d9SThomas Zimmermann 	drm_connector_update_edid_property(connector, NULL);
1381312fec14SDave Airlie 	return 0;
1382312fec14SDave Airlie }
1383312fec14SDave Airlie 
1384b20384d9SThomas Zimmermann static const struct drm_connector_helper_funcs ast_vga_connector_helper_funcs = {
1385b20384d9SThomas Zimmermann 	.get_modes = ast_vga_connector_helper_get_modes,
1386312fec14SDave Airlie };
1387312fec14SDave Airlie 
1388b20384d9SThomas Zimmermann static const struct drm_connector_funcs ast_vga_connector_funcs = {
13894961eb60SThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
1390312fec14SDave Airlie 	.fill_modes = drm_helper_probe_single_connector_modes,
1391a2cce09cSThomas Zimmermann 	.destroy = drm_connector_cleanup,
13924961eb60SThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
13934961eb60SThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1394312fec14SDave Airlie };
1395312fec14SDave Airlie 
1396a59b0264SThomas Zimmermann static int ast_vga_connector_init(struct drm_device *dev,
1397a59b0264SThomas Zimmermann 				  struct ast_vga_connector *ast_vga_connector)
1398312fec14SDave Airlie {
1399b20384d9SThomas Zimmermann 	struct drm_connector *connector = &ast_vga_connector->base;
14009285f09eSThomas Zimmermann 	int ret;
1401312fec14SDave Airlie 
1402b20384d9SThomas Zimmermann 	ast_vga_connector->i2c = ast_i2c_create(dev);
1403b20384d9SThomas Zimmermann 	if (!ast_vga_connector->i2c)
14041a19b4cbSThomas Zimmermann 		drm_err(dev, "failed to add ddc bus for connector\n");
1405350fd554SAndrzej Pietrasiewicz 
1406b20384d9SThomas Zimmermann 	if (ast_vga_connector->i2c)
1407b20384d9SThomas Zimmermann 		ret = drm_connector_init_with_ddc(dev, connector, &ast_vga_connector_funcs,
1408350fd554SAndrzej Pietrasiewicz 						  DRM_MODE_CONNECTOR_VGA,
1409b20384d9SThomas Zimmermann 						  &ast_vga_connector->i2c->adapter);
141055dc449aSThomas Zimmermann 	else
1411b20384d9SThomas Zimmermann 		ret = drm_connector_init(dev, connector, &ast_vga_connector_funcs,
141255dc449aSThomas Zimmermann 					 DRM_MODE_CONNECTOR_VGA);
14139285f09eSThomas Zimmermann 	if (ret)
14149285f09eSThomas Zimmermann 		return ret;
1415312fec14SDave Airlie 
1416b20384d9SThomas Zimmermann 	drm_connector_helper_add(connector, &ast_vga_connector_helper_funcs);
1417312fec14SDave Airlie 
1418312fec14SDave Airlie 	connector->interlace_allowed = 0;
1419312fec14SDave Airlie 	connector->doublescan_allowed = 0;
1420312fec14SDave Airlie 
1421595cb5e0SKim Phillips 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1422312fec14SDave Airlie 
1423a59b0264SThomas Zimmermann 	return 0;
1424a59b0264SThomas Zimmermann }
1425a59b0264SThomas Zimmermann 
1426a59b0264SThomas Zimmermann static int ast_vga_output_init(struct ast_private *ast)
1427a59b0264SThomas Zimmermann {
1428a59b0264SThomas Zimmermann 	struct drm_device *dev = &ast->base;
1429a59b0264SThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
1430a59b0264SThomas Zimmermann 	struct drm_encoder *encoder = &ast->output.vga.encoder;
1431a59b0264SThomas Zimmermann 	struct ast_vga_connector *ast_vga_connector = &ast->output.vga.vga_connector;
1432a59b0264SThomas Zimmermann 	struct drm_connector *connector = &ast_vga_connector->base;
1433a59b0264SThomas Zimmermann 	int ret;
1434a59b0264SThomas Zimmermann 
1435a59b0264SThomas Zimmermann 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_DAC);
1436a59b0264SThomas Zimmermann 	if (ret)
1437a59b0264SThomas Zimmermann 		return ret;
1438f665147cSThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1439a59b0264SThomas Zimmermann 
1440a59b0264SThomas Zimmermann 	ret = ast_vga_connector_init(dev, ast_vga_connector);
1441a59b0264SThomas Zimmermann 	if (ret)
1442a59b0264SThomas Zimmermann 		return ret;
1443a59b0264SThomas Zimmermann 
1444a59b0264SThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
1445a59b0264SThomas Zimmermann 	if (ret)
1446a59b0264SThomas Zimmermann 		return ret;
1447312fec14SDave Airlie 
1448312fec14SDave Airlie 	return 0;
1449312fec14SDave Airlie }
1450312fec14SDave Airlie 
1451e6949ff3SThomas Zimmermann /*
14525e78d59aSThomas Zimmermann  * SIL164 Connector
14535e78d59aSThomas Zimmermann  */
14545e78d59aSThomas Zimmermann 
14555e78d59aSThomas Zimmermann static int ast_sil164_connector_helper_get_modes(struct drm_connector *connector)
14565e78d59aSThomas Zimmermann {
14575e78d59aSThomas Zimmermann 	struct ast_sil164_connector *ast_sil164_connector = to_ast_sil164_connector(connector);
1458f870231fSThomas Zimmermann 	struct drm_device *dev = connector->dev;
1459f870231fSThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
14605e78d59aSThomas Zimmermann 	struct edid *edid;
14615e78d59aSThomas Zimmermann 	int count;
14625e78d59aSThomas Zimmermann 
14635e78d59aSThomas Zimmermann 	if (!ast_sil164_connector->i2c)
14645e78d59aSThomas Zimmermann 		goto err_drm_connector_update_edid_property;
14655e78d59aSThomas Zimmermann 
1466f870231fSThomas Zimmermann 	/*
1467f870231fSThomas Zimmermann 	 * Protect access to I/O registers from concurrent modesetting
1468f870231fSThomas Zimmermann 	 * by acquiring the I/O-register lock.
1469f870231fSThomas Zimmermann 	 */
1470f870231fSThomas Zimmermann 	mutex_lock(&ast->ioregs_lock);
1471f870231fSThomas Zimmermann 
14725e78d59aSThomas Zimmermann 	edid = drm_get_edid(connector, &ast_sil164_connector->i2c->adapter);
14735e78d59aSThomas Zimmermann 	if (!edid)
1474f870231fSThomas Zimmermann 		goto err_mutex_unlock;
1475f870231fSThomas Zimmermann 
1476f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
14775e78d59aSThomas Zimmermann 
14785e78d59aSThomas Zimmermann 	count = drm_add_edid_modes(connector, edid);
14795e78d59aSThomas Zimmermann 	kfree(edid);
14805e78d59aSThomas Zimmermann 
14815e78d59aSThomas Zimmermann 	return count;
14825e78d59aSThomas Zimmermann 
1483f870231fSThomas Zimmermann err_mutex_unlock:
1484f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
14855e78d59aSThomas Zimmermann err_drm_connector_update_edid_property:
14865e78d59aSThomas Zimmermann 	drm_connector_update_edid_property(connector, NULL);
14875e78d59aSThomas Zimmermann 	return 0;
14885e78d59aSThomas Zimmermann }
14895e78d59aSThomas Zimmermann 
14905e78d59aSThomas Zimmermann static const struct drm_connector_helper_funcs ast_sil164_connector_helper_funcs = {
14915e78d59aSThomas Zimmermann 	.get_modes = ast_sil164_connector_helper_get_modes,
14925e78d59aSThomas Zimmermann };
14935e78d59aSThomas Zimmermann 
14945e78d59aSThomas Zimmermann static const struct drm_connector_funcs ast_sil164_connector_funcs = {
14955e78d59aSThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
14965e78d59aSThomas Zimmermann 	.fill_modes = drm_helper_probe_single_connector_modes,
14975e78d59aSThomas Zimmermann 	.destroy = drm_connector_cleanup,
14985e78d59aSThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
14995e78d59aSThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
15005e78d59aSThomas Zimmermann };
15015e78d59aSThomas Zimmermann 
15025e78d59aSThomas Zimmermann static int ast_sil164_connector_init(struct drm_device *dev,
15035e78d59aSThomas Zimmermann 				     struct ast_sil164_connector *ast_sil164_connector)
15045e78d59aSThomas Zimmermann {
15055e78d59aSThomas Zimmermann 	struct drm_connector *connector = &ast_sil164_connector->base;
15065e78d59aSThomas Zimmermann 	int ret;
15075e78d59aSThomas Zimmermann 
15085e78d59aSThomas Zimmermann 	ast_sil164_connector->i2c = ast_i2c_create(dev);
15095e78d59aSThomas Zimmermann 	if (!ast_sil164_connector->i2c)
15105e78d59aSThomas Zimmermann 		drm_err(dev, "failed to add ddc bus for connector\n");
15115e78d59aSThomas Zimmermann 
15125e78d59aSThomas Zimmermann 	if (ast_sil164_connector->i2c)
15135e78d59aSThomas Zimmermann 		ret = drm_connector_init_with_ddc(dev, connector, &ast_sil164_connector_funcs,
15145e78d59aSThomas Zimmermann 						  DRM_MODE_CONNECTOR_DVII,
15155e78d59aSThomas Zimmermann 						  &ast_sil164_connector->i2c->adapter);
15165e78d59aSThomas Zimmermann 	else
15175e78d59aSThomas Zimmermann 		ret = drm_connector_init(dev, connector, &ast_sil164_connector_funcs,
15185e78d59aSThomas Zimmermann 					 DRM_MODE_CONNECTOR_DVII);
15195e78d59aSThomas Zimmermann 	if (ret)
15205e78d59aSThomas Zimmermann 		return ret;
15215e78d59aSThomas Zimmermann 
15225e78d59aSThomas Zimmermann 	drm_connector_helper_add(connector, &ast_sil164_connector_helper_funcs);
15235e78d59aSThomas Zimmermann 
15245e78d59aSThomas Zimmermann 	connector->interlace_allowed = 0;
15255e78d59aSThomas Zimmermann 	connector->doublescan_allowed = 0;
15265e78d59aSThomas Zimmermann 
15275e78d59aSThomas Zimmermann 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
15285e78d59aSThomas Zimmermann 
15295e78d59aSThomas Zimmermann 	return 0;
15305e78d59aSThomas Zimmermann }
15315e78d59aSThomas Zimmermann 
15325e78d59aSThomas Zimmermann static int ast_sil164_output_init(struct ast_private *ast)
15335e78d59aSThomas Zimmermann {
15345e78d59aSThomas Zimmermann 	struct drm_device *dev = &ast->base;
15355e78d59aSThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
15365e78d59aSThomas Zimmermann 	struct drm_encoder *encoder = &ast->output.sil164.encoder;
15375e78d59aSThomas Zimmermann 	struct ast_sil164_connector *ast_sil164_connector = &ast->output.sil164.sil164_connector;
15385e78d59aSThomas Zimmermann 	struct drm_connector *connector = &ast_sil164_connector->base;
15395e78d59aSThomas Zimmermann 	int ret;
15405e78d59aSThomas Zimmermann 
15415e78d59aSThomas Zimmermann 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
15425e78d59aSThomas Zimmermann 	if (ret)
15435e78d59aSThomas Zimmermann 		return ret;
15445e78d59aSThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
15455e78d59aSThomas Zimmermann 
15465e78d59aSThomas Zimmermann 	ret = ast_sil164_connector_init(dev, ast_sil164_connector);
15475e78d59aSThomas Zimmermann 	if (ret)
15485e78d59aSThomas Zimmermann 		return ret;
15495e78d59aSThomas Zimmermann 
15505e78d59aSThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
15515e78d59aSThomas Zimmermann 	if (ret)
15525e78d59aSThomas Zimmermann 		return ret;
15535e78d59aSThomas Zimmermann 
15545e78d59aSThomas Zimmermann 	return 0;
15555e78d59aSThomas Zimmermann }
15565e78d59aSThomas Zimmermann 
15575e78d59aSThomas Zimmermann /*
15583ab26eddSThomas Zimmermann  * DP501 Connector
15593ab26eddSThomas Zimmermann  */
15603ab26eddSThomas Zimmermann 
15613ab26eddSThomas Zimmermann static int ast_dp501_connector_helper_get_modes(struct drm_connector *connector)
15623ab26eddSThomas Zimmermann {
15633ab26eddSThomas Zimmermann 	void *edid;
15643ab26eddSThomas Zimmermann 	bool succ;
15653ab26eddSThomas Zimmermann 	int count;
15663ab26eddSThomas Zimmermann 
15673ab26eddSThomas Zimmermann 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
15683ab26eddSThomas Zimmermann 	if (!edid)
15693ab26eddSThomas Zimmermann 		goto err_drm_connector_update_edid_property;
15703ab26eddSThomas Zimmermann 
15713ab26eddSThomas Zimmermann 	succ = ast_dp501_read_edid(connector->dev, edid);
15723ab26eddSThomas Zimmermann 	if (!succ)
15733ab26eddSThomas Zimmermann 		goto err_kfree;
15743ab26eddSThomas Zimmermann 
15753ab26eddSThomas Zimmermann 	drm_connector_update_edid_property(connector, edid);
15763ab26eddSThomas Zimmermann 	count = drm_add_edid_modes(connector, edid);
15773ab26eddSThomas Zimmermann 	kfree(edid);
15783ab26eddSThomas Zimmermann 
15793ab26eddSThomas Zimmermann 	return count;
15803ab26eddSThomas Zimmermann 
15813ab26eddSThomas Zimmermann err_kfree:
15823ab26eddSThomas Zimmermann 	kfree(edid);
15833ab26eddSThomas Zimmermann err_drm_connector_update_edid_property:
15843ab26eddSThomas Zimmermann 	drm_connector_update_edid_property(connector, NULL);
15853ab26eddSThomas Zimmermann 	return 0;
15863ab26eddSThomas Zimmermann }
15873ab26eddSThomas Zimmermann 
15883ab26eddSThomas Zimmermann static const struct drm_connector_helper_funcs ast_dp501_connector_helper_funcs = {
15893ab26eddSThomas Zimmermann 	.get_modes = ast_dp501_connector_helper_get_modes,
15903ab26eddSThomas Zimmermann };
15913ab26eddSThomas Zimmermann 
15923ab26eddSThomas Zimmermann static const struct drm_connector_funcs ast_dp501_connector_funcs = {
15933ab26eddSThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
15943ab26eddSThomas Zimmermann 	.fill_modes = drm_helper_probe_single_connector_modes,
15953ab26eddSThomas Zimmermann 	.destroy = drm_connector_cleanup,
15963ab26eddSThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
15973ab26eddSThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
15983ab26eddSThomas Zimmermann };
15993ab26eddSThomas Zimmermann 
16003ab26eddSThomas Zimmermann static int ast_dp501_connector_init(struct drm_device *dev, struct drm_connector *connector)
16013ab26eddSThomas Zimmermann {
16023ab26eddSThomas Zimmermann 	int ret;
16033ab26eddSThomas Zimmermann 
16043ab26eddSThomas Zimmermann 	ret = drm_connector_init(dev, connector, &ast_dp501_connector_funcs,
16053ab26eddSThomas Zimmermann 				 DRM_MODE_CONNECTOR_DisplayPort);
16063ab26eddSThomas Zimmermann 	if (ret)
16073ab26eddSThomas Zimmermann 		return ret;
16083ab26eddSThomas Zimmermann 
16093ab26eddSThomas Zimmermann 	drm_connector_helper_add(connector, &ast_dp501_connector_helper_funcs);
16103ab26eddSThomas Zimmermann 
16113ab26eddSThomas Zimmermann 	connector->interlace_allowed = 0;
16123ab26eddSThomas Zimmermann 	connector->doublescan_allowed = 0;
16133ab26eddSThomas Zimmermann 
16143ab26eddSThomas Zimmermann 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
16153ab26eddSThomas Zimmermann 
16163ab26eddSThomas Zimmermann 	return 0;
16173ab26eddSThomas Zimmermann }
16183ab26eddSThomas Zimmermann 
16193ab26eddSThomas Zimmermann static int ast_dp501_output_init(struct ast_private *ast)
16203ab26eddSThomas Zimmermann {
16213ab26eddSThomas Zimmermann 	struct drm_device *dev = &ast->base;
16223ab26eddSThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
16233ab26eddSThomas Zimmermann 	struct drm_encoder *encoder = &ast->output.dp501.encoder;
16243ab26eddSThomas Zimmermann 	struct drm_connector *connector = &ast->output.dp501.connector;
16253ab26eddSThomas Zimmermann 	int ret;
16263ab26eddSThomas Zimmermann 
16273ab26eddSThomas Zimmermann 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
16283ab26eddSThomas Zimmermann 	if (ret)
16293ab26eddSThomas Zimmermann 		return ret;
16303ab26eddSThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
16313ab26eddSThomas Zimmermann 
16323ab26eddSThomas Zimmermann 	ret = ast_dp501_connector_init(dev, connector);
16333ab26eddSThomas Zimmermann 	if (ret)
16343ab26eddSThomas Zimmermann 		return ret;
16353ab26eddSThomas Zimmermann 
16363ab26eddSThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
16373ab26eddSThomas Zimmermann 	if (ret)
16383ab26eddSThomas Zimmermann 		return ret;
1639312fec14SDave Airlie 
1640312fec14SDave Airlie 	return 0;
16417f5ccd44SRashika }
1642312fec14SDave Airlie 
1643e6949ff3SThomas Zimmermann /*
1644594e9c04SKuoHsiang Chou  * ASPEED Display-Port Connector
1645594e9c04SKuoHsiang Chou  */
1646594e9c04SKuoHsiang Chou 
1647594e9c04SKuoHsiang Chou static int ast_astdp_connector_helper_get_modes(struct drm_connector *connector)
1648594e9c04SKuoHsiang Chou {
1649594e9c04SKuoHsiang Chou 	void *edid;
1650594e9c04SKuoHsiang Chou 
1651594e9c04SKuoHsiang Chou 	int succ;
1652594e9c04SKuoHsiang Chou 	int count;
1653594e9c04SKuoHsiang Chou 
1654594e9c04SKuoHsiang Chou 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1655594e9c04SKuoHsiang Chou 	if (!edid)
1656594e9c04SKuoHsiang Chou 		goto err_drm_connector_update_edid_property;
1657594e9c04SKuoHsiang Chou 
1658594e9c04SKuoHsiang Chou 	succ = ast_astdp_read_edid(connector->dev, edid);
1659594e9c04SKuoHsiang Chou 	if (succ < 0)
1660594e9c04SKuoHsiang Chou 		goto err_kfree;
1661594e9c04SKuoHsiang Chou 
1662594e9c04SKuoHsiang Chou 	drm_connector_update_edid_property(connector, edid);
1663594e9c04SKuoHsiang Chou 	count = drm_add_edid_modes(connector, edid);
1664594e9c04SKuoHsiang Chou 	kfree(edid);
1665594e9c04SKuoHsiang Chou 
1666594e9c04SKuoHsiang Chou 	return count;
1667594e9c04SKuoHsiang Chou 
1668594e9c04SKuoHsiang Chou err_kfree:
1669594e9c04SKuoHsiang Chou 	kfree(edid);
1670594e9c04SKuoHsiang Chou err_drm_connector_update_edid_property:
1671594e9c04SKuoHsiang Chou 	drm_connector_update_edid_property(connector, NULL);
1672594e9c04SKuoHsiang Chou 	return 0;
1673594e9c04SKuoHsiang Chou }
1674594e9c04SKuoHsiang Chou 
1675594e9c04SKuoHsiang Chou static const struct drm_connector_helper_funcs ast_astdp_connector_helper_funcs = {
1676594e9c04SKuoHsiang Chou 	.get_modes = ast_astdp_connector_helper_get_modes,
1677594e9c04SKuoHsiang Chou };
1678594e9c04SKuoHsiang Chou 
1679594e9c04SKuoHsiang Chou static const struct drm_connector_funcs ast_astdp_connector_funcs = {
1680594e9c04SKuoHsiang Chou 	.reset = drm_atomic_helper_connector_reset,
1681594e9c04SKuoHsiang Chou 	.fill_modes = drm_helper_probe_single_connector_modes,
1682594e9c04SKuoHsiang Chou 	.destroy = drm_connector_cleanup,
1683594e9c04SKuoHsiang Chou 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1684594e9c04SKuoHsiang Chou 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1685594e9c04SKuoHsiang Chou };
1686594e9c04SKuoHsiang Chou 
1687594e9c04SKuoHsiang Chou static int ast_astdp_connector_init(struct drm_device *dev, struct drm_connector *connector)
1688594e9c04SKuoHsiang Chou {
1689594e9c04SKuoHsiang Chou 	int ret;
1690594e9c04SKuoHsiang Chou 
1691594e9c04SKuoHsiang Chou 	ret = drm_connector_init(dev, connector, &ast_astdp_connector_funcs,
1692594e9c04SKuoHsiang Chou 				 DRM_MODE_CONNECTOR_DisplayPort);
1693594e9c04SKuoHsiang Chou 	if (ret)
1694594e9c04SKuoHsiang Chou 		return ret;
1695594e9c04SKuoHsiang Chou 
1696594e9c04SKuoHsiang Chou 	drm_connector_helper_add(connector, &ast_astdp_connector_helper_funcs);
1697594e9c04SKuoHsiang Chou 
1698594e9c04SKuoHsiang Chou 	connector->interlace_allowed = 0;
1699594e9c04SKuoHsiang Chou 	connector->doublescan_allowed = 0;
1700594e9c04SKuoHsiang Chou 
1701594e9c04SKuoHsiang Chou 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1702594e9c04SKuoHsiang Chou 
1703594e9c04SKuoHsiang Chou 	return 0;
1704594e9c04SKuoHsiang Chou }
1705594e9c04SKuoHsiang Chou 
1706594e9c04SKuoHsiang Chou static int ast_astdp_output_init(struct ast_private *ast)
1707594e9c04SKuoHsiang Chou {
1708594e9c04SKuoHsiang Chou 	struct drm_device *dev = &ast->base;
1709594e9c04SKuoHsiang Chou 	struct drm_crtc *crtc = &ast->crtc;
1710594e9c04SKuoHsiang Chou 	struct drm_encoder *encoder = &ast->output.astdp.encoder;
1711594e9c04SKuoHsiang Chou 	struct drm_connector *connector = &ast->output.astdp.connector;
1712594e9c04SKuoHsiang Chou 	int ret;
1713594e9c04SKuoHsiang Chou 
1714594e9c04SKuoHsiang Chou 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
1715594e9c04SKuoHsiang Chou 	if (ret)
1716594e9c04SKuoHsiang Chou 		return ret;
1717594e9c04SKuoHsiang Chou 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1718594e9c04SKuoHsiang Chou 
1719594e9c04SKuoHsiang Chou 	ret = ast_astdp_connector_init(dev, connector);
1720594e9c04SKuoHsiang Chou 	if (ret)
1721594e9c04SKuoHsiang Chou 		return ret;
1722594e9c04SKuoHsiang Chou 
1723594e9c04SKuoHsiang Chou 	ret = drm_connector_attach_encoder(connector, encoder);
1724594e9c04SKuoHsiang Chou 	if (ret)
1725594e9c04SKuoHsiang Chou 		return ret;
1726594e9c04SKuoHsiang Chou 
1727594e9c04SKuoHsiang Chou 	return 0;
1728594e9c04SKuoHsiang Chou }
1729594e9c04SKuoHsiang Chou 
1730594e9c04SKuoHsiang Chou /*
1731e6949ff3SThomas Zimmermann  * Mode config
1732e6949ff3SThomas Zimmermann  */
1733e6949ff3SThomas Zimmermann 
17341fe18215SThomas Zimmermann static void ast_mode_config_helper_atomic_commit_tail(struct drm_atomic_state *state)
17351fe18215SThomas Zimmermann {
17361fe18215SThomas Zimmermann 	struct ast_private *ast = to_ast_private(state->dev);
17371fe18215SThomas Zimmermann 
17381fe18215SThomas Zimmermann 	/*
17391fe18215SThomas Zimmermann 	 * Concurrent operations could possibly trigger a call to
17401fe18215SThomas Zimmermann 	 * drm_connector_helper_funcs.get_modes by trying to read the
17411fe18215SThomas Zimmermann 	 * display modes. Protect access to I/O registers by acquiring
17421fe18215SThomas Zimmermann 	 * the I/O-register lock. Released in atomic_flush().
17431fe18215SThomas Zimmermann 	 */
17441fe18215SThomas Zimmermann 	mutex_lock(&ast->ioregs_lock);
17451fe18215SThomas Zimmermann 	drm_atomic_helper_commit_tail_rpm(state);
17461fe18215SThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
17471fe18215SThomas Zimmermann }
17481fe18215SThomas Zimmermann 
1749b20384d9SThomas Zimmermann static const struct drm_mode_config_helper_funcs ast_mode_config_helper_funcs = {
17501fe18215SThomas Zimmermann 	.atomic_commit_tail = ast_mode_config_helper_atomic_commit_tail,
17512f0ddd89SThomas Zimmermann };
17522f0ddd89SThomas Zimmermann 
1753f2fa5a99SThomas Zimmermann static enum drm_mode_status ast_mode_config_mode_valid(struct drm_device *dev,
1754f2fa5a99SThomas Zimmermann 						       const struct drm_display_mode *mode)
1755f2fa5a99SThomas Zimmermann {
1756f2fa5a99SThomas Zimmermann 	static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1757f2fa5a99SThomas Zimmermann 	struct ast_private *ast = to_ast_private(dev);
1758f2fa5a99SThomas Zimmermann 	unsigned long fbsize, fbpages, max_fbpages;
1759f2fa5a99SThomas Zimmermann 
1760f2fa5a99SThomas Zimmermann 	max_fbpages = (ast->vram_fb_available) >> PAGE_SHIFT;
1761f2fa5a99SThomas Zimmermann 
1762f2fa5a99SThomas Zimmermann 	fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1763f2fa5a99SThomas Zimmermann 	fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1764f2fa5a99SThomas Zimmermann 
1765f2fa5a99SThomas Zimmermann 	if (fbpages > max_fbpages)
1766f2fa5a99SThomas Zimmermann 		return MODE_MEM;
1767f2fa5a99SThomas Zimmermann 
1768f2fa5a99SThomas Zimmermann 	return MODE_OK;
1769f2fa5a99SThomas Zimmermann }
1770f2fa5a99SThomas Zimmermann 
1771e6949ff3SThomas Zimmermann static const struct drm_mode_config_funcs ast_mode_config_funcs = {
1772f2fa5a99SThomas Zimmermann 	.fb_create = drm_gem_fb_create_with_dirty,
1773f2fa5a99SThomas Zimmermann 	.mode_valid = ast_mode_config_mode_valid,
1774e6949ff3SThomas Zimmermann 	.atomic_check = drm_atomic_helper_check,
1775e6949ff3SThomas Zimmermann 	.atomic_commit = drm_atomic_helper_commit,
1776e6949ff3SThomas Zimmermann };
1777e6949ff3SThomas Zimmermann 
1778e6949ff3SThomas Zimmermann int ast_mode_config_init(struct ast_private *ast)
1779312fec14SDave Airlie {
1780e0f5a738SThomas Zimmermann 	struct drm_device *dev = &ast->base;
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 
1792e6949ff3SThomas Zimmermann 	if (ast->chip == AST2100 ||
1793e6949ff3SThomas Zimmermann 	    ast->chip == AST2200 ||
1794e6949ff3SThomas Zimmermann 	    ast->chip == AST2300 ||
1795e6949ff3SThomas Zimmermann 	    ast->chip == AST2400 ||
1796594e9c04SKuoHsiang Chou 	    ast->chip == AST2500 ||
1797594e9c04SKuoHsiang Chou 	    ast->chip == AST2600) {
1798e6949ff3SThomas Zimmermann 		dev->mode_config.max_width = 1920;
1799e6949ff3SThomas Zimmermann 		dev->mode_config.max_height = 2048;
1800e6949ff3SThomas Zimmermann 	} else {
1801e6949ff3SThomas Zimmermann 		dev->mode_config.max_width = 1600;
1802e6949ff3SThomas Zimmermann 		dev->mode_config.max_height = 1200;
1803e6949ff3SThomas Zimmermann 	}
1804e6949ff3SThomas Zimmermann 
18052f0ddd89SThomas Zimmermann 	dev->mode_config.helper_private = &ast_mode_config_helper_funcs;
18062f0ddd89SThomas Zimmermann 
1807616048afSThomas Zimmermann 	ret = ast_primary_plane_init(ast);
1808616048afSThomas Zimmermann 	if (ret)
180902f3bb75SThomas Zimmermann 		return ret;
1810616048afSThomas Zimmermann 
1811616048afSThomas Zimmermann 	ret = ast_cursor_plane_init(ast);
1812616048afSThomas Zimmermann 	if (ret)
1813616048afSThomas Zimmermann 		return ret;
181402f3bb75SThomas Zimmermann 
1815312fec14SDave Airlie 	ast_crtc_init(dev);
1816a59b0264SThomas Zimmermann 
18177f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_NONE_BIT) {
1818a59b0264SThomas Zimmermann 		ret = ast_vga_output_init(ast);
1819a59b0264SThomas Zimmermann 		if (ret)
1820a59b0264SThomas Zimmermann 			return ret;
18217f35680aSThomas Zimmermann 	}
18227f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_SIL164_BIT) {
18237f35680aSThomas Zimmermann 		ret = ast_sil164_output_init(ast);
18247f35680aSThomas Zimmermann 		if (ret)
18257f35680aSThomas Zimmermann 			return ret;
18267f35680aSThomas Zimmermann 	}
18277f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_DP501_BIT) {
18287f35680aSThomas Zimmermann 		ret = ast_dp501_output_init(ast);
18297f35680aSThomas Zimmermann 		if (ret)
18307f35680aSThomas Zimmermann 			return ret;
18317f35680aSThomas Zimmermann 	}
18327f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
18337f35680aSThomas Zimmermann 		ret = ast_astdp_output_init(ast);
18347f35680aSThomas Zimmermann 		if (ret)
18357f35680aSThomas Zimmermann 			return ret;
18367f35680aSThomas Zimmermann 	}
1837a6ff807bSThomas Zimmermann 
1838e6949ff3SThomas Zimmermann 	drm_mode_config_reset(dev);
1839e6949ff3SThomas Zimmermann 
1840312fec14SDave Airlie 	return 0;
1841312fec14SDave Airlie }
1842