xref: /openbmc/linux/drivers/gpu/drm/ast/ast_mode.c (revision f1a0e42f)
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 
ast_load_palette_index(struct ast_device * ast,u8 index,u8 red,u8 green,u8 blue)5437b42cf9SThomas Zimmermann static inline void ast_load_palette_index(struct ast_device *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 
ast_crtc_set_gamma_linear(struct ast_device * ast,const struct drm_format_info * format)6837b42cf9SThomas Zimmermann static void ast_crtc_set_gamma_linear(struct ast_device *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 
ast_crtc_set_gamma(struct ast_device * ast,const struct drm_format_info * format,struct drm_color_lut * lut)8737b42cf9SThomas Zimmermann static void ast_crtc_set_gamma(struct ast_device *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 
ast_get_vbios_mode_info(const struct drm_format_info * format,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode,struct ast_vbios_mode_info * vbios_mode)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 
ast_set_vbios_color_reg(struct ast_device * ast,const struct drm_format_info * format,const struct ast_vbios_mode_info * vbios_mode)23537b42cf9SThomas Zimmermann static void ast_set_vbios_color_reg(struct ast_device *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 
ast_set_vbios_mode_reg(struct ast_device * ast,const struct drm_display_mode * adjusted_mode,const struct ast_vbios_mode_info * vbios_mode)26637b42cf9SThomas Zimmermann static void ast_set_vbios_mode_reg(struct ast_device *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 
ast_set_std_reg(struct ast_device * ast,struct drm_display_mode * mode,struct ast_vbios_mode_info * vbios_mode)29037b42cf9SThomas Zimmermann static void ast_set_std_reg(struct ast_device *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 
ast_set_crtc_reg(struct ast_device * ast,struct drm_display_mode * mode,struct ast_vbios_mode_info * vbios_mode)33837b42cf9SThomas Zimmermann static void ast_set_crtc_reg(struct ast_device *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 
345ecf64579SThomas Zimmermann 	if ((IS_AST_GEN6(ast) || IS_AST_GEN7(ast)) &&
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);
387ecf64579SThomas Zimmermann 	if (IS_AST_GEN7(ast) && (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 
ast_set_offset_reg(struct ast_device * ast,struct drm_framebuffer * fb)45337b42cf9SThomas Zimmermann static void ast_set_offset_reg(struct ast_device *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 
ast_set_dclk_reg(struct ast_device * ast,struct drm_display_mode * mode,struct ast_vbios_mode_info * vbios_mode)46337b42cf9SThomas Zimmermann static void ast_set_dclk_reg(struct ast_device *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 
469ecf64579SThomas Zimmermann 	if (IS_AST_GEN6(ast) || IS_AST_GEN7(ast))
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 
ast_set_color_reg(struct ast_device * ast,const struct drm_format_info * format)48137b42cf9SThomas Zimmermann static void ast_set_color_reg(struct ast_device *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 
ast_set_crtthd_reg(struct ast_device * ast)51037b42cf9SThomas Zimmermann static void ast_set_crtthd_reg(struct ast_device *ast)
5110d45ad98SThomas Zimmermann {
512312fec14SDave Airlie 	/* Set Threshold */
513ecf64579SThomas Zimmermann 	if (IS_AST_GEN7(ast)) {
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);
516ecf64579SThomas Zimmermann 	} else if (IS_AST_GEN6(ast) || IS_AST_GEN5(ast) || IS_AST_GEN4(ast)) {
517312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x78);
518312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x60);
519ecf64579SThomas Zimmermann 	} else if (IS_AST_GEN3(ast) || IS_AST_GEN2(ast)) {
520312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x3f);
521312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x2f);
522312fec14SDave Airlie 	} else {
523312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x2f);
524312fec14SDave Airlie 		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x1f);
525312fec14SDave Airlie 	}
526312fec14SDave Airlie }
527312fec14SDave Airlie 
ast_set_sync_reg(struct ast_device * ast,struct drm_display_mode * mode,struct ast_vbios_mode_info * vbios_mode)52837b42cf9SThomas Zimmermann static void ast_set_sync_reg(struct ast_device *ast,
529ae37025dSThomas Zimmermann 			     struct drm_display_mode *mode,
530312fec14SDave Airlie 			     struct ast_vbios_mode_info *vbios_mode)
531312fec14SDave Airlie {
532312fec14SDave Airlie 	u8 jreg;
533312fec14SDave Airlie 
534312fec14SDave Airlie 	jreg  = ast_io_read8(ast, AST_IO_MISC_PORT_READ);
53594d12b13SY.C. Chen 	jreg &= ~0xC0;
5366c9bd443SGregory Williams 	if (vbios_mode->enh_table->flags & NVSync)
5376c9bd443SGregory Williams 		jreg |= 0x80;
5386c9bd443SGregory Williams 	if (vbios_mode->enh_table->flags & NHSync)
5396c9bd443SGregory Williams 		jreg |= 0x40;
540312fec14SDave Airlie 	ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
541312fec14SDave Airlie }
542312fec14SDave Airlie 
ast_set_start_address_crt1(struct ast_device * ast,unsigned int offset)54337b42cf9SThomas Zimmermann static void ast_set_start_address_crt1(struct ast_device *ast,
5446c9bd443SGregory Williams 				       unsigned int offset)
545312fec14SDave Airlie {
546312fec14SDave Airlie 	u32 addr;
547312fec14SDave Airlie 
548312fec14SDave Airlie 	addr = offset >> 2;
549312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0d, (u8)(addr & 0xff));
550312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0c, (u8)((addr >> 8) & 0xff));
551312fec14SDave Airlie 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xaf, (u8)((addr >> 16) & 0xff));
552312fec14SDave Airlie 
553312fec14SDave Airlie }
554312fec14SDave Airlie 
ast_wait_for_vretrace(struct ast_device * ast)55537b42cf9SThomas Zimmermann static void ast_wait_for_vretrace(struct ast_device *ast)
55639edb287SThomas Zimmermann {
55739edb287SThomas Zimmermann 	unsigned long timeout = jiffies + HZ;
55839edb287SThomas Zimmermann 	u8 vgair1;
55939edb287SThomas Zimmermann 
56039edb287SThomas Zimmermann 	do {
56139edb287SThomas Zimmermann 		vgair1 = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
56239edb287SThomas Zimmermann 	} while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout));
56339edb287SThomas Zimmermann }
56439edb287SThomas Zimmermann 
565a6ff807bSThomas Zimmermann /*
566f2fa5a99SThomas Zimmermann  * Planes
567f2fa5a99SThomas Zimmermann  */
568f2fa5a99SThomas Zimmermann 
ast_plane_init(struct drm_device * dev,struct ast_plane * ast_plane,void __iomem * vaddr,u64 offset,unsigned long size,uint32_t possible_crtcs,const struct drm_plane_funcs * funcs,const uint32_t * formats,unsigned int format_count,const uint64_t * format_modifiers,enum drm_plane_type type)569f2fa5a99SThomas Zimmermann static int ast_plane_init(struct drm_device *dev, struct ast_plane *ast_plane,
570f2fa5a99SThomas Zimmermann 			  void __iomem *vaddr, u64 offset, unsigned long size,
571f2fa5a99SThomas Zimmermann 			  uint32_t possible_crtcs,
572f2fa5a99SThomas Zimmermann 			  const struct drm_plane_funcs *funcs,
573f2fa5a99SThomas Zimmermann 			  const uint32_t *formats, unsigned int format_count,
574f2fa5a99SThomas Zimmermann 			  const uint64_t *format_modifiers,
575f2fa5a99SThomas Zimmermann 			  enum drm_plane_type type)
576f2fa5a99SThomas Zimmermann {
577f2fa5a99SThomas Zimmermann 	struct drm_plane *plane = &ast_plane->base;
578f2fa5a99SThomas Zimmermann 
579f2fa5a99SThomas Zimmermann 	ast_plane->vaddr = vaddr;
580f2fa5a99SThomas Zimmermann 	ast_plane->offset = offset;
581f2fa5a99SThomas Zimmermann 	ast_plane->size = size;
582f2fa5a99SThomas Zimmermann 
583f2fa5a99SThomas Zimmermann 	return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
584f2fa5a99SThomas Zimmermann 					formats, format_count, format_modifiers,
585f2fa5a99SThomas Zimmermann 					type, NULL);
586f2fa5a99SThomas Zimmermann }
587f2fa5a99SThomas Zimmermann 
588f2fa5a99SThomas Zimmermann /*
589a6ff807bSThomas Zimmermann  * Primary plane
590a6ff807bSThomas Zimmermann  */
591a6ff807bSThomas Zimmermann 
592a6ff807bSThomas Zimmermann static const uint32_t ast_primary_plane_formats[] = {
593a6ff807bSThomas Zimmermann 	DRM_FORMAT_XRGB8888,
594a6ff807bSThomas Zimmermann 	DRM_FORMAT_RGB565,
595a6ff807bSThomas Zimmermann 	DRM_FORMAT_C8,
596a6ff807bSThomas Zimmermann };
597a6ff807bSThomas Zimmermann 
ast_primary_plane_helper_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)598ae46a57dSThomas Zimmermann static int ast_primary_plane_helper_atomic_check(struct drm_plane *plane,
5997c11b99aSMaxime Ripard 						 struct drm_atomic_state *state)
600a6ff807bSThomas Zimmermann {
6010432a504SThomas Zimmermann 	struct drm_device *dev = plane->dev;
602d95dcfc4SThomas Zimmermann 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
603d95dcfc4SThomas Zimmermann 	struct drm_crtc_state *new_crtc_state = NULL;
604d95dcfc4SThomas Zimmermann 	struct ast_crtc_state *new_ast_crtc_state;
605ae46a57dSThomas Zimmermann 	int ret;
606ae46a57dSThomas Zimmermann 
6070432a504SThomas Zimmermann 	if (new_plane_state->crtc)
608d95dcfc4SThomas Zimmermann 		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
609ae46a57dSThomas Zimmermann 
610d95dcfc4SThomas Zimmermann 	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
611cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
612cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
613ae46a57dSThomas Zimmermann 						  false, true);
6140432a504SThomas Zimmermann 	if (ret) {
615ae46a57dSThomas Zimmermann 		return ret;
6160432a504SThomas Zimmermann 	} else if (!new_plane_state->visible) {
6170432a504SThomas Zimmermann 		if (drm_WARN_ON(dev, new_plane_state->crtc)) /* cannot legally happen */
6180432a504SThomas Zimmermann 			return -EINVAL;
6190432a504SThomas Zimmermann 		else
6203339fdf5SThomas Zimmermann 			return 0;
6210432a504SThomas Zimmermann 	}
6223339fdf5SThomas Zimmermann 
623d95dcfc4SThomas Zimmermann 	new_ast_crtc_state = to_ast_crtc_state(new_crtc_state);
6243339fdf5SThomas Zimmermann 
625d95dcfc4SThomas Zimmermann 	new_ast_crtc_state->format = new_plane_state->fb->format;
6263339fdf5SThomas Zimmermann 
627a6ff807bSThomas Zimmermann 	return 0;
628a6ff807bSThomas Zimmermann }
629a6ff807bSThomas Zimmermann 
ast_handle_damage(struct ast_plane * ast_plane,struct iosys_map * src,struct drm_framebuffer * fb,const struct drm_rect * clip)630f2fa5a99SThomas Zimmermann static void ast_handle_damage(struct ast_plane *ast_plane, struct iosys_map *src,
631f2fa5a99SThomas Zimmermann 			      struct drm_framebuffer *fb,
632f2fa5a99SThomas Zimmermann 			      const struct drm_rect *clip)
633f2fa5a99SThomas Zimmermann {
634b1def7faSThomas Zimmermann 	struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(ast_plane->vaddr);
635f2fa5a99SThomas Zimmermann 
636f2fa5a99SThomas Zimmermann 	iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, clip));
637f2fa5a99SThomas Zimmermann 	drm_fb_memcpy(&dst, fb->pitches, src, fb, clip);
638f2fa5a99SThomas Zimmermann }
639f2fa5a99SThomas Zimmermann 
ast_primary_plane_helper_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)640d95dcfc4SThomas Zimmermann static void ast_primary_plane_helper_atomic_update(struct drm_plane *plane,
641977697e2SMaxime Ripard 						   struct drm_atomic_state *state)
642a6ff807bSThomas Zimmermann {
6431a19b4cbSThomas Zimmermann 	struct drm_device *dev = plane->dev;
6445abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(dev);
645d95dcfc4SThomas Zimmermann 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
646f2fa5a99SThomas Zimmermann 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
647d95dcfc4SThomas Zimmermann 	struct drm_framebuffer *fb = plane_state->fb;
648d95dcfc4SThomas Zimmermann 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
649d95dcfc4SThomas Zimmermann 	struct drm_framebuffer *old_fb = old_plane_state->fb;
650f2fa5a99SThomas Zimmermann 	struct ast_plane *ast_plane = to_ast_plane(plane);
651f2fa5a99SThomas Zimmermann 	struct drm_rect damage;
652f2fa5a99SThomas Zimmermann 	struct drm_atomic_helper_damage_iter iter;
653a6ff807bSThomas Zimmermann 
6545638c82cSThomas Zimmermann 	if (!old_fb || (fb->format != old_fb->format)) {
655d95dcfc4SThomas Zimmermann 		struct drm_crtc *crtc = plane_state->crtc;
656d95dcfc4SThomas Zimmermann 		struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
6575638c82cSThomas Zimmermann 		struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
6585638c82cSThomas Zimmermann 		struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
6595638c82cSThomas Zimmermann 
6605638c82cSThomas Zimmermann 		ast_set_color_reg(ast, fb->format);
6615638c82cSThomas Zimmermann 		ast_set_vbios_color_reg(ast, fb->format, vbios_mode_info);
6625638c82cSThomas Zimmermann 	}
6635638c82cSThomas Zimmermann 
664f2fa5a99SThomas Zimmermann 	drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
665f2fa5a99SThomas Zimmermann 	drm_atomic_for_each_plane_damage(&iter, &damage) {
666f2fa5a99SThomas Zimmermann 		ast_handle_damage(ast_plane, shadow_plane_state->data, fb, &damage);
667f2fa5a99SThomas Zimmermann 	}
668a6ff807bSThomas Zimmermann 
669202fb33bSThomas Zimmermann 	/*
670202fb33bSThomas Zimmermann 	 * Some BMCs stop scanning out the video signal after the driver
6718f85e4a1SThomas Zimmermann 	 * reprogrammed the offset. This stalls display output for several
6728f85e4a1SThomas Zimmermann 	 * seconds and makes the display unusable. Therefore only update
6738f85e4a1SThomas Zimmermann 	 * the offset if it changes.
674202fb33bSThomas Zimmermann 	 */
675202fb33bSThomas Zimmermann 	if (!old_fb || old_fb->pitches[0] != fb->pitches[0])
6765638c82cSThomas Zimmermann 		ast_set_offset_reg(ast, fb);
6778f85e4a1SThomas Zimmermann }
6788f85e4a1SThomas Zimmermann 
ast_primary_plane_helper_atomic_enable(struct drm_plane * plane,struct drm_atomic_state * state)6798f85e4a1SThomas Zimmermann static void ast_primary_plane_helper_atomic_enable(struct drm_plane *plane,
6808f85e4a1SThomas Zimmermann 						   struct drm_atomic_state *state)
6818f85e4a1SThomas Zimmermann {
6825abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(plane->dev);
6838f85e4a1SThomas Zimmermann 	struct ast_plane *ast_plane = to_ast_plane(plane);
6848f85e4a1SThomas Zimmermann 
6858f85e4a1SThomas Zimmermann 	/*
6868f85e4a1SThomas Zimmermann 	 * Some BMCs stop scanning out the video signal after the driver
6878f85e4a1SThomas Zimmermann 	 * reprogrammed the scanout address. This stalls display
6888f85e4a1SThomas Zimmermann 	 * output for several seconds and makes the display unusable.
6898f85e4a1SThomas Zimmermann 	 * Therefore only reprogram the address after enabling the plane.
6908f85e4a1SThomas Zimmermann 	 */
691f2fa5a99SThomas Zimmermann 	ast_set_start_address_crt1(ast, (u32)ast_plane->offset);
6922fbeec03SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x00);
6932fbeec03SThomas Zimmermann }
6942fbeec03SThomas Zimmermann 
ast_primary_plane_helper_atomic_disable(struct drm_plane * plane,struct drm_atomic_state * state)695d95dcfc4SThomas Zimmermann static void ast_primary_plane_helper_atomic_disable(struct drm_plane *plane,
696977697e2SMaxime Ripard 						    struct drm_atomic_state *state)
6972fbeec03SThomas Zimmermann {
6985abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(plane->dev);
6992fbeec03SThomas Zimmermann 
7002fbeec03SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x20);
701a6ff807bSThomas Zimmermann }
702a6ff807bSThomas Zimmermann 
703a6ff807bSThomas Zimmermann static const struct drm_plane_helper_funcs ast_primary_plane_helper_funcs = {
704f2fa5a99SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
705a6ff807bSThomas Zimmermann 	.atomic_check = ast_primary_plane_helper_atomic_check,
706a6ff807bSThomas Zimmermann 	.atomic_update = ast_primary_plane_helper_atomic_update,
7078f85e4a1SThomas Zimmermann 	.atomic_enable = ast_primary_plane_helper_atomic_enable,
7082fbeec03SThomas Zimmermann 	.atomic_disable = ast_primary_plane_helper_atomic_disable,
709a6ff807bSThomas Zimmermann };
710a6ff807bSThomas Zimmermann 
711a6ff807bSThomas Zimmermann static const struct drm_plane_funcs ast_primary_plane_funcs = {
712a6ff807bSThomas Zimmermann 	.update_plane = drm_atomic_helper_update_plane,
713a6ff807bSThomas Zimmermann 	.disable_plane = drm_atomic_helper_disable_plane,
714a6ff807bSThomas Zimmermann 	.destroy = drm_plane_cleanup,
715f2fa5a99SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_FUNCS,
716a6ff807bSThomas Zimmermann };
717a6ff807bSThomas Zimmermann 
ast_primary_plane_init(struct ast_device * ast)71837b42cf9SThomas Zimmermann static int ast_primary_plane_init(struct ast_device *ast)
719616048afSThomas Zimmermann {
720616048afSThomas Zimmermann 	struct drm_device *dev = &ast->base;
721f2fa5a99SThomas Zimmermann 	struct ast_plane *ast_primary_plane = &ast->primary_plane;
722f2fa5a99SThomas Zimmermann 	struct drm_plane *primary_plane = &ast_primary_plane->base;
723f2fa5a99SThomas Zimmermann 	void __iomem *vaddr = ast->vram;
7247484a5bcSJocelyn Falempe 	u64 offset = 0; /* with shmem, the primary plane is always at offset 0 */
725f2fa5a99SThomas Zimmermann 	unsigned long cursor_size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
726f2fa5a99SThomas Zimmermann 	unsigned long size = ast->vram_fb_available - cursor_size;
727616048afSThomas Zimmermann 	int ret;
728616048afSThomas Zimmermann 
729f2fa5a99SThomas Zimmermann 	ret = ast_plane_init(dev, ast_primary_plane, vaddr, offset, size,
730f2fa5a99SThomas Zimmermann 			     0x01, &ast_primary_plane_funcs,
731f2fa5a99SThomas Zimmermann 			     ast_primary_plane_formats, ARRAY_SIZE(ast_primary_plane_formats),
732f2fa5a99SThomas Zimmermann 			     NULL, DRM_PLANE_TYPE_PRIMARY);
733616048afSThomas Zimmermann 	if (ret) {
734f2fa5a99SThomas Zimmermann 		drm_err(dev, "ast_plane_init() failed: %d\n", ret);
735616048afSThomas Zimmermann 		return ret;
736616048afSThomas Zimmermann 	}
737616048afSThomas Zimmermann 	drm_plane_helper_add(primary_plane, &ast_primary_plane_helper_funcs);
738f2fa5a99SThomas Zimmermann 	drm_plane_enable_fb_damage_clips(primary_plane);
739616048afSThomas Zimmermann 
740616048afSThomas Zimmermann 	return 0;
741616048afSThomas Zimmermann }
742616048afSThomas Zimmermann 
743a6ff807bSThomas Zimmermann /*
74402f3bb75SThomas Zimmermann  * Cursor plane
74502f3bb75SThomas Zimmermann  */
74602f3bb75SThomas Zimmermann 
ast_update_cursor_image(u8 __iomem * dst,const u8 * src,int width,int height)747718c2286SThomas Zimmermann static void ast_update_cursor_image(u8 __iomem *dst, const u8 *src, int width, int height)
748718c2286SThomas Zimmermann {
749718c2286SThomas Zimmermann 	union {
750718c2286SThomas Zimmermann 		u32 ul;
751718c2286SThomas Zimmermann 		u8 b[4];
752718c2286SThomas Zimmermann 	} srcdata32[2], data32;
753718c2286SThomas Zimmermann 	union {
754718c2286SThomas Zimmermann 		u16 us;
755718c2286SThomas Zimmermann 		u8 b[2];
756718c2286SThomas Zimmermann 	} data16;
757718c2286SThomas Zimmermann 	u32 csum = 0;
758718c2286SThomas Zimmermann 	s32 alpha_dst_delta, last_alpha_dst_delta;
759718c2286SThomas Zimmermann 	u8 __iomem *dstxor;
760718c2286SThomas Zimmermann 	const u8 *srcxor;
761718c2286SThomas Zimmermann 	int i, j;
762718c2286SThomas Zimmermann 	u32 per_pixel_copy, two_pixel_copy;
763718c2286SThomas Zimmermann 
764718c2286SThomas Zimmermann 	alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
765718c2286SThomas Zimmermann 	last_alpha_dst_delta = alpha_dst_delta - (width << 1);
766718c2286SThomas Zimmermann 
767718c2286SThomas Zimmermann 	srcxor = src;
768718c2286SThomas Zimmermann 	dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
769718c2286SThomas Zimmermann 	per_pixel_copy = width & 1;
770718c2286SThomas Zimmermann 	two_pixel_copy = width >> 1;
771718c2286SThomas Zimmermann 
772718c2286SThomas Zimmermann 	for (j = 0; j < height; j++) {
773718c2286SThomas Zimmermann 		for (i = 0; i < two_pixel_copy; i++) {
774718c2286SThomas Zimmermann 			srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
775718c2286SThomas Zimmermann 			srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
776718c2286SThomas Zimmermann 			data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
777718c2286SThomas Zimmermann 			data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
778718c2286SThomas Zimmermann 			data32.b[2] = srcdata32[1].b[1] | (srcdata32[1].b[0] >> 4);
779718c2286SThomas Zimmermann 			data32.b[3] = srcdata32[1].b[3] | (srcdata32[1].b[2] >> 4);
780718c2286SThomas Zimmermann 
781718c2286SThomas Zimmermann 			writel(data32.ul, dstxor);
782718c2286SThomas Zimmermann 			csum += data32.ul;
783718c2286SThomas Zimmermann 
784718c2286SThomas Zimmermann 			dstxor += 4;
785718c2286SThomas Zimmermann 			srcxor += 8;
786718c2286SThomas Zimmermann 
787718c2286SThomas Zimmermann 		}
788718c2286SThomas Zimmermann 
789718c2286SThomas Zimmermann 		for (i = 0; i < per_pixel_copy; i++) {
790718c2286SThomas Zimmermann 			srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
791718c2286SThomas Zimmermann 			data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
792718c2286SThomas Zimmermann 			data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
793718c2286SThomas Zimmermann 			writew(data16.us, dstxor);
794718c2286SThomas Zimmermann 			csum += (u32)data16.us;
795718c2286SThomas Zimmermann 
796718c2286SThomas Zimmermann 			dstxor += 2;
797718c2286SThomas Zimmermann 			srcxor += 4;
798718c2286SThomas Zimmermann 		}
799718c2286SThomas Zimmermann 		dstxor += last_alpha_dst_delta;
800718c2286SThomas Zimmermann 	}
801718c2286SThomas Zimmermann 
802718c2286SThomas Zimmermann 	/* write checksum + signature */
803718c2286SThomas Zimmermann 	dst += AST_HWC_SIZE;
804718c2286SThomas Zimmermann 	writel(csum, dst);
805718c2286SThomas Zimmermann 	writel(width, dst + AST_HWC_SIGNATURE_SizeX);
806718c2286SThomas Zimmermann 	writel(height, dst + AST_HWC_SIGNATURE_SizeY);
807718c2286SThomas Zimmermann 	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
808718c2286SThomas Zimmermann 	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
809718c2286SThomas Zimmermann }
810718c2286SThomas Zimmermann 
ast_set_cursor_base(struct ast_device * ast,u64 address)81137b42cf9SThomas Zimmermann static void ast_set_cursor_base(struct ast_device *ast, u64 address)
812718c2286SThomas Zimmermann {
813718c2286SThomas Zimmermann 	u8 addr0 = (address >> 3) & 0xff;
814718c2286SThomas Zimmermann 	u8 addr1 = (address >> 11) & 0xff;
815718c2286SThomas Zimmermann 	u8 addr2 = (address >> 19) & 0xff;
816718c2286SThomas Zimmermann 
817718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc8, addr0);
818718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc9, addr1);
819718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xca, addr2);
820718c2286SThomas Zimmermann }
821718c2286SThomas Zimmermann 
ast_set_cursor_location(struct ast_device * ast,u16 x,u16 y,u8 x_offset,u8 y_offset)82237b42cf9SThomas Zimmermann static void ast_set_cursor_location(struct ast_device *ast, u16 x, u16 y,
823718c2286SThomas Zimmermann 				    u8 x_offset, u8 y_offset)
824718c2286SThomas Zimmermann {
825718c2286SThomas Zimmermann 	u8 x0 = (x & 0x00ff);
826718c2286SThomas Zimmermann 	u8 x1 = (x & 0x0f00) >> 8;
827718c2286SThomas Zimmermann 	u8 y0 = (y & 0x00ff);
828718c2286SThomas Zimmermann 	u8 y1 = (y & 0x0700) >> 8;
829718c2286SThomas Zimmermann 
830718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc2, x_offset);
831718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc3, y_offset);
832718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc4, x0);
833718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc5, x1);
834718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc6, y0);
835718c2286SThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc7, y1);
836718c2286SThomas Zimmermann }
837718c2286SThomas Zimmermann 
ast_set_cursor_enabled(struct ast_device * ast,bool enabled)83837b42cf9SThomas Zimmermann static void ast_set_cursor_enabled(struct ast_device *ast, bool enabled)
839718c2286SThomas Zimmermann {
840718c2286SThomas Zimmermann 	static const u8 mask = (u8)~(AST_IO_VGACRCB_HWC_16BPP |
841718c2286SThomas Zimmermann 				     AST_IO_VGACRCB_HWC_ENABLED);
842718c2286SThomas Zimmermann 
843718c2286SThomas Zimmermann 	u8 vgacrcb = AST_IO_VGACRCB_HWC_16BPP;
844718c2286SThomas Zimmermann 
845718c2286SThomas Zimmermann 	if (enabled)
846718c2286SThomas Zimmermann 		vgacrcb |= AST_IO_VGACRCB_HWC_ENABLED;
847718c2286SThomas Zimmermann 
848718c2286SThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, mask, vgacrcb);
849718c2286SThomas Zimmermann }
850718c2286SThomas Zimmermann 
85102f3bb75SThomas Zimmermann static const uint32_t ast_cursor_plane_formats[] = {
85202f3bb75SThomas Zimmermann 	DRM_FORMAT_ARGB8888,
85302f3bb75SThomas Zimmermann };
85402f3bb75SThomas Zimmermann 
ast_cursor_plane_helper_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)85502f3bb75SThomas Zimmermann static int ast_cursor_plane_helper_atomic_check(struct drm_plane *plane,
8567c11b99aSMaxime Ripard 						struct drm_atomic_state *state)
85702f3bb75SThomas Zimmermann {
858d95dcfc4SThomas Zimmermann 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
859d95dcfc4SThomas Zimmermann 	struct drm_framebuffer *new_fb = new_plane_state->fb;
860d95dcfc4SThomas Zimmermann 	struct drm_crtc_state *new_crtc_state = NULL;
861ae46a57dSThomas Zimmermann 	int ret;
862ae46a57dSThomas Zimmermann 
8630432a504SThomas Zimmermann 	if (new_plane_state->crtc)
864d95dcfc4SThomas Zimmermann 		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
865ae46a57dSThomas Zimmermann 
866d95dcfc4SThomas Zimmermann 	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
867cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
868cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
869ae46a57dSThomas Zimmermann 						  true, true);
8700432a504SThomas Zimmermann 	if (ret || !new_plane_state->visible)
871ae46a57dSThomas Zimmermann 		return ret;
872ae46a57dSThomas Zimmermann 
873d95dcfc4SThomas Zimmermann 	if (new_fb->width > AST_MAX_HWC_WIDTH || new_fb->height > AST_MAX_HWC_HEIGHT)
874ae46a57dSThomas Zimmermann 		return -EINVAL;
875ae46a57dSThomas Zimmermann 
87602f3bb75SThomas Zimmermann 	return 0;
87702f3bb75SThomas Zimmermann }
87802f3bb75SThomas Zimmermann 
ast_cursor_plane_helper_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)879d95dcfc4SThomas Zimmermann static void ast_cursor_plane_helper_atomic_update(struct drm_plane *plane,
880977697e2SMaxime Ripard 						  struct drm_atomic_state *state)
88102f3bb75SThomas Zimmermann {
882537a1db9SThomas Zimmermann 	struct ast_plane *ast_plane = to_ast_plane(plane);
883d95dcfc4SThomas Zimmermann 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
884d95dcfc4SThomas Zimmermann 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
885d95dcfc4SThomas Zimmermann 	struct drm_framebuffer *fb = plane_state->fb;
886d95dcfc4SThomas Zimmermann 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
8875abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(plane->dev);
8887938f421SLucas De Marchi 	struct iosys_map src_map = shadow_plane_state->data[0];
889f2fa5a99SThomas Zimmermann 	struct drm_rect damage;
890f2fa5a99SThomas Zimmermann 	const u8 *src = src_map.vaddr; /* TODO: Use mapping abstraction properly */
891f2fa5a99SThomas Zimmermann 	u64 dst_off = ast_plane->offset;
892f2fa5a99SThomas Zimmermann 	u8 __iomem *dst = ast_plane->vaddr; /* TODO: Use mapping abstraction properly */
893f2fa5a99SThomas Zimmermann 	u8 __iomem *sig = dst + AST_HWC_SIZE; /* TODO: Use mapping abstraction properly */
89481039adcSThomas Zimmermann 	unsigned int offset_x, offset_y;
895718c2286SThomas Zimmermann 	u16 x, y;
896718c2286SThomas Zimmermann 	u8 x_offset, y_offset;
8974d36cf07SThomas Zimmermann 
8984d36cf07SThomas Zimmermann 	/*
899f2fa5a99SThomas Zimmermann 	 * Do data transfer to hardware buffer and point the scanout
900f2fa5a99SThomas Zimmermann 	 * engine to the offset.
9014d36cf07SThomas Zimmermann 	 */
9024d36cf07SThomas Zimmermann 
903f2fa5a99SThomas Zimmermann 	if (drm_atomic_helper_damage_merged(old_plane_state, plane_state, &damage)) {
9044d36cf07SThomas Zimmermann 		ast_update_cursor_image(dst, src, fb->width, fb->height);
905385131f3SThomas Zimmermann 		ast_set_cursor_base(ast, dst_off);
906f2fa5a99SThomas Zimmermann 	}
907718c2286SThomas Zimmermann 
9084d36cf07SThomas Zimmermann 	/*
9094d36cf07SThomas Zimmermann 	 * Update location in HWC signature and registers.
9104d36cf07SThomas Zimmermann 	 */
911718c2286SThomas Zimmermann 
912d95dcfc4SThomas Zimmermann 	writel(plane_state->crtc_x, sig + AST_HWC_SIGNATURE_X);
913d95dcfc4SThomas Zimmermann 	writel(plane_state->crtc_y, sig + AST_HWC_SIGNATURE_Y);
914718c2286SThomas Zimmermann 
91581039adcSThomas Zimmermann 	offset_x = AST_MAX_HWC_WIDTH - fb->width;
916ee4a92d6SThomas Zimmermann 	offset_y = AST_MAX_HWC_HEIGHT - fb->height;
91702f3bb75SThomas Zimmermann 
918d95dcfc4SThomas Zimmermann 	if (plane_state->crtc_x < 0) {
919d95dcfc4SThomas Zimmermann 		x_offset = (-plane_state->crtc_x) + offset_x;
920718c2286SThomas Zimmermann 		x = 0;
921718c2286SThomas Zimmermann 	} else {
922718c2286SThomas Zimmermann 		x_offset = offset_x;
923d95dcfc4SThomas Zimmermann 		x = plane_state->crtc_x;
924718c2286SThomas Zimmermann 	}
925d95dcfc4SThomas Zimmermann 	if (plane_state->crtc_y < 0) {
926d95dcfc4SThomas Zimmermann 		y_offset = (-plane_state->crtc_y) + offset_y;
927718c2286SThomas Zimmermann 		y = 0;
928718c2286SThomas Zimmermann 	} else {
929718c2286SThomas Zimmermann 		y_offset = offset_y;
930d95dcfc4SThomas Zimmermann 		y = plane_state->crtc_y;
93102f3bb75SThomas Zimmermann 	}
93202f3bb75SThomas Zimmermann 
933718c2286SThomas Zimmermann 	ast_set_cursor_location(ast, x, y, x_offset, y_offset);
934718c2286SThomas Zimmermann 
9354d36cf07SThomas Zimmermann 	/* Dummy write to enable HWC and make the HW pick-up the changes. */
936718c2286SThomas Zimmermann 	ast_set_cursor_enabled(ast, true);
93702f3bb75SThomas Zimmermann }
93802f3bb75SThomas Zimmermann 
ast_cursor_plane_helper_atomic_disable(struct drm_plane * plane,struct drm_atomic_state * state)939d95dcfc4SThomas Zimmermann static void ast_cursor_plane_helper_atomic_disable(struct drm_plane *plane,
940977697e2SMaxime Ripard 						   struct drm_atomic_state *state)
94102f3bb75SThomas Zimmermann {
9425abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(plane->dev);
94302f3bb75SThomas Zimmermann 
944718c2286SThomas Zimmermann 	ast_set_cursor_enabled(ast, false);
94502f3bb75SThomas Zimmermann }
94602f3bb75SThomas Zimmermann 
94702f3bb75SThomas Zimmermann static const struct drm_plane_helper_funcs ast_cursor_plane_helper_funcs = {
9484d36cf07SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
94902f3bb75SThomas Zimmermann 	.atomic_check = ast_cursor_plane_helper_atomic_check,
95002f3bb75SThomas Zimmermann 	.atomic_update = ast_cursor_plane_helper_atomic_update,
95102f3bb75SThomas Zimmermann 	.atomic_disable = ast_cursor_plane_helper_atomic_disable,
95202f3bb75SThomas Zimmermann };
95302f3bb75SThomas Zimmermann 
95402f3bb75SThomas Zimmermann static const struct drm_plane_funcs ast_cursor_plane_funcs = {
95502f3bb75SThomas Zimmermann 	.update_plane = drm_atomic_helper_update_plane,
95602f3bb75SThomas Zimmermann 	.disable_plane = drm_atomic_helper_disable_plane,
957f2fa5a99SThomas Zimmermann 	.destroy = drm_plane_cleanup,
9584d36cf07SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_FUNCS,
95902f3bb75SThomas Zimmermann };
96002f3bb75SThomas Zimmermann 
ast_cursor_plane_init(struct ast_device * ast)96137b42cf9SThomas Zimmermann static int ast_cursor_plane_init(struct ast_device *ast)
962616048afSThomas Zimmermann {
963616048afSThomas Zimmermann 	struct drm_device *dev = &ast->base;
964f2fa5a99SThomas Zimmermann 	struct ast_plane *ast_cursor_plane = &ast->cursor_plane;
965f2fa5a99SThomas Zimmermann 	struct drm_plane *cursor_plane = &ast_cursor_plane->base;
966aa7c8865SThomas Zimmermann 	size_t size;
967f2fa5a99SThomas Zimmermann 	void __iomem *vaddr;
968f2fa5a99SThomas Zimmermann 	u64 offset;
969616048afSThomas Zimmermann 	int ret;
970616048afSThomas Zimmermann 
97122b6591fSThomas Zimmermann 	/*
97222b6591fSThomas Zimmermann 	 * Allocate backing storage for cursors. The BOs are permanently
97322b6591fSThomas Zimmermann 	 * pinned to the top end of the VRAM.
97422b6591fSThomas Zimmermann 	 */
97522b6591fSThomas Zimmermann 
97622b6591fSThomas Zimmermann 	size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
97722b6591fSThomas Zimmermann 
978f2fa5a99SThomas Zimmermann 	if (ast->vram_fb_available < size)
979f2fa5a99SThomas Zimmermann 		return -ENOMEM;
980aa7c8865SThomas Zimmermann 
981f2fa5a99SThomas Zimmermann 	vaddr = ast->vram + ast->vram_fb_available - size;
9827484a5bcSJocelyn Falempe 	offset = ast->vram_fb_available - size;
983aa7c8865SThomas Zimmermann 
984f2fa5a99SThomas Zimmermann 	ret = ast_plane_init(dev, ast_cursor_plane, vaddr, offset, size,
985f2fa5a99SThomas Zimmermann 			     0x01, &ast_cursor_plane_funcs,
986f2fa5a99SThomas Zimmermann 			     ast_cursor_plane_formats, ARRAY_SIZE(ast_cursor_plane_formats),
987f2fa5a99SThomas Zimmermann 			     NULL, DRM_PLANE_TYPE_CURSOR);
988616048afSThomas Zimmermann 	if (ret) {
989f2fa5a99SThomas Zimmermann 		drm_err(dev, "ast_plane_init() failed: %d\n", ret);
990f2fa5a99SThomas Zimmermann 		return ret;
991616048afSThomas Zimmermann 	}
992616048afSThomas Zimmermann 	drm_plane_helper_add(cursor_plane, &ast_cursor_plane_helper_funcs);
993f2fa5a99SThomas Zimmermann 	drm_plane_enable_fb_damage_clips(cursor_plane);
994f2fa5a99SThomas Zimmermann 
995f2fa5a99SThomas Zimmermann 	ast->vram_fb_available -= size;
996616048afSThomas Zimmermann 
997616048afSThomas Zimmermann 	return 0;
998616048afSThomas Zimmermann }
999616048afSThomas Zimmermann 
100002f3bb75SThomas Zimmermann /*
1001a6ff807bSThomas Zimmermann  * CRTC
1002a6ff807bSThomas Zimmermann  */
1003a6ff807bSThomas Zimmermann 
ast_crtc_dpms(struct drm_crtc * crtc,int mode)1004312fec14SDave Airlie static void ast_crtc_dpms(struct drm_crtc *crtc, int mode)
1005312fec14SDave Airlie {
10065abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(crtc->dev);
1007594e9c04SKuoHsiang Chou 	u8 ch = AST_DPMS_VSYNC_OFF | AST_DPMS_HSYNC_OFF;
10085171660cSJocelyn Falempe 	struct ast_crtc_state *ast_state;
10095171660cSJocelyn Falempe 	const struct drm_format_info *format;
10105171660cSJocelyn Falempe 	struct ast_vbios_mode_info *vbios_mode_info;
1011312fec14SDave Airlie 
10122fbeec03SThomas Zimmermann 	/* TODO: Maybe control display signal generation with
10132fbeec03SThomas Zimmermann 	 *       Sync Enable (bit CR17.7).
10142fbeec03SThomas Zimmermann 	 */
1015312fec14SDave Airlie 	switch (mode) {
1016312fec14SDave Airlie 	case DRM_MODE_DPMS_ON:
1017594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT,  0x01, 0xdf, 0);
1018594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0xfc, 0);
10197f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_DP501_BIT)
102083c6620bSDave Airlie 			ast_set_dp501_video_output(crtc->dev, 1);
1021594e9c04SKuoHsiang Chou 
10227f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
1023594e9c04SKuoHsiang Chou 			ast_dp_power_on_off(crtc->dev, AST_DP_POWER_ON);
1024594e9c04SKuoHsiang Chou 			ast_wait_for_vretrace(ast);
1025594e9c04SKuoHsiang Chou 			ast_dp_set_on_off(crtc->dev, 1);
1026594e9c04SKuoHsiang Chou 		}
1027594e9c04SKuoHsiang Chou 
10285171660cSJocelyn Falempe 		ast_state = to_ast_crtc_state(crtc->state);
10295171660cSJocelyn Falempe 		format = ast_state->format;
10305171660cSJocelyn Falempe 
10315171660cSJocelyn Falempe 		if (format) {
10325171660cSJocelyn Falempe 			vbios_mode_info = &ast_state->vbios_mode_info;
10335171660cSJocelyn Falempe 
10345171660cSJocelyn Falempe 			ast_set_color_reg(ast, format);
10355171660cSJocelyn Falempe 			ast_set_vbios_color_reg(ast, format, vbios_mode_info);
1036ce7fcf70SJocelyn Falempe 			if (crtc->state->gamma_lut)
1037ce7fcf70SJocelyn Falempe 				ast_crtc_set_gamma(ast, format, crtc->state->gamma_lut->data);
1038ce7fcf70SJocelyn Falempe 			else
1039ce7fcf70SJocelyn Falempe 				ast_crtc_set_gamma_linear(ast, format);
10405171660cSJocelyn Falempe 		}
1041312fec14SDave Airlie 		break;
1042594e9c04SKuoHsiang Chou 	case DRM_MODE_DPMS_STANDBY:
1043594e9c04SKuoHsiang Chou 	case DRM_MODE_DPMS_SUSPEND:
1044312fec14SDave Airlie 	case DRM_MODE_DPMS_OFF:
1045594e9c04SKuoHsiang Chou 		ch = mode;
10467f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_DP501_BIT)
104783c6620bSDave Airlie 			ast_set_dp501_video_output(crtc->dev, 0);
1048594e9c04SKuoHsiang Chou 
10497f35680aSThomas Zimmermann 		if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
1050594e9c04SKuoHsiang Chou 			ast_dp_set_on_off(crtc->dev, 0);
1051594e9c04SKuoHsiang Chou 			ast_dp_power_on_off(crtc->dev, AST_DP_POWER_OFF);
1052594e9c04SKuoHsiang Chou 		}
1053594e9c04SKuoHsiang Chou 
1054594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT,  0x01, 0xdf, 0x20);
1055594e9c04SKuoHsiang Chou 		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0xfc, ch);
10567f35680aSThomas Zimmermann 		break;
1057312fec14SDave Airlie 	}
1058312fec14SDave Airlie }
1059312fec14SDave Airlie 
10606abbad2cSThomas Zimmermann static enum drm_mode_status
ast_crtc_helper_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)10616abbad2cSThomas Zimmermann ast_crtc_helper_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode)
10626abbad2cSThomas Zimmermann {
10635abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(crtc->dev);
10646abbad2cSThomas Zimmermann 	enum drm_mode_status status;
10656abbad2cSThomas Zimmermann 	uint32_t jtemp;
10666abbad2cSThomas Zimmermann 
10676abbad2cSThomas Zimmermann 	if (ast->support_wide_screen) {
10686abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
10696abbad2cSThomas Zimmermann 			return MODE_OK;
10706abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
10716abbad2cSThomas Zimmermann 			return MODE_OK;
10726abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
10736abbad2cSThomas Zimmermann 			return MODE_OK;
10746abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
10756abbad2cSThomas Zimmermann 			return MODE_OK;
10766abbad2cSThomas Zimmermann 		if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
10776abbad2cSThomas Zimmermann 			return MODE_OK;
107871dee036SJammy Huang 		if ((mode->hdisplay == 1152) && (mode->vdisplay == 864))
107971dee036SJammy Huang 			return MODE_OK;
10806abbad2cSThomas Zimmermann 
1081ecf64579SThomas Zimmermann 		if ((ast->chip == AST2100) || // GEN2, but not AST1100 (?)
1082ecf64579SThomas Zimmermann 		    (ast->chip == AST2200) || // GEN3, but not AST2150 (?)
1083ecf64579SThomas Zimmermann 		    IS_AST_GEN4(ast) || IS_AST_GEN5(ast) ||
1084ecf64579SThomas Zimmermann 		    IS_AST_GEN6(ast) || IS_AST_GEN7(ast)) {
10856abbad2cSThomas Zimmermann 			if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
10866abbad2cSThomas Zimmermann 				return MODE_OK;
10876abbad2cSThomas Zimmermann 
10886abbad2cSThomas Zimmermann 			if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
10896abbad2cSThomas Zimmermann 				jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
10906abbad2cSThomas Zimmermann 				if (jtemp & 0x01)
10916abbad2cSThomas Zimmermann 					return MODE_NOMODE;
10926abbad2cSThomas Zimmermann 				else
10936abbad2cSThomas Zimmermann 					return MODE_OK;
10946abbad2cSThomas Zimmermann 			}
10956abbad2cSThomas Zimmermann 		}
10966abbad2cSThomas Zimmermann 	}
10976abbad2cSThomas Zimmermann 
10986abbad2cSThomas Zimmermann 	status = MODE_NOMODE;
10996abbad2cSThomas Zimmermann 
11006abbad2cSThomas Zimmermann 	switch (mode->hdisplay) {
11016abbad2cSThomas Zimmermann 	case 640:
11026abbad2cSThomas Zimmermann 		if (mode->vdisplay == 480)
11036abbad2cSThomas Zimmermann 			status = MODE_OK;
11046abbad2cSThomas Zimmermann 		break;
11056abbad2cSThomas Zimmermann 	case 800:
11066abbad2cSThomas Zimmermann 		if (mode->vdisplay == 600)
11076abbad2cSThomas Zimmermann 			status = MODE_OK;
11086abbad2cSThomas Zimmermann 		break;
11096abbad2cSThomas Zimmermann 	case 1024:
11106abbad2cSThomas Zimmermann 		if (mode->vdisplay == 768)
11116abbad2cSThomas Zimmermann 			status = MODE_OK;
11126abbad2cSThomas Zimmermann 		break;
111371dee036SJammy Huang 	case 1152:
111471dee036SJammy Huang 		if (mode->vdisplay == 864)
111571dee036SJammy Huang 			status = MODE_OK;
111671dee036SJammy Huang 		break;
11176abbad2cSThomas Zimmermann 	case 1280:
11186abbad2cSThomas Zimmermann 		if (mode->vdisplay == 1024)
11196abbad2cSThomas Zimmermann 			status = MODE_OK;
11206abbad2cSThomas Zimmermann 		break;
11216abbad2cSThomas Zimmermann 	case 1600:
11226abbad2cSThomas Zimmermann 		if (mode->vdisplay == 1200)
11236abbad2cSThomas Zimmermann 			status = MODE_OK;
11246abbad2cSThomas Zimmermann 		break;
11256abbad2cSThomas Zimmermann 	default:
11266abbad2cSThomas Zimmermann 		break;
11276abbad2cSThomas Zimmermann 	}
11286abbad2cSThomas Zimmermann 
11296abbad2cSThomas Zimmermann 	return status;
11306abbad2cSThomas Zimmermann }
11316abbad2cSThomas Zimmermann 
ast_crtc_helper_atomic_check(struct drm_crtc * crtc,struct drm_atomic_state * state)1132b48e1b6fSThomas Zimmermann static int ast_crtc_helper_atomic_check(struct drm_crtc *crtc,
113329b77ad7SMaxime Ripard 					struct drm_atomic_state *state)
1134b48e1b6fSThomas Zimmermann {
1135016a14beSThomas Zimmermann 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1136ce7fcf70SJocelyn Falempe 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
1137ce7fcf70SJocelyn Falempe 	struct ast_crtc_state *old_ast_crtc_state = to_ast_crtc_state(old_crtc_state);
11385638c82cSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
1139e7d70cd4SThomas Zimmermann 	struct ast_crtc_state *ast_state;
11403339fdf5SThomas Zimmermann 	const struct drm_format_info *format;
1141b48e1b6fSThomas Zimmermann 	bool succ;
1142016a14beSThomas Zimmermann 	int ret;
1143016a14beSThomas Zimmermann 
114429b77ad7SMaxime Ripard 	if (!crtc_state->enable)
1145963a2ba2SThomas Zimmermann 		return 0;
1146d6ddbd5cSThomas Zimmermann 
11478f2fd57dSThomas Zimmermann 	ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state);
11488f2fd57dSThomas Zimmermann 	if (ret)
11498f2fd57dSThomas Zimmermann 		return ret;
11508f2fd57dSThomas Zimmermann 
115129b77ad7SMaxime Ripard 	ast_state = to_ast_crtc_state(crtc_state);
1152b48e1b6fSThomas Zimmermann 
11533339fdf5SThomas Zimmermann 	format = ast_state->format;
11545638c82cSThomas Zimmermann 	if (drm_WARN_ON_ONCE(dev, !format))
11555638c82cSThomas Zimmermann 		return -EINVAL; /* BUG: We didn't set format in primary check(). */
1156e7d70cd4SThomas Zimmermann 
1157ce7fcf70SJocelyn Falempe 	/*
1158ce7fcf70SJocelyn Falempe 	 * The gamma LUT has to be reloaded after changing the primary
1159ce7fcf70SJocelyn Falempe 	 * plane's color format.
1160ce7fcf70SJocelyn Falempe 	 */
1161ce7fcf70SJocelyn Falempe 	if (old_ast_crtc_state->format != format)
1162ce7fcf70SJocelyn Falempe 		crtc_state->color_mgmt_changed = true;
1163ce7fcf70SJocelyn Falempe 
1164ce7fcf70SJocelyn Falempe 	if (crtc_state->color_mgmt_changed && crtc_state->gamma_lut) {
1165ce7fcf70SJocelyn Falempe 		if (crtc_state->gamma_lut->length !=
1166ce7fcf70SJocelyn Falempe 		    AST_LUT_SIZE * sizeof(struct drm_color_lut)) {
1167ce7fcf70SJocelyn Falempe 			drm_err(dev, "Wrong size for gamma_lut %zu\n",
1168ce7fcf70SJocelyn Falempe 				crtc_state->gamma_lut->length);
1169ce7fcf70SJocelyn Falempe 			return -EINVAL;
1170ce7fcf70SJocelyn Falempe 		}
1171ce7fcf70SJocelyn Falempe 	}
1172ce7fcf70SJocelyn Falempe 
117329b77ad7SMaxime Ripard 	succ = ast_get_vbios_mode_info(format, &crtc_state->mode,
117429b77ad7SMaxime Ripard 				       &crtc_state->adjusted_mode,
1175e7d70cd4SThomas Zimmermann 				       &ast_state->vbios_mode_info);
1176b48e1b6fSThomas Zimmermann 	if (!succ)
1177b48e1b6fSThomas Zimmermann 		return -EINVAL;
1178b48e1b6fSThomas Zimmermann 
1179963a2ba2SThomas Zimmermann 	return 0;
1180b48e1b6fSThomas Zimmermann }
1181b48e1b6fSThomas Zimmermann 
1182f3901b5fSThomas Zimmermann static void
ast_crtc_helper_atomic_flush(struct drm_crtc * crtc,struct drm_atomic_state * state)1183f6ebe9f9SMaxime Ripard ast_crtc_helper_atomic_flush(struct drm_crtc *crtc,
1184f6ebe9f9SMaxime Ripard 			     struct drm_atomic_state *state)
11858e3784dfSThomas Zimmermann {
1186253f28b6SMaxime Ripard 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
1187253f28b6SMaxime Ripard 									  crtc);
1188f870231fSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
11895abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(dev);
1190253f28b6SMaxime Ripard 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
1191594e9c04SKuoHsiang Chou 	struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
11928e3784dfSThomas Zimmermann 
11938e3784dfSThomas Zimmermann 	/*
11948e3784dfSThomas Zimmermann 	 * The gamma LUT has to be reloaded after changing the primary
11958e3784dfSThomas Zimmermann 	 * plane's color format.
11968e3784dfSThomas Zimmermann 	 */
1197ce7fcf70SJocelyn Falempe 	if (crtc_state->enable && crtc_state->color_mgmt_changed) {
1198ce7fcf70SJocelyn Falempe 		if (crtc_state->gamma_lut)
1199ce7fcf70SJocelyn Falempe 			ast_crtc_set_gamma(ast,
1200ce7fcf70SJocelyn Falempe 					   ast_crtc_state->format,
1201ce7fcf70SJocelyn Falempe 					   crtc_state->gamma_lut->data);
1202ce7fcf70SJocelyn Falempe 		else
1203ce7fcf70SJocelyn Falempe 			ast_crtc_set_gamma_linear(ast, ast_crtc_state->format);
1204ce7fcf70SJocelyn Falempe 	}
1205594e9c04SKuoHsiang Chou 
1206594e9c04SKuoHsiang Chou 	//Set Aspeed Display-Port
12077f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_ASTDP_BIT)
1208594e9c04SKuoHsiang Chou 		ast_dp_set_mode(crtc, vbios_mode_info);
12098e3784dfSThomas Zimmermann }
12108e3784dfSThomas Zimmermann 
ast_crtc_helper_atomic_enable(struct drm_crtc * crtc,struct drm_atomic_state * state)1211d95dcfc4SThomas Zimmermann static void ast_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state)
1212b48e1b6fSThomas Zimmermann {
121371d873ccSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
12145abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(dev);
1215d95dcfc4SThomas Zimmermann 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
12165638c82cSThomas Zimmermann 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
12175638c82cSThomas Zimmermann 	struct ast_vbios_mode_info *vbios_mode_info =
12185638c82cSThomas Zimmermann 		&ast_crtc_state->vbios_mode_info;
12195638c82cSThomas Zimmermann 	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
1220b48e1b6fSThomas Zimmermann 
1221e7d70cd4SThomas Zimmermann 	ast_set_vbios_mode_reg(ast, adjusted_mode, vbios_mode_info);
1222b48e1b6fSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa1, 0x06);
1223e7d70cd4SThomas Zimmermann 	ast_set_std_reg(ast, adjusted_mode, vbios_mode_info);
1224e7d70cd4SThomas Zimmermann 	ast_set_crtc_reg(ast, adjusted_mode, vbios_mode_info);
1225e7d70cd4SThomas Zimmermann 	ast_set_dclk_reg(ast, adjusted_mode, vbios_mode_info);
1226ae37025dSThomas Zimmermann 	ast_set_crtthd_reg(ast);
1227e7d70cd4SThomas Zimmermann 	ast_set_sync_reg(ast, adjusted_mode, vbios_mode_info);
1228b48e1b6fSThomas Zimmermann 
1229b48e1b6fSThomas Zimmermann 	ast_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
1230b48e1b6fSThomas Zimmermann }
1231b48e1b6fSThomas Zimmermann 
ast_crtc_helper_atomic_disable(struct drm_crtc * crtc,struct drm_atomic_state * state)1232d95dcfc4SThomas Zimmermann static void ast_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state)
1233b48e1b6fSThomas Zimmermann {
1234d95dcfc4SThomas Zimmermann 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
123539edb287SThomas Zimmermann 	struct drm_device *dev = crtc->dev;
12365abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(dev);
123739edb287SThomas Zimmermann 
1238b48e1b6fSThomas Zimmermann 	ast_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
123939edb287SThomas Zimmermann 
124039edb287SThomas Zimmermann 	/*
124139edb287SThomas Zimmermann 	 * HW cursors require the underlying primary plane and CRTC to
124239edb287SThomas Zimmermann 	 * display a valid mode and image. This is not the case during
124339edb287SThomas Zimmermann 	 * full modeset operations. So we temporarily disable any active
124439edb287SThomas Zimmermann 	 * plane, including the HW cursor. Each plane's atomic_update()
124539edb287SThomas Zimmermann 	 * helper will re-enable it if necessary.
124639edb287SThomas Zimmermann 	 *
124739edb287SThomas Zimmermann 	 * We only do this during *full* modesets. It does not affect
124839edb287SThomas Zimmermann 	 * simple pageflips on the planes.
124939edb287SThomas Zimmermann 	 */
125039edb287SThomas Zimmermann 	drm_atomic_helper_disable_planes_on_crtc(old_crtc_state, false);
125139edb287SThomas Zimmermann 
125239edb287SThomas Zimmermann 	/*
125339edb287SThomas Zimmermann 	 * Ensure that no scanout takes place before reprogramming mode
125439edb287SThomas Zimmermann 	 * and format registers.
125539edb287SThomas Zimmermann 	 */
125639edb287SThomas Zimmermann 	ast_wait_for_vretrace(ast);
1257b48e1b6fSThomas Zimmermann }
1258312fec14SDave Airlie 
1259312fec14SDave Airlie static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
12606abbad2cSThomas Zimmermann 	.mode_valid = ast_crtc_helper_mode_valid,
1261b48e1b6fSThomas Zimmermann 	.atomic_check = ast_crtc_helper_atomic_check,
12628e3784dfSThomas Zimmermann 	.atomic_flush = ast_crtc_helper_atomic_flush,
1263b48e1b6fSThomas Zimmermann 	.atomic_enable = ast_crtc_helper_atomic_enable,
1264b48e1b6fSThomas Zimmermann 	.atomic_disable = ast_crtc_helper_atomic_disable,
1265312fec14SDave Airlie };
1266312fec14SDave Airlie 
ast_crtc_reset(struct drm_crtc * crtc)1267f0adbc38SThomas Zimmermann static void ast_crtc_reset(struct drm_crtc *crtc)
1268f0adbc38SThomas Zimmermann {
1269f0adbc38SThomas Zimmermann 	struct ast_crtc_state *ast_state =
1270f0adbc38SThomas Zimmermann 		kzalloc(sizeof(*ast_state), GFP_KERNEL);
1271f0adbc38SThomas Zimmermann 
1272f0adbc38SThomas Zimmermann 	if (crtc->state)
1273f0adbc38SThomas Zimmermann 		crtc->funcs->atomic_destroy_state(crtc, crtc->state);
1274f0adbc38SThomas Zimmermann 
1275fea3fdf9SJiasheng Jiang 	if (ast_state)
1276f0adbc38SThomas Zimmermann 		__drm_atomic_helper_crtc_reset(crtc, &ast_state->base);
1277fea3fdf9SJiasheng Jiang 	else
1278fea3fdf9SJiasheng Jiang 		__drm_atomic_helper_crtc_reset(crtc, NULL);
1279f0adbc38SThomas Zimmermann }
1280f0adbc38SThomas Zimmermann 
128183be6a3cSThomas Zimmermann static struct drm_crtc_state *
ast_crtc_atomic_duplicate_state(struct drm_crtc * crtc)128283be6a3cSThomas Zimmermann ast_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
128383be6a3cSThomas Zimmermann {
1284e7d70cd4SThomas Zimmermann 	struct ast_crtc_state *new_ast_state, *ast_state;
12851a19b4cbSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
128683be6a3cSThomas Zimmermann 
12871a19b4cbSThomas Zimmermann 	if (drm_WARN_ON(dev, !crtc->state))
128883be6a3cSThomas Zimmermann 		return NULL;
128983be6a3cSThomas Zimmermann 
129083be6a3cSThomas Zimmermann 	new_ast_state = kmalloc(sizeof(*new_ast_state), GFP_KERNEL);
129183be6a3cSThomas Zimmermann 	if (!new_ast_state)
129283be6a3cSThomas Zimmermann 		return NULL;
129383be6a3cSThomas Zimmermann 	__drm_atomic_helper_crtc_duplicate_state(crtc, &new_ast_state->base);
129483be6a3cSThomas Zimmermann 
1295e7d70cd4SThomas Zimmermann 	ast_state = to_ast_crtc_state(crtc->state);
1296e7d70cd4SThomas Zimmermann 
12973339fdf5SThomas Zimmermann 	new_ast_state->format = ast_state->format;
1298e7d70cd4SThomas Zimmermann 	memcpy(&new_ast_state->vbios_mode_info, &ast_state->vbios_mode_info,
1299e7d70cd4SThomas Zimmermann 	       sizeof(new_ast_state->vbios_mode_info));
1300e7d70cd4SThomas Zimmermann 
130183be6a3cSThomas Zimmermann 	return &new_ast_state->base;
130283be6a3cSThomas Zimmermann }
130383be6a3cSThomas Zimmermann 
ast_crtc_atomic_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * state)130483be6a3cSThomas Zimmermann static void ast_crtc_atomic_destroy_state(struct drm_crtc *crtc,
130583be6a3cSThomas Zimmermann 					  struct drm_crtc_state *state)
130683be6a3cSThomas Zimmermann {
130783be6a3cSThomas Zimmermann 	struct ast_crtc_state *ast_state = to_ast_crtc_state(state);
130883be6a3cSThomas Zimmermann 
130983be6a3cSThomas Zimmermann 	__drm_atomic_helper_crtc_destroy_state(&ast_state->base);
131083be6a3cSThomas Zimmermann 	kfree(ast_state);
131183be6a3cSThomas Zimmermann }
131283be6a3cSThomas Zimmermann 
1313312fec14SDave Airlie static const struct drm_crtc_funcs ast_crtc_funcs = {
1314f0adbc38SThomas Zimmermann 	.reset = ast_crtc_reset,
13156a470dc2SThomas Zimmermann 	.destroy = drm_crtc_cleanup,
13164961eb60SThomas Zimmermann 	.set_config = drm_atomic_helper_set_config,
13174961eb60SThomas Zimmermann 	.page_flip = drm_atomic_helper_page_flip,
131883be6a3cSThomas Zimmermann 	.atomic_duplicate_state = ast_crtc_atomic_duplicate_state,
131983be6a3cSThomas Zimmermann 	.atomic_destroy_state = ast_crtc_atomic_destroy_state,
1320312fec14SDave Airlie };
1321312fec14SDave Airlie 
ast_crtc_init(struct drm_device * dev)13227f5ccd44SRashika static int ast_crtc_init(struct drm_device *dev)
1323312fec14SDave Airlie {
13245abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(dev);
13256a470dc2SThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
1326a6ff807bSThomas Zimmermann 	int ret;
1327312fec14SDave Airlie 
1328f2fa5a99SThomas Zimmermann 	ret = drm_crtc_init_with_planes(dev, crtc, &ast->primary_plane.base,
1329a0ba992dSThomas Zimmermann 					&ast->cursor_plane.base, &ast_crtc_funcs,
133002f3bb75SThomas Zimmermann 					NULL);
1331a6ff807bSThomas Zimmermann 	if (ret)
13326a470dc2SThomas Zimmermann 		return ret;
1333a6ff807bSThomas Zimmermann 
1334ce7fcf70SJocelyn Falempe 	drm_mode_crtc_set_gamma_size(crtc, AST_LUT_SIZE);
1335ce7fcf70SJocelyn Falempe 	drm_crtc_enable_color_mgmt(crtc, 0, false, AST_LUT_SIZE);
1336ce7fcf70SJocelyn Falempe 
1337c35da0edSThomas Zimmermann 	drm_crtc_helper_add(crtc, &ast_crtc_helper_funcs);
1338c35da0edSThomas Zimmermann 
1339312fec14SDave Airlie 	return 0;
1340312fec14SDave Airlie }
1341312fec14SDave Airlie 
13424961eb60SThomas Zimmermann /*
1343b20384d9SThomas Zimmermann  * VGA Connector
13444961eb60SThomas Zimmermann  */
13454961eb60SThomas Zimmermann 
ast_vga_connector_helper_get_modes(struct drm_connector * connector)1346b20384d9SThomas Zimmermann static int ast_vga_connector_helper_get_modes(struct drm_connector *connector)
1347312fec14SDave Airlie {
1348b20384d9SThomas Zimmermann 	struct ast_vga_connector *ast_vga_connector = to_ast_vga_connector(connector);
1349f870231fSThomas Zimmermann 	struct drm_device *dev = connector->dev;
13505abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(dev);
13513ab26eddSThomas Zimmermann 	struct edid *edid;
13523ab26eddSThomas Zimmermann 	int count;
1353312fec14SDave Airlie 
13543ab26eddSThomas Zimmermann 	if (!ast_vga_connector->i2c)
13553ab26eddSThomas Zimmermann 		goto err_drm_connector_update_edid_property;
1356312fec14SDave Airlie 
1357f870231fSThomas Zimmermann 	/*
1358f870231fSThomas Zimmermann 	 * Protect access to I/O registers from concurrent modesetting
1359f870231fSThomas Zimmermann 	 * by acquiring the I/O-register lock.
1360f870231fSThomas Zimmermann 	 */
1361f870231fSThomas Zimmermann 	mutex_lock(&ast->ioregs_lock);
1362f870231fSThomas Zimmermann 
1363b20384d9SThomas Zimmermann 	edid = drm_get_edid(connector, &ast_vga_connector->i2c->adapter);
136483c6620bSDave Airlie 	if (!edid)
1365f870231fSThomas Zimmermann 		goto err_mutex_unlock;
1366f870231fSThomas Zimmermann 
1367f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
1368312fec14SDave Airlie 
13693ab26eddSThomas Zimmermann 	count = drm_add_edid_modes(connector, edid);
137083c6620bSDave Airlie 	kfree(edid);
13713ab26eddSThomas Zimmermann 
13723ab26eddSThomas Zimmermann 	return count;
13733ab26eddSThomas Zimmermann 
1374f870231fSThomas Zimmermann err_mutex_unlock:
1375f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
13763ab26eddSThomas Zimmermann err_drm_connector_update_edid_property:
1377b20384d9SThomas Zimmermann 	drm_connector_update_edid_property(connector, NULL);
1378312fec14SDave Airlie 	return 0;
1379312fec14SDave Airlie }
1380312fec14SDave Airlie 
1381b20384d9SThomas Zimmermann static const struct drm_connector_helper_funcs ast_vga_connector_helper_funcs = {
1382b20384d9SThomas Zimmermann 	.get_modes = ast_vga_connector_helper_get_modes,
1383312fec14SDave Airlie };
1384312fec14SDave Airlie 
1385b20384d9SThomas Zimmermann static const struct drm_connector_funcs ast_vga_connector_funcs = {
13864961eb60SThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
1387312fec14SDave Airlie 	.fill_modes = drm_helper_probe_single_connector_modes,
1388a2cce09cSThomas Zimmermann 	.destroy = drm_connector_cleanup,
13894961eb60SThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
13904961eb60SThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1391312fec14SDave Airlie };
1392312fec14SDave Airlie 
ast_vga_connector_init(struct drm_device * dev,struct ast_vga_connector * ast_vga_connector)1393a59b0264SThomas Zimmermann static int ast_vga_connector_init(struct drm_device *dev,
1394a59b0264SThomas Zimmermann 				  struct ast_vga_connector *ast_vga_connector)
1395312fec14SDave Airlie {
1396b20384d9SThomas Zimmermann 	struct drm_connector *connector = &ast_vga_connector->base;
13979285f09eSThomas Zimmermann 	int ret;
1398312fec14SDave Airlie 
1399b20384d9SThomas Zimmermann 	ast_vga_connector->i2c = ast_i2c_create(dev);
1400b20384d9SThomas Zimmermann 	if (!ast_vga_connector->i2c)
14011a19b4cbSThomas Zimmermann 		drm_err(dev, "failed to add ddc bus for connector\n");
1402350fd554SAndrzej Pietrasiewicz 
1403b20384d9SThomas Zimmermann 	if (ast_vga_connector->i2c)
1404b20384d9SThomas Zimmermann 		ret = drm_connector_init_with_ddc(dev, connector, &ast_vga_connector_funcs,
1405350fd554SAndrzej Pietrasiewicz 						  DRM_MODE_CONNECTOR_VGA,
1406b20384d9SThomas Zimmermann 						  &ast_vga_connector->i2c->adapter);
140755dc449aSThomas Zimmermann 	else
1408b20384d9SThomas Zimmermann 		ret = drm_connector_init(dev, connector, &ast_vga_connector_funcs,
140955dc449aSThomas Zimmermann 					 DRM_MODE_CONNECTOR_VGA);
14109285f09eSThomas Zimmermann 	if (ret)
14119285f09eSThomas Zimmermann 		return ret;
1412312fec14SDave Airlie 
1413b20384d9SThomas Zimmermann 	drm_connector_helper_add(connector, &ast_vga_connector_helper_funcs);
1414312fec14SDave Airlie 
1415312fec14SDave Airlie 	connector->interlace_allowed = 0;
1416312fec14SDave Airlie 	connector->doublescan_allowed = 0;
1417312fec14SDave Airlie 
1418595cb5e0SKim Phillips 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1419312fec14SDave Airlie 
1420a59b0264SThomas Zimmermann 	return 0;
1421a59b0264SThomas Zimmermann }
1422a59b0264SThomas Zimmermann 
ast_vga_output_init(struct ast_device * ast)142337b42cf9SThomas Zimmermann static int ast_vga_output_init(struct ast_device *ast)
1424a59b0264SThomas Zimmermann {
1425a59b0264SThomas Zimmermann 	struct drm_device *dev = &ast->base;
1426a59b0264SThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
1427a59b0264SThomas Zimmermann 	struct drm_encoder *encoder = &ast->output.vga.encoder;
1428a59b0264SThomas Zimmermann 	struct ast_vga_connector *ast_vga_connector = &ast->output.vga.vga_connector;
1429a59b0264SThomas Zimmermann 	struct drm_connector *connector = &ast_vga_connector->base;
1430a59b0264SThomas Zimmermann 	int ret;
1431a59b0264SThomas Zimmermann 
1432a59b0264SThomas Zimmermann 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_DAC);
1433a59b0264SThomas Zimmermann 	if (ret)
1434a59b0264SThomas Zimmermann 		return ret;
1435f665147cSThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1436a59b0264SThomas Zimmermann 
1437a59b0264SThomas Zimmermann 	ret = ast_vga_connector_init(dev, ast_vga_connector);
1438a59b0264SThomas Zimmermann 	if (ret)
1439a59b0264SThomas Zimmermann 		return ret;
1440a59b0264SThomas Zimmermann 
1441a59b0264SThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
1442a59b0264SThomas Zimmermann 	if (ret)
1443a59b0264SThomas Zimmermann 		return ret;
1444312fec14SDave Airlie 
1445312fec14SDave Airlie 	return 0;
1446312fec14SDave Airlie }
1447312fec14SDave Airlie 
1448e6949ff3SThomas Zimmermann /*
14495e78d59aSThomas Zimmermann  * SIL164 Connector
14505e78d59aSThomas Zimmermann  */
14515e78d59aSThomas Zimmermann 
ast_sil164_connector_helper_get_modes(struct drm_connector * connector)14525e78d59aSThomas Zimmermann static int ast_sil164_connector_helper_get_modes(struct drm_connector *connector)
14535e78d59aSThomas Zimmermann {
14545e78d59aSThomas Zimmermann 	struct ast_sil164_connector *ast_sil164_connector = to_ast_sil164_connector(connector);
1455f870231fSThomas Zimmermann 	struct drm_device *dev = connector->dev;
14565abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(dev);
14575e78d59aSThomas Zimmermann 	struct edid *edid;
14585e78d59aSThomas Zimmermann 	int count;
14595e78d59aSThomas Zimmermann 
14605e78d59aSThomas Zimmermann 	if (!ast_sil164_connector->i2c)
14615e78d59aSThomas Zimmermann 		goto err_drm_connector_update_edid_property;
14625e78d59aSThomas Zimmermann 
1463f870231fSThomas Zimmermann 	/*
1464f870231fSThomas Zimmermann 	 * Protect access to I/O registers from concurrent modesetting
1465f870231fSThomas Zimmermann 	 * by acquiring the I/O-register lock.
1466f870231fSThomas Zimmermann 	 */
1467f870231fSThomas Zimmermann 	mutex_lock(&ast->ioregs_lock);
1468f870231fSThomas Zimmermann 
14695e78d59aSThomas Zimmermann 	edid = drm_get_edid(connector, &ast_sil164_connector->i2c->adapter);
14705e78d59aSThomas Zimmermann 	if (!edid)
1471f870231fSThomas Zimmermann 		goto err_mutex_unlock;
1472f870231fSThomas Zimmermann 
1473f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
14745e78d59aSThomas Zimmermann 
14755e78d59aSThomas Zimmermann 	count = drm_add_edid_modes(connector, edid);
14765e78d59aSThomas Zimmermann 	kfree(edid);
14775e78d59aSThomas Zimmermann 
14785e78d59aSThomas Zimmermann 	return count;
14795e78d59aSThomas Zimmermann 
1480f870231fSThomas Zimmermann err_mutex_unlock:
1481f870231fSThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
14825e78d59aSThomas Zimmermann err_drm_connector_update_edid_property:
14835e78d59aSThomas Zimmermann 	drm_connector_update_edid_property(connector, NULL);
14845e78d59aSThomas Zimmermann 	return 0;
14855e78d59aSThomas Zimmermann }
14865e78d59aSThomas Zimmermann 
14875e78d59aSThomas Zimmermann static const struct drm_connector_helper_funcs ast_sil164_connector_helper_funcs = {
14885e78d59aSThomas Zimmermann 	.get_modes = ast_sil164_connector_helper_get_modes,
14895e78d59aSThomas Zimmermann };
14905e78d59aSThomas Zimmermann 
14915e78d59aSThomas Zimmermann static const struct drm_connector_funcs ast_sil164_connector_funcs = {
14925e78d59aSThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
14935e78d59aSThomas Zimmermann 	.fill_modes = drm_helper_probe_single_connector_modes,
14945e78d59aSThomas Zimmermann 	.destroy = drm_connector_cleanup,
14955e78d59aSThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
14965e78d59aSThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
14975e78d59aSThomas Zimmermann };
14985e78d59aSThomas Zimmermann 
ast_sil164_connector_init(struct drm_device * dev,struct ast_sil164_connector * ast_sil164_connector)14995e78d59aSThomas Zimmermann static int ast_sil164_connector_init(struct drm_device *dev,
15005e78d59aSThomas Zimmermann 				     struct ast_sil164_connector *ast_sil164_connector)
15015e78d59aSThomas Zimmermann {
15025e78d59aSThomas Zimmermann 	struct drm_connector *connector = &ast_sil164_connector->base;
15035e78d59aSThomas Zimmermann 	int ret;
15045e78d59aSThomas Zimmermann 
15055e78d59aSThomas Zimmermann 	ast_sil164_connector->i2c = ast_i2c_create(dev);
15065e78d59aSThomas Zimmermann 	if (!ast_sil164_connector->i2c)
15075e78d59aSThomas Zimmermann 		drm_err(dev, "failed to add ddc bus for connector\n");
15085e78d59aSThomas Zimmermann 
15095e78d59aSThomas Zimmermann 	if (ast_sil164_connector->i2c)
15105e78d59aSThomas Zimmermann 		ret = drm_connector_init_with_ddc(dev, connector, &ast_sil164_connector_funcs,
15115e78d59aSThomas Zimmermann 						  DRM_MODE_CONNECTOR_DVII,
15125e78d59aSThomas Zimmermann 						  &ast_sil164_connector->i2c->adapter);
15135e78d59aSThomas Zimmermann 	else
15145e78d59aSThomas Zimmermann 		ret = drm_connector_init(dev, connector, &ast_sil164_connector_funcs,
15155e78d59aSThomas Zimmermann 					 DRM_MODE_CONNECTOR_DVII);
15165e78d59aSThomas Zimmermann 	if (ret)
15175e78d59aSThomas Zimmermann 		return ret;
15185e78d59aSThomas Zimmermann 
15195e78d59aSThomas Zimmermann 	drm_connector_helper_add(connector, &ast_sil164_connector_helper_funcs);
15205e78d59aSThomas Zimmermann 
15215e78d59aSThomas Zimmermann 	connector->interlace_allowed = 0;
15225e78d59aSThomas Zimmermann 	connector->doublescan_allowed = 0;
15235e78d59aSThomas Zimmermann 
15245e78d59aSThomas Zimmermann 	connector->polled = DRM_CONNECTOR_POLL_CONNECT;
15255e78d59aSThomas Zimmermann 
15265e78d59aSThomas Zimmermann 	return 0;
15275e78d59aSThomas Zimmermann }
15285e78d59aSThomas Zimmermann 
ast_sil164_output_init(struct ast_device * ast)152937b42cf9SThomas Zimmermann static int ast_sil164_output_init(struct ast_device *ast)
15305e78d59aSThomas Zimmermann {
15315e78d59aSThomas Zimmermann 	struct drm_device *dev = &ast->base;
15325e78d59aSThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
15335e78d59aSThomas Zimmermann 	struct drm_encoder *encoder = &ast->output.sil164.encoder;
15345e78d59aSThomas Zimmermann 	struct ast_sil164_connector *ast_sil164_connector = &ast->output.sil164.sil164_connector;
15355e78d59aSThomas Zimmermann 	struct drm_connector *connector = &ast_sil164_connector->base;
15365e78d59aSThomas Zimmermann 	int ret;
15375e78d59aSThomas Zimmermann 
15385e78d59aSThomas Zimmermann 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
15395e78d59aSThomas Zimmermann 	if (ret)
15405e78d59aSThomas Zimmermann 		return ret;
15415e78d59aSThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
15425e78d59aSThomas Zimmermann 
15435e78d59aSThomas Zimmermann 	ret = ast_sil164_connector_init(dev, ast_sil164_connector);
15445e78d59aSThomas Zimmermann 	if (ret)
15455e78d59aSThomas Zimmermann 		return ret;
15465e78d59aSThomas Zimmermann 
15475e78d59aSThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
15485e78d59aSThomas Zimmermann 	if (ret)
15495e78d59aSThomas Zimmermann 		return ret;
15505e78d59aSThomas Zimmermann 
15515e78d59aSThomas Zimmermann 	return 0;
15525e78d59aSThomas Zimmermann }
15535e78d59aSThomas Zimmermann 
15545e78d59aSThomas Zimmermann /*
15553ab26eddSThomas Zimmermann  * DP501 Connector
15563ab26eddSThomas Zimmermann  */
15573ab26eddSThomas Zimmermann 
ast_dp501_connector_helper_get_modes(struct drm_connector * connector)15583ab26eddSThomas Zimmermann static int ast_dp501_connector_helper_get_modes(struct drm_connector *connector)
15593ab26eddSThomas Zimmermann {
15603ab26eddSThomas Zimmermann 	void *edid;
15613ab26eddSThomas Zimmermann 	bool succ;
15623ab26eddSThomas Zimmermann 	int count;
15633ab26eddSThomas Zimmermann 
15643ab26eddSThomas Zimmermann 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
15653ab26eddSThomas Zimmermann 	if (!edid)
15663ab26eddSThomas Zimmermann 		goto err_drm_connector_update_edid_property;
15673ab26eddSThomas Zimmermann 
15683ab26eddSThomas Zimmermann 	succ = ast_dp501_read_edid(connector->dev, edid);
15693ab26eddSThomas Zimmermann 	if (!succ)
15703ab26eddSThomas Zimmermann 		goto err_kfree;
15713ab26eddSThomas Zimmermann 
15723ab26eddSThomas Zimmermann 	drm_connector_update_edid_property(connector, edid);
15733ab26eddSThomas Zimmermann 	count = drm_add_edid_modes(connector, edid);
15743ab26eddSThomas Zimmermann 	kfree(edid);
15753ab26eddSThomas Zimmermann 
15763ab26eddSThomas Zimmermann 	return count;
15773ab26eddSThomas Zimmermann 
15783ab26eddSThomas Zimmermann err_kfree:
15793ab26eddSThomas Zimmermann 	kfree(edid);
15803ab26eddSThomas Zimmermann err_drm_connector_update_edid_property:
15813ab26eddSThomas Zimmermann 	drm_connector_update_edid_property(connector, NULL);
15823ab26eddSThomas Zimmermann 	return 0;
15833ab26eddSThomas Zimmermann }
15843ab26eddSThomas Zimmermann 
ast_dp501_connector_helper_detect_ctx(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)1585f81bb0acSJocelyn Falempe static int ast_dp501_connector_helper_detect_ctx(struct drm_connector *connector,
1586f81bb0acSJocelyn Falempe 						 struct drm_modeset_acquire_ctx *ctx,
1587f81bb0acSJocelyn Falempe 						 bool force)
1588f81bb0acSJocelyn Falempe {
1589f81bb0acSJocelyn Falempe 	struct ast_device *ast = to_ast_device(connector->dev);
1590f81bb0acSJocelyn Falempe 
1591f81bb0acSJocelyn Falempe 	if (ast_dp501_is_connected(ast))
1592f81bb0acSJocelyn Falempe 		return connector_status_connected;
1593f81bb0acSJocelyn Falempe 	return connector_status_disconnected;
1594f81bb0acSJocelyn Falempe }
1595f81bb0acSJocelyn Falempe 
15963ab26eddSThomas Zimmermann static const struct drm_connector_helper_funcs ast_dp501_connector_helper_funcs = {
15973ab26eddSThomas Zimmermann 	.get_modes = ast_dp501_connector_helper_get_modes,
1598f81bb0acSJocelyn Falempe 	.detect_ctx = ast_dp501_connector_helper_detect_ctx,
15993ab26eddSThomas Zimmermann };
16003ab26eddSThomas Zimmermann 
16013ab26eddSThomas Zimmermann static const struct drm_connector_funcs ast_dp501_connector_funcs = {
16023ab26eddSThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
16033ab26eddSThomas Zimmermann 	.fill_modes = drm_helper_probe_single_connector_modes,
16043ab26eddSThomas Zimmermann 	.destroy = drm_connector_cleanup,
16053ab26eddSThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
16063ab26eddSThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
16073ab26eddSThomas Zimmermann };
16083ab26eddSThomas Zimmermann 
ast_dp501_connector_init(struct drm_device * dev,struct drm_connector * connector)16093ab26eddSThomas Zimmermann static int ast_dp501_connector_init(struct drm_device *dev, struct drm_connector *connector)
16103ab26eddSThomas Zimmermann {
16113ab26eddSThomas Zimmermann 	int ret;
16123ab26eddSThomas Zimmermann 
16133ab26eddSThomas Zimmermann 	ret = drm_connector_init(dev, connector, &ast_dp501_connector_funcs,
16143ab26eddSThomas Zimmermann 				 DRM_MODE_CONNECTOR_DisplayPort);
16153ab26eddSThomas Zimmermann 	if (ret)
16163ab26eddSThomas Zimmermann 		return ret;
16173ab26eddSThomas Zimmermann 
16183ab26eddSThomas Zimmermann 	drm_connector_helper_add(connector, &ast_dp501_connector_helper_funcs);
16193ab26eddSThomas Zimmermann 
16203ab26eddSThomas Zimmermann 	connector->interlace_allowed = 0;
16213ab26eddSThomas Zimmermann 	connector->doublescan_allowed = 0;
16223ab26eddSThomas Zimmermann 
1623f81bb0acSJocelyn Falempe 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
16243ab26eddSThomas Zimmermann 
16253ab26eddSThomas Zimmermann 	return 0;
16263ab26eddSThomas Zimmermann }
16273ab26eddSThomas Zimmermann 
ast_dp501_output_init(struct ast_device * ast)162837b42cf9SThomas Zimmermann static int ast_dp501_output_init(struct ast_device *ast)
16293ab26eddSThomas Zimmermann {
16303ab26eddSThomas Zimmermann 	struct drm_device *dev = &ast->base;
16313ab26eddSThomas Zimmermann 	struct drm_crtc *crtc = &ast->crtc;
16323ab26eddSThomas Zimmermann 	struct drm_encoder *encoder = &ast->output.dp501.encoder;
16333ab26eddSThomas Zimmermann 	struct drm_connector *connector = &ast->output.dp501.connector;
16343ab26eddSThomas Zimmermann 	int ret;
16353ab26eddSThomas Zimmermann 
16363ab26eddSThomas Zimmermann 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
16373ab26eddSThomas Zimmermann 	if (ret)
16383ab26eddSThomas Zimmermann 		return ret;
16393ab26eddSThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
16403ab26eddSThomas Zimmermann 
16413ab26eddSThomas Zimmermann 	ret = ast_dp501_connector_init(dev, connector);
16423ab26eddSThomas Zimmermann 	if (ret)
16433ab26eddSThomas Zimmermann 		return ret;
16443ab26eddSThomas Zimmermann 
16453ab26eddSThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
16463ab26eddSThomas Zimmermann 	if (ret)
16473ab26eddSThomas Zimmermann 		return ret;
1648312fec14SDave Airlie 
1649312fec14SDave Airlie 	return 0;
16507f5ccd44SRashika }
1651312fec14SDave Airlie 
1652e6949ff3SThomas Zimmermann /*
1653594e9c04SKuoHsiang Chou  * ASPEED Display-Port Connector
1654594e9c04SKuoHsiang Chou  */
1655594e9c04SKuoHsiang Chou 
ast_astdp_connector_helper_get_modes(struct drm_connector * connector)1656594e9c04SKuoHsiang Chou static int ast_astdp_connector_helper_get_modes(struct drm_connector *connector)
1657594e9c04SKuoHsiang Chou {
1658594e9c04SKuoHsiang Chou 	void *edid;
16593692ababSJammy Huang 	struct drm_device *dev = connector->dev;
16603692ababSJammy Huang 	struct ast_device *ast = to_ast_device(dev);
1661594e9c04SKuoHsiang Chou 
1662594e9c04SKuoHsiang Chou 	int succ;
1663594e9c04SKuoHsiang Chou 	int count;
1664594e9c04SKuoHsiang Chou 
1665594e9c04SKuoHsiang Chou 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1666594e9c04SKuoHsiang Chou 	if (!edid)
1667594e9c04SKuoHsiang Chou 		goto err_drm_connector_update_edid_property;
1668594e9c04SKuoHsiang Chou 
16693692ababSJammy Huang 	/*
16703692ababSJammy Huang 	 * Protect access to I/O registers from concurrent modesetting
16713692ababSJammy Huang 	 * by acquiring the I/O-register lock.
16723692ababSJammy Huang 	 */
16733692ababSJammy Huang 	mutex_lock(&ast->ioregs_lock);
16743692ababSJammy Huang 
1675594e9c04SKuoHsiang Chou 	succ = ast_astdp_read_edid(connector->dev, edid);
1676594e9c04SKuoHsiang Chou 	if (succ < 0)
16773692ababSJammy Huang 		goto err_mutex_unlock;
16783692ababSJammy Huang 
16793692ababSJammy Huang 	mutex_unlock(&ast->ioregs_lock);
1680594e9c04SKuoHsiang Chou 
1681594e9c04SKuoHsiang Chou 	drm_connector_update_edid_property(connector, edid);
1682594e9c04SKuoHsiang Chou 	count = drm_add_edid_modes(connector, edid);
1683594e9c04SKuoHsiang Chou 	kfree(edid);
1684594e9c04SKuoHsiang Chou 
1685594e9c04SKuoHsiang Chou 	return count;
1686594e9c04SKuoHsiang Chou 
16873692ababSJammy Huang err_mutex_unlock:
16883692ababSJammy Huang 	mutex_unlock(&ast->ioregs_lock);
1689594e9c04SKuoHsiang Chou 	kfree(edid);
1690594e9c04SKuoHsiang Chou err_drm_connector_update_edid_property:
1691594e9c04SKuoHsiang Chou 	drm_connector_update_edid_property(connector, NULL);
1692594e9c04SKuoHsiang Chou 	return 0;
1693594e9c04SKuoHsiang Chou }
1694594e9c04SKuoHsiang Chou 
ast_astdp_connector_helper_detect_ctx(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)1695f81bb0acSJocelyn Falempe static int ast_astdp_connector_helper_detect_ctx(struct drm_connector *connector,
1696f81bb0acSJocelyn Falempe 						 struct drm_modeset_acquire_ctx *ctx,
1697f81bb0acSJocelyn Falempe 						 bool force)
1698f81bb0acSJocelyn Falempe {
1699f81bb0acSJocelyn Falempe 	struct ast_device *ast = to_ast_device(connector->dev);
1700f81bb0acSJocelyn Falempe 
1701f81bb0acSJocelyn Falempe 	if (ast_astdp_is_connected(ast))
1702f81bb0acSJocelyn Falempe 		return connector_status_connected;
1703f81bb0acSJocelyn Falempe 	return connector_status_disconnected;
1704f81bb0acSJocelyn Falempe }
1705f81bb0acSJocelyn Falempe 
1706594e9c04SKuoHsiang Chou static const struct drm_connector_helper_funcs ast_astdp_connector_helper_funcs = {
1707594e9c04SKuoHsiang Chou 	.get_modes = ast_astdp_connector_helper_get_modes,
1708f81bb0acSJocelyn Falempe 	.detect_ctx = ast_astdp_connector_helper_detect_ctx,
1709594e9c04SKuoHsiang Chou };
1710594e9c04SKuoHsiang Chou 
1711594e9c04SKuoHsiang Chou static const struct drm_connector_funcs ast_astdp_connector_funcs = {
1712594e9c04SKuoHsiang Chou 	.reset = drm_atomic_helper_connector_reset,
1713594e9c04SKuoHsiang Chou 	.fill_modes = drm_helper_probe_single_connector_modes,
1714594e9c04SKuoHsiang Chou 	.destroy = drm_connector_cleanup,
1715594e9c04SKuoHsiang Chou 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1716594e9c04SKuoHsiang Chou 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1717594e9c04SKuoHsiang Chou };
1718594e9c04SKuoHsiang Chou 
ast_astdp_connector_init(struct drm_device * dev,struct drm_connector * connector)1719594e9c04SKuoHsiang Chou static int ast_astdp_connector_init(struct drm_device *dev, struct drm_connector *connector)
1720594e9c04SKuoHsiang Chou {
1721594e9c04SKuoHsiang Chou 	int ret;
1722594e9c04SKuoHsiang Chou 
1723594e9c04SKuoHsiang Chou 	ret = drm_connector_init(dev, connector, &ast_astdp_connector_funcs,
1724594e9c04SKuoHsiang Chou 				 DRM_MODE_CONNECTOR_DisplayPort);
1725594e9c04SKuoHsiang Chou 	if (ret)
1726594e9c04SKuoHsiang Chou 		return ret;
1727594e9c04SKuoHsiang Chou 
1728594e9c04SKuoHsiang Chou 	drm_connector_helper_add(connector, &ast_astdp_connector_helper_funcs);
1729594e9c04SKuoHsiang Chou 
1730594e9c04SKuoHsiang Chou 	connector->interlace_allowed = 0;
1731594e9c04SKuoHsiang Chou 	connector->doublescan_allowed = 0;
1732594e9c04SKuoHsiang Chou 
1733f81bb0acSJocelyn Falempe 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
1734594e9c04SKuoHsiang Chou 
1735594e9c04SKuoHsiang Chou 	return 0;
1736594e9c04SKuoHsiang Chou }
1737594e9c04SKuoHsiang Chou 
ast_astdp_output_init(struct ast_device * ast)173837b42cf9SThomas Zimmermann static int ast_astdp_output_init(struct ast_device *ast)
1739594e9c04SKuoHsiang Chou {
1740594e9c04SKuoHsiang Chou 	struct drm_device *dev = &ast->base;
1741594e9c04SKuoHsiang Chou 	struct drm_crtc *crtc = &ast->crtc;
1742594e9c04SKuoHsiang Chou 	struct drm_encoder *encoder = &ast->output.astdp.encoder;
1743594e9c04SKuoHsiang Chou 	struct drm_connector *connector = &ast->output.astdp.connector;
1744594e9c04SKuoHsiang Chou 	int ret;
1745594e9c04SKuoHsiang Chou 
1746594e9c04SKuoHsiang Chou 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
1747594e9c04SKuoHsiang Chou 	if (ret)
1748594e9c04SKuoHsiang Chou 		return ret;
1749594e9c04SKuoHsiang Chou 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1750594e9c04SKuoHsiang Chou 
1751594e9c04SKuoHsiang Chou 	ret = ast_astdp_connector_init(dev, connector);
1752594e9c04SKuoHsiang Chou 	if (ret)
1753594e9c04SKuoHsiang Chou 		return ret;
1754594e9c04SKuoHsiang Chou 
1755594e9c04SKuoHsiang Chou 	ret = drm_connector_attach_encoder(connector, encoder);
1756594e9c04SKuoHsiang Chou 	if (ret)
1757594e9c04SKuoHsiang Chou 		return ret;
1758594e9c04SKuoHsiang Chou 
1759594e9c04SKuoHsiang Chou 	return 0;
1760594e9c04SKuoHsiang Chou }
1761594e9c04SKuoHsiang Chou 
1762594e9c04SKuoHsiang Chou /*
1763e329cb53SJocelyn Falempe  * BMC virtual Connector
1764e329cb53SJocelyn Falempe  */
1765e329cb53SJocelyn Falempe 
1766e329cb53SJocelyn Falempe static const struct drm_encoder_funcs ast_bmc_encoder_funcs = {
1767e329cb53SJocelyn Falempe 	.destroy = drm_encoder_cleanup,
1768e329cb53SJocelyn Falempe };
1769e329cb53SJocelyn Falempe 
ast_bmc_connector_helper_detect_ctx(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)1770*f1a0e42fSThomas Zimmermann static int ast_bmc_connector_helper_detect_ctx(struct drm_connector *connector,
1771*f1a0e42fSThomas Zimmermann 					       struct drm_modeset_acquire_ctx *ctx,
1772*f1a0e42fSThomas Zimmermann 					       bool force)
1773*f1a0e42fSThomas Zimmermann {
1774*f1a0e42fSThomas Zimmermann 	struct ast_bmc_connector *bmc_connector = to_ast_bmc_connector(connector);
1775*f1a0e42fSThomas Zimmermann 	struct drm_connector *physical_connector = bmc_connector->physical_connector;
1776*f1a0e42fSThomas Zimmermann 
1777*f1a0e42fSThomas Zimmermann 	/*
1778*f1a0e42fSThomas Zimmermann 	 * Most user-space compositors cannot handle more than one connected
1779*f1a0e42fSThomas Zimmermann 	 * connector per CRTC. Hence, we only mark the BMC as connected if the
1780*f1a0e42fSThomas Zimmermann 	 * physical connector is disconnected. If the physical connector's status
1781*f1a0e42fSThomas Zimmermann 	 * is connected or unknown, the BMC remains disconnected. This has no
1782*f1a0e42fSThomas Zimmermann 	 * effect on the output of the BMC.
1783*f1a0e42fSThomas Zimmermann 	 *
1784*f1a0e42fSThomas Zimmermann 	 * FIXME: Remove this logic once user-space compositors can handle more
1785*f1a0e42fSThomas Zimmermann 	 *        than one connector per CRTC. The BMC should always be connected.
1786*f1a0e42fSThomas Zimmermann 	 */
1787*f1a0e42fSThomas Zimmermann 
1788*f1a0e42fSThomas Zimmermann 	if (physical_connector && physical_connector->status == connector_status_disconnected)
1789*f1a0e42fSThomas Zimmermann 		return connector_status_connected;
1790*f1a0e42fSThomas Zimmermann 
1791*f1a0e42fSThomas Zimmermann 	return connector_status_disconnected;
1792*f1a0e42fSThomas Zimmermann }
1793*f1a0e42fSThomas Zimmermann 
ast_bmc_connector_helper_get_modes(struct drm_connector * connector)1794e329cb53SJocelyn Falempe static int ast_bmc_connector_helper_get_modes(struct drm_connector *connector)
1795e329cb53SJocelyn Falempe {
1796e329cb53SJocelyn Falempe 	return drm_add_modes_noedid(connector, 4096, 4096);
1797e329cb53SJocelyn Falempe }
1798e329cb53SJocelyn Falempe 
1799e329cb53SJocelyn Falempe static const struct drm_connector_helper_funcs ast_bmc_connector_helper_funcs = {
1800e329cb53SJocelyn Falempe 	.get_modes = ast_bmc_connector_helper_get_modes,
1801*f1a0e42fSThomas Zimmermann 	.detect_ctx = ast_bmc_connector_helper_detect_ctx,
1802e329cb53SJocelyn Falempe };
1803e329cb53SJocelyn Falempe 
1804e329cb53SJocelyn Falempe static const struct drm_connector_funcs ast_bmc_connector_funcs = {
1805e329cb53SJocelyn Falempe 	.reset = drm_atomic_helper_connector_reset,
1806e329cb53SJocelyn Falempe 	.fill_modes = drm_helper_probe_single_connector_modes,
1807e329cb53SJocelyn Falempe 	.destroy = drm_connector_cleanup,
1808e329cb53SJocelyn Falempe 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1809e329cb53SJocelyn Falempe 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1810e329cb53SJocelyn Falempe };
1811e329cb53SJocelyn Falempe 
ast_bmc_connector_init(struct drm_device * dev,struct ast_bmc_connector * bmc_connector,struct drm_connector * physical_connector)1812*f1a0e42fSThomas Zimmermann static int ast_bmc_connector_init(struct drm_device *dev,
1813*f1a0e42fSThomas Zimmermann 				  struct ast_bmc_connector *bmc_connector,
1814*f1a0e42fSThomas Zimmermann 				  struct drm_connector *physical_connector)
1815*f1a0e42fSThomas Zimmermann {
1816*f1a0e42fSThomas Zimmermann 	struct drm_connector *connector = &bmc_connector->base;
1817*f1a0e42fSThomas Zimmermann 	int ret;
1818*f1a0e42fSThomas Zimmermann 
1819*f1a0e42fSThomas Zimmermann 	ret = drm_connector_init(dev, connector, &ast_bmc_connector_funcs,
1820*f1a0e42fSThomas Zimmermann 				 DRM_MODE_CONNECTOR_VIRTUAL);
1821*f1a0e42fSThomas Zimmermann 	if (ret)
1822*f1a0e42fSThomas Zimmermann 		return ret;
1823*f1a0e42fSThomas Zimmermann 
1824*f1a0e42fSThomas Zimmermann 	drm_connector_helper_add(connector, &ast_bmc_connector_helper_funcs);
1825*f1a0e42fSThomas Zimmermann 
1826*f1a0e42fSThomas Zimmermann 	bmc_connector->physical_connector = physical_connector;
1827*f1a0e42fSThomas Zimmermann 
1828*f1a0e42fSThomas Zimmermann 	return 0;
1829*f1a0e42fSThomas Zimmermann }
1830*f1a0e42fSThomas Zimmermann 
ast_bmc_output_init(struct ast_device * ast,struct drm_connector * physical_connector)1831*f1a0e42fSThomas Zimmermann static int ast_bmc_output_init(struct ast_device *ast,
1832*f1a0e42fSThomas Zimmermann 			       struct drm_connector *physical_connector)
1833e329cb53SJocelyn Falempe {
1834e329cb53SJocelyn Falempe 	struct drm_device *dev = &ast->base;
1835e329cb53SJocelyn Falempe 	struct drm_crtc *crtc = &ast->crtc;
1836e329cb53SJocelyn Falempe 	struct drm_encoder *encoder = &ast->output.bmc.encoder;
1837*f1a0e42fSThomas Zimmermann 	struct ast_bmc_connector *bmc_connector = &ast->output.bmc.bmc_connector;
1838*f1a0e42fSThomas Zimmermann 	struct drm_connector *connector = &bmc_connector->base;
1839e329cb53SJocelyn Falempe 	int ret;
1840e329cb53SJocelyn Falempe 
1841e329cb53SJocelyn Falempe 	ret = drm_encoder_init(dev, encoder,
1842e329cb53SJocelyn Falempe 			       &ast_bmc_encoder_funcs,
1843e329cb53SJocelyn Falempe 			       DRM_MODE_ENCODER_VIRTUAL, "ast_bmc");
1844e329cb53SJocelyn Falempe 	if (ret)
1845e329cb53SJocelyn Falempe 		return ret;
1846e329cb53SJocelyn Falempe 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1847e329cb53SJocelyn Falempe 
1848*f1a0e42fSThomas Zimmermann 	ret = ast_bmc_connector_init(dev, bmc_connector, physical_connector);
1849e329cb53SJocelyn Falempe 	if (ret)
1850e329cb53SJocelyn Falempe 		return ret;
1851e329cb53SJocelyn Falempe 
1852e329cb53SJocelyn Falempe 	ret = drm_connector_attach_encoder(connector, encoder);
1853e329cb53SJocelyn Falempe 	if (ret)
1854e329cb53SJocelyn Falempe 		return ret;
1855e329cb53SJocelyn Falempe 
1856e329cb53SJocelyn Falempe 	return 0;
1857e329cb53SJocelyn Falempe }
1858e329cb53SJocelyn Falempe 
1859e329cb53SJocelyn Falempe /*
1860e6949ff3SThomas Zimmermann  * Mode config
1861e6949ff3SThomas Zimmermann  */
1862e6949ff3SThomas Zimmermann 
ast_mode_config_helper_atomic_commit_tail(struct drm_atomic_state * state)18631fe18215SThomas Zimmermann static void ast_mode_config_helper_atomic_commit_tail(struct drm_atomic_state *state)
18641fe18215SThomas Zimmermann {
18655abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(state->dev);
18661fe18215SThomas Zimmermann 
18671fe18215SThomas Zimmermann 	/*
18681fe18215SThomas Zimmermann 	 * Concurrent operations could possibly trigger a call to
18691fe18215SThomas Zimmermann 	 * drm_connector_helper_funcs.get_modes by trying to read the
18701fe18215SThomas Zimmermann 	 * display modes. Protect access to I/O registers by acquiring
18711fe18215SThomas Zimmermann 	 * the I/O-register lock. Released in atomic_flush().
18721fe18215SThomas Zimmermann 	 */
18731fe18215SThomas Zimmermann 	mutex_lock(&ast->ioregs_lock);
18741fe18215SThomas Zimmermann 	drm_atomic_helper_commit_tail_rpm(state);
18751fe18215SThomas Zimmermann 	mutex_unlock(&ast->ioregs_lock);
18761fe18215SThomas Zimmermann }
18771fe18215SThomas Zimmermann 
1878b20384d9SThomas Zimmermann static const struct drm_mode_config_helper_funcs ast_mode_config_helper_funcs = {
18791fe18215SThomas Zimmermann 	.atomic_commit_tail = ast_mode_config_helper_atomic_commit_tail,
18802f0ddd89SThomas Zimmermann };
18812f0ddd89SThomas Zimmermann 
ast_mode_config_mode_valid(struct drm_device * dev,const struct drm_display_mode * mode)1882f2fa5a99SThomas Zimmermann static enum drm_mode_status ast_mode_config_mode_valid(struct drm_device *dev,
1883f2fa5a99SThomas Zimmermann 						       const struct drm_display_mode *mode)
1884f2fa5a99SThomas Zimmermann {
1885f2fa5a99SThomas Zimmermann 	static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
18865abaa683SThomas Zimmermann 	struct ast_device *ast = to_ast_device(dev);
1887f2fa5a99SThomas Zimmermann 	unsigned long fbsize, fbpages, max_fbpages;
1888f2fa5a99SThomas Zimmermann 
1889f2fa5a99SThomas Zimmermann 	max_fbpages = (ast->vram_fb_available) >> PAGE_SHIFT;
1890f2fa5a99SThomas Zimmermann 
1891f2fa5a99SThomas Zimmermann 	fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1892f2fa5a99SThomas Zimmermann 	fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1893f2fa5a99SThomas Zimmermann 
1894f2fa5a99SThomas Zimmermann 	if (fbpages > max_fbpages)
1895f2fa5a99SThomas Zimmermann 		return MODE_MEM;
1896f2fa5a99SThomas Zimmermann 
1897f2fa5a99SThomas Zimmermann 	return MODE_OK;
1898f2fa5a99SThomas Zimmermann }
1899f2fa5a99SThomas Zimmermann 
1900e6949ff3SThomas Zimmermann static const struct drm_mode_config_funcs ast_mode_config_funcs = {
1901f2fa5a99SThomas Zimmermann 	.fb_create = drm_gem_fb_create_with_dirty,
1902f2fa5a99SThomas Zimmermann 	.mode_valid = ast_mode_config_mode_valid,
1903e6949ff3SThomas Zimmermann 	.atomic_check = drm_atomic_helper_check,
1904e6949ff3SThomas Zimmermann 	.atomic_commit = drm_atomic_helper_commit,
1905e6949ff3SThomas Zimmermann };
1906e6949ff3SThomas Zimmermann 
ast_mode_config_init(struct ast_device * ast)190737b42cf9SThomas Zimmermann int ast_mode_config_init(struct ast_device *ast)
1908312fec14SDave Airlie {
1909e0f5a738SThomas Zimmermann 	struct drm_device *dev = &ast->base;
1910*f1a0e42fSThomas Zimmermann 	struct drm_connector *physical_connector = NULL;
1911a6ff807bSThomas Zimmermann 	int ret;
1912a6ff807bSThomas Zimmermann 
1913e6949ff3SThomas Zimmermann 	ret = drmm_mode_config_init(dev);
1914e6949ff3SThomas Zimmermann 	if (ret)
1915e6949ff3SThomas Zimmermann 		return ret;
1916e6949ff3SThomas Zimmermann 
1917e6949ff3SThomas Zimmermann 	dev->mode_config.funcs = &ast_mode_config_funcs;
1918e6949ff3SThomas Zimmermann 	dev->mode_config.min_width = 0;
1919e6949ff3SThomas Zimmermann 	dev->mode_config.min_height = 0;
1920e6949ff3SThomas Zimmermann 	dev->mode_config.preferred_depth = 24;
1921e6949ff3SThomas Zimmermann 
1922ecf64579SThomas Zimmermann 	if (ast->chip == AST2100 || // GEN2, but not AST1100 (?)
1923ecf64579SThomas Zimmermann 	    ast->chip == AST2200 || // GEN3, but not AST2150 (?)
1924ecf64579SThomas Zimmermann 	    IS_AST_GEN7(ast) ||
1925ecf64579SThomas Zimmermann 	    IS_AST_GEN6(ast) ||
1926ecf64579SThomas Zimmermann 	    IS_AST_GEN5(ast) ||
1927ecf64579SThomas Zimmermann 	    IS_AST_GEN4(ast)) {
1928e6949ff3SThomas Zimmermann 		dev->mode_config.max_width = 1920;
1929e6949ff3SThomas Zimmermann 		dev->mode_config.max_height = 2048;
1930e6949ff3SThomas Zimmermann 	} else {
1931e6949ff3SThomas Zimmermann 		dev->mode_config.max_width = 1600;
1932e6949ff3SThomas Zimmermann 		dev->mode_config.max_height = 1200;
1933e6949ff3SThomas Zimmermann 	}
1934e6949ff3SThomas Zimmermann 
19352f0ddd89SThomas Zimmermann 	dev->mode_config.helper_private = &ast_mode_config_helper_funcs;
19362f0ddd89SThomas Zimmermann 
1937616048afSThomas Zimmermann 	ret = ast_primary_plane_init(ast);
1938616048afSThomas Zimmermann 	if (ret)
193902f3bb75SThomas Zimmermann 		return ret;
1940616048afSThomas Zimmermann 
1941616048afSThomas Zimmermann 	ret = ast_cursor_plane_init(ast);
1942616048afSThomas Zimmermann 	if (ret)
1943616048afSThomas Zimmermann 		return ret;
194402f3bb75SThomas Zimmermann 
1945312fec14SDave Airlie 	ast_crtc_init(dev);
1946a59b0264SThomas Zimmermann 
19477f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_NONE_BIT) {
1948a59b0264SThomas Zimmermann 		ret = ast_vga_output_init(ast);
1949a59b0264SThomas Zimmermann 		if (ret)
1950a59b0264SThomas Zimmermann 			return ret;
1951*f1a0e42fSThomas Zimmermann 		physical_connector = &ast->output.vga.vga_connector.base;
19527f35680aSThomas Zimmermann 	}
19537f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_SIL164_BIT) {
19547f35680aSThomas Zimmermann 		ret = ast_sil164_output_init(ast);
19557f35680aSThomas Zimmermann 		if (ret)
19567f35680aSThomas Zimmermann 			return ret;
1957*f1a0e42fSThomas Zimmermann 		physical_connector = &ast->output.sil164.sil164_connector.base;
19587f35680aSThomas Zimmermann 	}
19597f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_DP501_BIT) {
19607f35680aSThomas Zimmermann 		ret = ast_dp501_output_init(ast);
19617f35680aSThomas Zimmermann 		if (ret)
19627f35680aSThomas Zimmermann 			return ret;
1963*f1a0e42fSThomas Zimmermann 		physical_connector = &ast->output.dp501.connector;
19647f35680aSThomas Zimmermann 	}
19657f35680aSThomas Zimmermann 	if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
19667f35680aSThomas Zimmermann 		ret = ast_astdp_output_init(ast);
19677f35680aSThomas Zimmermann 		if (ret)
19687f35680aSThomas Zimmermann 			return ret;
1969*f1a0e42fSThomas Zimmermann 		physical_connector = &ast->output.astdp.connector;
19707f35680aSThomas Zimmermann 	}
1971*f1a0e42fSThomas Zimmermann 	ret = ast_bmc_output_init(ast, physical_connector);
1972e329cb53SJocelyn Falempe 	if (ret)
1973e329cb53SJocelyn Falempe 		return ret;
1974a6ff807bSThomas Zimmermann 
1975e6949ff3SThomas Zimmermann 	drm_mode_config_reset(dev);
1976e6949ff3SThomas Zimmermann 
1977f81bb0acSJocelyn Falempe 	drm_kms_helper_poll_init(dev);
1978f81bb0acSJocelyn Falempe 
1979312fec14SDave Airlie 	return 0;
1980312fec14SDave Airlie }
1981