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