ast_mode.c 42.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright 2012 Red Hat Inc.
 * Parts based on xf86-video-ast
 * Copyright (c) 2005 ASPEED Technology Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 */
/*
 * Authors: Dave Airlie <airlied@redhat.com>
 */
S
Sam Ravnborg 已提交
30

31
#include <linux/export.h>
S
Sam Ravnborg 已提交
32 33
#include <linux/pci.h>

34
#include <drm/drm_atomic.h>
T
Thomas Zimmermann 已提交
35 36
#include <drm/drm_atomic_helper.h>
#include <drm/drm_atomic_state_helper.h>
37 38
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
S
Sam Ravnborg 已提交
39
#include <drm/drm_fourcc.h>
40
#include <drm/drm_gem_framebuffer_helper.h>
S
Sam Ravnborg 已提交
41
#include <drm/drm_gem_vram_helper.h>
42
#include <drm/drm_plane_helper.h>
43
#include <drm/drm_probe_helper.h>
T
Thomas Zimmermann 已提交
44
#include <drm/drm_simple_kms_helper.h>
45

S
Sam Ravnborg 已提交
46
#include "ast_drv.h"
47 48 49 50
#include "ast_tables.h"

static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev);
static void ast_i2c_destroy(struct ast_i2c_chan *i2c);
T
Thomas Zimmermann 已提交
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65
static inline void ast_load_palette_index(struct ast_private *ast,
				     u8 index, u8 red, u8 green,
				     u8 blue)
{
	ast_io_write8(ast, AST_IO_DAC_INDEX_WRITE, index);
	ast_io_read8(ast, AST_IO_SEQ_PORT);
	ast_io_write8(ast, AST_IO_DAC_DATA, red);
	ast_io_read8(ast, AST_IO_SEQ_PORT);
	ast_io_write8(ast, AST_IO_DAC_DATA, green);
	ast_io_read8(ast, AST_IO_SEQ_PORT);
	ast_io_write8(ast, AST_IO_DAC_DATA, blue);
	ast_io_read8(ast, AST_IO_SEQ_PORT);
}

66
static void ast_crtc_load_lut(struct ast_private *ast, struct drm_crtc *crtc)
67
{
68
	u16 *r, *g, *b;
69 70 71 72 73
	int i;

	if (!crtc->enabled)
		return;

74 75 76 77
	r = crtc->gamma_store;
	g = r + crtc->gamma_size;
	b = g + crtc->gamma_size;

78
	for (i = 0; i < 256; i++)
79
		ast_load_palette_index(ast, i, *r++ >> 8, *g++ >> 8, *b++ >> 8);
80 81
}

82
static bool ast_get_vbios_mode_info(const struct drm_format_info *format,
83
				    const struct drm_display_mode *mode,
84 85 86
				    struct drm_display_mode *adjusted_mode,
				    struct ast_vbios_mode_info *vbios_mode)
{
87
	u32 refresh_rate_index = 0, refresh_rate;
88
	const struct ast_vbios_enhtable *best = NULL;
89
	u32 hborder, vborder;
90
	bool check_sync;
91

92
	switch (format->cpp[0] * 8) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106
	case 8:
		vbios_mode->std_table = &vbios_stdtable[VGAModeIndex];
		break;
	case 16:
		vbios_mode->std_table = &vbios_stdtable[HiCModeIndex];
		break;
	case 24:
	case 32:
		vbios_mode->std_table = &vbios_stdtable[TrueCModeIndex];
		break;
	default:
		return false;
	}

107
	switch (mode->crtc_hdisplay) {
108 109 110 111 112 113 114 115 116 117
	case 640:
		vbios_mode->enh_table = &res_640x480[refresh_rate_index];
		break;
	case 800:
		vbios_mode->enh_table = &res_800x600[refresh_rate_index];
		break;
	case 1024:
		vbios_mode->enh_table = &res_1024x768[refresh_rate_index];
		break;
	case 1280:
118
		if (mode->crtc_vdisplay == 800)
119 120 121 122
			vbios_mode->enh_table = &res_1280x800[refresh_rate_index];
		else
			vbios_mode->enh_table = &res_1280x1024[refresh_rate_index];
		break;
123 124 125
	case 1360:
		vbios_mode->enh_table = &res_1360x768[refresh_rate_index];
		break;
126 127 128 129
	case 1440:
		vbios_mode->enh_table = &res_1440x900[refresh_rate_index];
		break;
	case 1600:
130
		if (mode->crtc_vdisplay == 900)
131 132 133
			vbios_mode->enh_table = &res_1600x900[refresh_rate_index];
		else
			vbios_mode->enh_table = &res_1600x1200[refresh_rate_index];
134 135 136 137 138
		break;
	case 1680:
		vbios_mode->enh_table = &res_1680x1050[refresh_rate_index];
		break;
	case 1920:
139
		if (mode->crtc_vdisplay == 1080)
140 141 142 143 144 145 146 147 148
			vbios_mode->enh_table = &res_1920x1080[refresh_rate_index];
		else
			vbios_mode->enh_table = &res_1920x1200[refresh_rate_index];
		break;
	default:
		return false;
	}

	refresh_rate = drm_mode_vrefresh(mode);
149
	check_sync = vbios_mode->enh_table->flags & WideScreenMode;
150 151

	while (1) {
152
		const struct ast_vbios_enhtable *loop = vbios_mode->enh_table;
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

		while (loop->refresh_rate != 0xff) {
			if ((check_sync) &&
			    (((mode->flags & DRM_MODE_FLAG_NVSYNC)  &&
			      (loop->flags & PVSync))  ||
			     ((mode->flags & DRM_MODE_FLAG_PVSYNC)  &&
			      (loop->flags & NVSync))  ||
			     ((mode->flags & DRM_MODE_FLAG_NHSYNC)  &&
			      (loop->flags & PHSync))  ||
			     ((mode->flags & DRM_MODE_FLAG_PHSYNC)  &&
			      (loop->flags & NHSync)))) {
				loop++;
				continue;
			}
			if (loop->refresh_rate <= refresh_rate
			    && (!best || loop->refresh_rate > best->refresh_rate))
				best = loop;
			loop++;
171
		}
172 173 174
		if (best || !check_sync)
			break;
		check_sync = 0;
175 176
	}

177 178
	if (best)
		vbios_mode->enh_table = best;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

	hborder = (vbios_mode->enh_table->flags & HBorder) ? 8 : 0;
	vborder = (vbios_mode->enh_table->flags & VBorder) ? 8 : 0;

	adjusted_mode->crtc_htotal = vbios_mode->enh_table->ht;
	adjusted_mode->crtc_hblank_start = vbios_mode->enh_table->hde + hborder;
	adjusted_mode->crtc_hblank_end = vbios_mode->enh_table->ht - hborder;
	adjusted_mode->crtc_hsync_start = vbios_mode->enh_table->hde + hborder +
		vbios_mode->enh_table->hfp;
	adjusted_mode->crtc_hsync_end = (vbios_mode->enh_table->hde + hborder +
					 vbios_mode->enh_table->hfp +
					 vbios_mode->enh_table->hsync);

	adjusted_mode->crtc_vtotal = vbios_mode->enh_table->vt;
	adjusted_mode->crtc_vblank_start = vbios_mode->enh_table->vde + vborder;
	adjusted_mode->crtc_vblank_end = vbios_mode->enh_table->vt - vborder;
	adjusted_mode->crtc_vsync_start = vbios_mode->enh_table->vde + vborder +
		vbios_mode->enh_table->vfp;
	adjusted_mode->crtc_vsync_end = (vbios_mode->enh_table->vde + vborder +
					 vbios_mode->enh_table->vfp +
					 vbios_mode->enh_table->vsync);

201 202
	return true;
}
203

204 205
static void ast_set_vbios_color_reg(struct ast_private *ast,
				    const struct drm_format_info *format,
206 207 208 209
				    const struct ast_vbios_mode_info *vbios_mode)
{
	u32 color_index;

210
	switch (format->cpp[0]) {
211 212 213 214 215 216 217 218 219
	case 1:
		color_index = VGAModeIndex - 1;
		break;
	case 2:
		color_index = HiCModeIndex;
		break;
	case 3:
	case 4:
		color_index = TrueCModeIndex;
220
		break;
221 222
	default:
		return;
223 224
	}

225
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8c, (u8)((color_index & 0x0f) << 4));
226

227
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0x00);
228

229 230
	if (vbios_mode->enh_table->flags & NewModeInfo) {
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0xa8);
231
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x92, format->cpp[0] * 8);
232
	}
233
}
234

235
static void ast_set_vbios_mode_reg(struct ast_private *ast,
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
				   const struct drm_display_mode *adjusted_mode,
				   const struct ast_vbios_mode_info *vbios_mode)
{
	u32 refresh_rate_index, mode_id;

	refresh_rate_index = vbios_mode->enh_table->refresh_rate_index;
	mode_id = vbios_mode->enh_table->mode_id;

	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8d, refresh_rate_index & 0xff);
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8e, mode_id & 0xff);

	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0x00);

	if (vbios_mode->enh_table->flags & NewModeInfo) {
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0xa8);
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x93, adjusted_mode->clock / 1000);
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x94, adjusted_mode->crtc_hdisplay);
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x95, adjusted_mode->crtc_hdisplay >> 8);
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x96, adjusted_mode->crtc_vdisplay);
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x97, adjusted_mode->crtc_vdisplay >> 8);
	}
}

259 260
static void ast_set_std_reg(struct ast_private *ast,
			    struct drm_display_mode *mode,
261 262
			    struct ast_vbios_mode_info *vbios_mode)
{
263
	const struct ast_vbios_stdtable *stdtable;
264 265 266 267 268 269 270 271
	u32 i;
	u8 jreg;

	stdtable = vbios_mode->std_table;

	jreg = stdtable->misc;
	ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);

272
	/* Set SEQ; except Screen Disable field */
273
	ast_set_index_reg(ast, AST_IO_SEQ_PORT, 0x00, 0x03);
274 275
	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x01, 0xdf, stdtable->seq[0]);
	for (i = 1; i < 4; i++) {
276 277 278 279
		jreg = stdtable->seq[i];
		ast_set_index_reg(ast, AST_IO_SEQ_PORT, (i + 1) , jreg);
	}

280
	/* Set CRTC; except base address and offset */
281
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
282 283 284 285 286
	for (i = 0; i < 12; i++)
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
	for (i = 14; i < 19; i++)
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
	for (i = 20; i < 25; i++)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);

	/* set AR */
	jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
	for (i = 0; i < 20; i++) {
		jreg = stdtable->ar[i];
		ast_io_write8(ast, AST_IO_AR_PORT_WRITE, (u8)i);
		ast_io_write8(ast, AST_IO_AR_PORT_WRITE, jreg);
	}
	ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x14);
	ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x00);

	jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
	ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x20);

	/* Set GR */
	for (i = 0; i < 9; i++)
		ast_set_index_reg(ast, AST_IO_GR_PORT, i, stdtable->gr[i]);
}

307 308
static void ast_set_crtc_reg(struct ast_private *ast,
			     struct drm_display_mode *mode,
309 310 311
			     struct ast_vbios_mode_info *vbios_mode)
{
	u8 jreg05 = 0, jreg07 = 0, jreg09 = 0, jregAC = 0, jregAD = 0, jregAE = 0;
Y
Y.C. Chen 已提交
312 313 314 315 316
	u16 temp, precache = 0;

	if ((ast->chip == AST2500) &&
	    (vbios_mode->enh_table->flags & AST2500PreCatchCRT))
		precache = 40;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);

	temp = (mode->crtc_htotal >> 3) - 5;
	if (temp & 0x100)
		jregAC |= 0x01; /* HT D[8] */
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x00, 0x00, temp);

	temp = (mode->crtc_hdisplay >> 3) - 1;
	if (temp & 0x100)
		jregAC |= 0x04; /* HDE D[8] */
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x01, 0x00, temp);

	temp = (mode->crtc_hblank_start >> 3) - 1;
	if (temp & 0x100)
		jregAC |= 0x10; /* HBS D[8] */
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x02, 0x00, temp);

	temp = ((mode->crtc_hblank_end >> 3) - 1) & 0x7f;
	if (temp & 0x20)
		jreg05 |= 0x80;  /* HBE D[5] */
	if (temp & 0x40)
		jregAD |= 0x01;  /* HBE D[5] */
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x03, 0xE0, (temp & 0x1f));

Y
Y.C. Chen 已提交
342
	temp = ((mode->crtc_hsync_start-precache) >> 3) - 1;
343 344 345 346
	if (temp & 0x100)
		jregAC |= 0x40; /* HRS D[5] */
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x04, 0x00, temp);

Y
Y.C. Chen 已提交
347
	temp = (((mode->crtc_hsync_end-precache) >> 3) - 1) & 0x3f;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	if (temp & 0x20)
		jregAD |= 0x04; /* HRE D[5] */
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x05, 0x60, (u8)((temp & 0x1f) | jreg05));

	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAC, 0x00, jregAC);
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAD, 0x00, jregAD);

	/* vert timings */
	temp = (mode->crtc_vtotal) - 2;
	if (temp & 0x100)
		jreg07 |= 0x01;
	if (temp & 0x200)
		jreg07 |= 0x20;
	if (temp & 0x400)
		jregAE |= 0x01;
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x06, 0x00, temp);

	temp = (mode->crtc_vsync_start) - 1;
	if (temp & 0x100)
		jreg07 |= 0x04;
	if (temp & 0x200)
		jreg07 |= 0x80;
	if (temp & 0x400)
		jregAE |= 0x08;
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x10, 0x00, temp);

	temp = (mode->crtc_vsync_end - 1) & 0x3f;
	if (temp & 0x10)
		jregAE |= 0x20;
	if (temp & 0x20)
		jregAE |= 0x40;
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x70, temp & 0xf);

	temp = mode->crtc_vdisplay - 1;
	if (temp & 0x100)
		jreg07 |= 0x02;
	if (temp & 0x200)
		jreg07 |= 0x40;
	if (temp & 0x400)
		jregAE |= 0x02;
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x12, 0x00, temp);

	temp = mode->crtc_vblank_start - 1;
	if (temp & 0x100)
		jreg07 |= 0x08;
	if (temp & 0x200)
		jreg09 |= 0x20;
	if (temp & 0x400)
		jregAE |= 0x04;
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x15, 0x00, temp);

	temp = mode->crtc_vblank_end - 1;
	if (temp & 0x100)
		jregAE |= 0x10;
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x16, 0x00, temp);

	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x07, 0x00, jreg07);
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x09, 0xdf, jreg09);
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAE, 0x00, (jregAE | 0x80));

Y
Y.C. Chen 已提交
408 409 410 411 412
	if (precache)
		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0x3f, 0x80);
	else
		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb6, 0x3f, 0x00);

413 414 415
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x80);
}

416 417
static void ast_set_offset_reg(struct ast_private *ast,
			       struct drm_framebuffer *fb)
418 419 420
{
	u16 offset;

421
	offset = fb->pitches[0] >> 3;
422 423 424 425
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x13, (offset & 0xff));
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xb0, (offset >> 8) & 0x3f);
}

426 427
static void ast_set_dclk_reg(struct ast_private *ast,
			     struct drm_display_mode *mode,
428 429
			     struct ast_vbios_mode_info *vbios_mode)
{
430
	const struct ast_vbios_dclk_info *clk_info;
431

Y
Y.C. Chen 已提交
432 433 434 435
	if (ast->chip == AST2500)
		clk_info = &dclk_table_ast2500[vbios_mode->enh_table->dclk_index];
	else
		clk_info = &dclk_table[vbios_mode->enh_table->dclk_index];
436 437 438 439

	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc0, 0x00, clk_info->param1);
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc1, 0x00, clk_info->param2);
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xbb, 0x0f,
Y
Y.C. Chen 已提交
440 441
			       (clk_info->param3 & 0xc0) |
			       ((clk_info->param3 & 0x3) << 4));
442 443
}

444 445
static void ast_set_color_reg(struct ast_private *ast,
			      const struct drm_format_info *format)
446 447 448
{
	u8 jregA0 = 0, jregA3 = 0, jregA8 = 0;

449
	switch (format->cpp[0] * 8) {
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	case 8:
		jregA0 = 0x70;
		jregA3 = 0x01;
		jregA8 = 0x00;
		break;
	case 15:
	case 16:
		jregA0 = 0x70;
		jregA3 = 0x04;
		jregA8 = 0x02;
		break;
	case 32:
		jregA0 = 0x70;
		jregA3 = 0x08;
		jregA8 = 0x02;
		break;
	}

	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa0, 0x8f, jregA0);
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xf0, jregA3);
	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa8, 0xfd, jregA8);
471 472
}

473
static void ast_set_crtthd_reg(struct ast_private *ast)
474
{
475
	/* Set Threshold */
Y
Y.C. Chen 已提交
476 477
	if (ast->chip == AST2300 || ast->chip == AST2400 ||
	    ast->chip == AST2500) {
478 479 480 481 482 483 484 485 486 487 488 489 490 491
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x78);
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x60);
	} else if (ast->chip == AST2100 ||
		   ast->chip == AST1100 ||
		   ast->chip == AST2200 ||
		   ast->chip == AST2150) {
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x3f);
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x2f);
	} else {
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x2f);
		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x1f);
	}
}

492 493
static void ast_set_sync_reg(struct ast_private *ast,
			     struct drm_display_mode *mode,
494
			     struct ast_vbios_mode_info *vbios_mode)
495 496 497
{
	u8 jreg;

498 499 500 501
	jreg  = ast_io_read8(ast, AST_IO_MISC_PORT_READ);
	jreg &= ~0xC0;
	if (vbios_mode->enh_table->flags & NVSync) jreg |= 0x80;
	if (vbios_mode->enh_table->flags & NHSync) jreg |= 0x40;
502 503 504
	ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
}

505 506
static void ast_set_start_address_crt1(struct ast_private *ast,
				       unsigned offset)
507 508 509 510 511 512 513 514 515 516
{
	u32 addr;

	addr = offset >> 2;
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0d, (u8)(addr & 0xff));
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0c, (u8)((addr >> 8) & 0xff));
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xaf, (u8)((addr >> 16) & 0xff));

}

517 518 519 520 521 522 523 524 525 526
static void ast_wait_for_vretrace(struct ast_private *ast)
{
	unsigned long timeout = jiffies + HZ;
	u8 vgair1;

	do {
		vgair1 = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
	} while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout));
}

T
Thomas Zimmermann 已提交
527 528 529 530 531 532 533 534 535 536
/*
 * Primary plane
 */

static const uint32_t ast_primary_plane_formats[] = {
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_RGB565,
	DRM_FORMAT_C8,
};

537 538
static int ast_primary_plane_helper_atomic_check(struct drm_plane *plane,
						 struct drm_plane_state *state)
T
Thomas Zimmermann 已提交
539
{
540
	struct drm_crtc_state *crtc_state;
541
	struct ast_crtc_state *ast_crtc_state;
542 543 544 545 546 547 548 549 550 551 552 553 554 555
	int ret;

	if (!state->crtc)
		return 0;

	crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);

	ret = drm_atomic_helper_check_plane_state(state, crtc_state,
						  DRM_PLANE_HELPER_NO_SCALING,
						  DRM_PLANE_HELPER_NO_SCALING,
						  false, true);
	if (ret)
		return ret;

556 557 558 559 560 561 562
	if (!state->visible)
		return 0;

	ast_crtc_state = to_ast_crtc_state(crtc_state);

	ast_crtc_state->format = state->fb->format;

T
Thomas Zimmermann 已提交
563 564 565
	return 0;
}

566 567 568
static void
ast_primary_plane_helper_atomic_update(struct drm_plane *plane,
				       struct drm_plane_state *old_state)
T
Thomas Zimmermann 已提交
569
{
570 571
	struct drm_device *dev = plane->dev;
	struct ast_private *ast = to_ast_private(dev);
T
Thomas Zimmermann 已提交
572 573 574
	struct drm_plane_state *state = plane->state;
	struct drm_gem_vram_object *gbo;
	s64 gpu_addr;
575 576 577 578 579 580 581 582 583 584 585
	struct drm_framebuffer *fb = state->fb;
	struct drm_framebuffer *old_fb = old_state->fb;

	if (!old_fb || (fb->format != old_fb->format)) {
		struct drm_crtc_state *crtc_state = state->crtc->state;
		struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
		struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;

		ast_set_color_reg(ast, fb->format);
		ast_set_vbios_color_reg(ast, fb->format, vbios_mode_info);
	}
T
Thomas Zimmermann 已提交
586

587
	gbo = drm_gem_vram_of_gem(fb->obj[0]);
T
Thomas Zimmermann 已提交
588
	gpu_addr = drm_gem_vram_offset(gbo);
589
	if (drm_WARN_ON_ONCE(dev, gpu_addr < 0))
T
Thomas Zimmermann 已提交
590 591
		return; /* Bug: we didn't pin the BO to VRAM in prepare_fb. */

592
	ast_set_offset_reg(ast, fb);
593
	ast_set_start_address_crt1(ast, (u32)gpu_addr);
594 595 596 597 598 599 600 601

	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x00);
}

static void
ast_primary_plane_helper_atomic_disable(struct drm_plane *plane,
					struct drm_plane_state *old_state)
{
602
	struct ast_private *ast = to_ast_private(plane->dev);
603 604

	ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x20);
T
Thomas Zimmermann 已提交
605 606 607 608 609 610 611
}

static const struct drm_plane_helper_funcs ast_primary_plane_helper_funcs = {
	.prepare_fb = drm_gem_vram_plane_helper_prepare_fb,
	.cleanup_fb = drm_gem_vram_plane_helper_cleanup_fb,
	.atomic_check = ast_primary_plane_helper_atomic_check,
	.atomic_update = ast_primary_plane_helper_atomic_update,
612
	.atomic_disable = ast_primary_plane_helper_atomic_disable,
T
Thomas Zimmermann 已提交
613 614 615 616 617 618 619 620 621 622 623
};

static const struct drm_plane_funcs ast_primary_plane_funcs = {
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
	.destroy = drm_plane_cleanup,
	.reset = drm_atomic_helper_plane_reset,
	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
};

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
static int ast_primary_plane_init(struct ast_private *ast)
{
	struct drm_device *dev = &ast->base;
	struct drm_plane *primary_plane = &ast->primary_plane;
	int ret;

	ret = drm_universal_plane_init(dev, primary_plane, 0x01,
				       &ast_primary_plane_funcs,
				       ast_primary_plane_formats,
				       ARRAY_SIZE(ast_primary_plane_formats),
				       NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
	if (ret) {
		drm_err(dev, "drm_universal_plane_init() failed: %d\n", ret);
		return ret;
	}
	drm_plane_helper_add(primary_plane, &ast_primary_plane_helper_funcs);

	return 0;
}

T
Thomas Zimmermann 已提交
644 645 646 647
/*
 * Cursor plane
 */

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
static void ast_update_cursor_image(u8 __iomem *dst, const u8 *src, int width, int height)
{
	union {
		u32 ul;
		u8 b[4];
	} srcdata32[2], data32;
	union {
		u16 us;
		u8 b[2];
	} data16;
	u32 csum = 0;
	s32 alpha_dst_delta, last_alpha_dst_delta;
	u8 __iomem *dstxor;
	const u8 *srcxor;
	int i, j;
	u32 per_pixel_copy, two_pixel_copy;

	alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
	last_alpha_dst_delta = alpha_dst_delta - (width << 1);

	srcxor = src;
	dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
	per_pixel_copy = width & 1;
	two_pixel_copy = width >> 1;

	for (j = 0; j < height; j++) {
		for (i = 0; i < two_pixel_copy; i++) {
			srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
			srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
			data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
			data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
			data32.b[2] = srcdata32[1].b[1] | (srcdata32[1].b[0] >> 4);
			data32.b[3] = srcdata32[1].b[3] | (srcdata32[1].b[2] >> 4);

			writel(data32.ul, dstxor);
			csum += data32.ul;

			dstxor += 4;
			srcxor += 8;

		}

		for (i = 0; i < per_pixel_copy; i++) {
			srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
			data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
			data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
			writew(data16.us, dstxor);
			csum += (u32)data16.us;

			dstxor += 2;
			srcxor += 4;
		}
		dstxor += last_alpha_dst_delta;
	}

	/* write checksum + signature */
	dst += AST_HWC_SIZE;
	writel(csum, dst);
	writel(width, dst + AST_HWC_SIGNATURE_SizeX);
	writel(height, dst + AST_HWC_SIGNATURE_SizeY);
	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
}

static void ast_set_cursor_base(struct ast_private *ast, u64 address)
{
	u8 addr0 = (address >> 3) & 0xff;
	u8 addr1 = (address >> 11) & 0xff;
	u8 addr2 = (address >> 19) & 0xff;

	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc8, addr0);
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc9, addr1);
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xca, addr2);
}

static void ast_set_cursor_location(struct ast_private *ast, u16 x, u16 y,
				    u8 x_offset, u8 y_offset)
{
	u8 x0 = (x & 0x00ff);
	u8 x1 = (x & 0x0f00) >> 8;
	u8 y0 = (y & 0x00ff);
	u8 y1 = (y & 0x0700) >> 8;

	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc2, x_offset);
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc3, y_offset);
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc4, x0);
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc5, x1);
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc6, y0);
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc7, y1);
}

static void ast_set_cursor_enabled(struct ast_private *ast, bool enabled)
{
	static const u8 mask = (u8)~(AST_IO_VGACRCB_HWC_16BPP |
				     AST_IO_VGACRCB_HWC_ENABLED);

	u8 vgacrcb = AST_IO_VGACRCB_HWC_16BPP;

	if (enabled)
		vgacrcb |= AST_IO_VGACRCB_HWC_ENABLED;

	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, mask, vgacrcb);
}

T
Thomas Zimmermann 已提交
752 753 754 755 756 757 758 759 760
static const uint32_t ast_cursor_plane_formats[] = {
	DRM_FORMAT_ARGB8888,
};

static int
ast_cursor_plane_helper_prepare_fb(struct drm_plane *plane,
				   struct drm_plane_state *new_state)
{
	struct drm_framebuffer *fb = new_state->fb;
761 762 763 764 765 766
	struct ast_private *ast = to_ast_private(plane->dev);
	struct drm_gem_vram_object *dst_gbo = ast->cursor.gbo[ast->cursor.next_index];
	struct drm_gem_vram_object *src_gbo;
	struct dma_buf_map src_map, dst_map;
	void __iomem *dst;
	void *src;
T
Thomas Zimmermann 已提交
767 768
	int ret;

769
	if (!fb)
T
Thomas Zimmermann 已提交
770 771
		return 0;

772
	src_gbo = drm_gem_vram_of_gem(fb->obj[0]);
T
Thomas Zimmermann 已提交
773

774
	ret = drm_gem_vram_vmap(src_gbo, &src_map);
T
Thomas Zimmermann 已提交
775
	if (ret)
776
		return ret;
777 778 779 780 781 782 783 784 785 786 787 788
	src = src_map.vaddr; /* TODO: Use mapping abstraction properly */

	ret = drm_gem_vram_vmap(dst_gbo, &dst_map);
	if (ret)
		goto err_drm_gem_vram_vunmap;
	dst = dst_map.vaddr_iomem; /* TODO: Use mapping abstraction properly */

	/* do data transfer to cursor BO */
	ast_update_cursor_image(dst, src, fb->width, fb->height);

	drm_gem_vram_vunmap(dst_gbo, &dst_map);
	drm_gem_vram_vunmap(src_gbo, &src_map);
T
Thomas Zimmermann 已提交
789 790

	return 0;
791 792 793 794

err_drm_gem_vram_vunmap:
	drm_gem_vram_vunmap(src_gbo, &src_map);
	return ret;
T
Thomas Zimmermann 已提交
795 796 797 798 799
}

static int ast_cursor_plane_helper_atomic_check(struct drm_plane *plane,
						struct drm_plane_state *state)
{
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
	struct drm_framebuffer *fb = state->fb;
	struct drm_crtc_state *crtc_state;
	int ret;

	if (!state->crtc)
		return 0;

	crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);

	ret = drm_atomic_helper_check_plane_state(state, crtc_state,
						  DRM_PLANE_HELPER_NO_SCALING,
						  DRM_PLANE_HELPER_NO_SCALING,
						  true, true);
	if (ret)
		return ret;

	if (!state->visible)
		return 0;

	if (fb->width > AST_MAX_HWC_WIDTH || fb->height > AST_MAX_HWC_HEIGHT)
		return -EINVAL;

T
Thomas Zimmermann 已提交
822 823 824 825 826 827 828 829 830
	return 0;
}

static void
ast_cursor_plane_helper_atomic_update(struct drm_plane *plane,
				      struct drm_plane_state *old_state)
{
	struct drm_plane_state *state = plane->state;
	struct drm_framebuffer *fb = state->fb;
831
	struct ast_private *ast = to_ast_private(plane->dev);
832 833
	struct drm_device *dev = &ast->base;
	struct drm_gem_vram_object *gbo = ast->cursor.gbo[ast->cursor.next_index];
834
	unsigned int offset_x, offset_y;
835 836 837 838 839 840 841
	s64 off;
	struct dma_buf_map map;
	u16 x, y;
	u8 x_offset, y_offset;
	u8 __iomem *dst;
	u8 __iomem *sig;
	int ret;
T
Thomas Zimmermann 已提交
842

843
	gbo = ast->cursor.gbo[ast->cursor.next_index];
T
Thomas Zimmermann 已提交
844 845 846

	if (state->fb != old_state->fb) {
		/* A new cursor image was installed. */
847 848 849 850 851 852 853
		off = drm_gem_vram_offset(gbo);
		if (drm_WARN_ON_ONCE(dev, off < 0))
			return; /* Bug: we didn't pin the cursor HW BO to VRAM. */
		ast_set_cursor_base(ast, off);

		++ast->cursor.next_index;
		ast->cursor.next_index %= ARRAY_SIZE(ast->cursor.gbo);
T
Thomas Zimmermann 已提交
854 855
	}

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	ret = drm_gem_vram_vmap(gbo, &map);
	if (drm_WARN_ONCE(dev, ret, "drm_gem_vram_vmap() failed, ret=%d\n", ret))
		return;
	dst = map.vaddr_iomem; /* TODO: Use mapping abstraction properly */

	sig = dst + AST_HWC_SIZE;
	writel(state->crtc_x, sig + AST_HWC_SIGNATURE_X);
	writel(state->crtc_y, sig + AST_HWC_SIGNATURE_Y);

	drm_gem_vram_vunmap(gbo, &map);

	offset_x = AST_MAX_HWC_WIDTH - fb->width;
	offset_y = AST_MAX_HWC_HEIGHT - fb->height;

	if (state->crtc_x < 0) {
		x_offset = (-state->crtc_x) + offset_x;
		x = 0;
	} else {
		x_offset = offset_x;
		x = state->crtc_x;
	}
	if (state->crtc_y < 0) {
		y_offset = (-state->crtc_y) + offset_y;
		y = 0;
	} else {
		y_offset = offset_y;
		y = state->crtc_y;
	}

	ast_set_cursor_location(ast, x, y, x_offset, y_offset);

	/* dummy write to fire HWC */
	ast_set_cursor_enabled(ast, true);
T
Thomas Zimmermann 已提交
889 890 891 892 893 894
}

static void
ast_cursor_plane_helper_atomic_disable(struct drm_plane *plane,
				       struct drm_plane_state *old_state)
{
895
	struct ast_private *ast = to_ast_private(plane->dev);
T
Thomas Zimmermann 已提交
896

897
	ast_set_cursor_enabled(ast, false);
T
Thomas Zimmermann 已提交
898 899 900 901 902 903 904 905 906 907
}

static const struct drm_plane_helper_funcs ast_cursor_plane_helper_funcs = {
	.prepare_fb = ast_cursor_plane_helper_prepare_fb,
	.cleanup_fb = NULL, /* not required for cursor plane */
	.atomic_check = ast_cursor_plane_helper_atomic_check,
	.atomic_update = ast_cursor_plane_helper_atomic_update,
	.atomic_disable = ast_cursor_plane_helper_atomic_disable,
};

908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
static void ast_cursor_plane_destroy(struct drm_plane *plane)
{
	struct ast_private *ast = to_ast_private(plane->dev);
	size_t i;
	struct drm_gem_vram_object *gbo;

	for (i = 0; i < ARRAY_SIZE(ast->cursor.gbo); ++i) {
		gbo = ast->cursor.gbo[i];
		drm_gem_vram_unpin(gbo);
		drm_gem_vram_put(gbo);
	}

	drm_plane_cleanup(plane);
}

T
Thomas Zimmermann 已提交
923 924 925
static const struct drm_plane_funcs ast_cursor_plane_funcs = {
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
926
	.destroy = ast_cursor_plane_destroy,
T
Thomas Zimmermann 已提交
927 928 929 930 931
	.reset = drm_atomic_helper_plane_reset,
	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
};

932 933 934 935
static int ast_cursor_plane_init(struct ast_private *ast)
{
	struct drm_device *dev = &ast->base;
	struct drm_plane *cursor_plane = &ast->cursor_plane;
936 937
	size_t size, i;
	struct drm_gem_vram_object *gbo;
938 939
	int ret;

940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
	/*
	 * Allocate backing storage for cursors. The BOs are permanently
	 * pinned to the top end of the VRAM.
	 */

	size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);

	for (i = 0; i < ARRAY_SIZE(ast->cursor.gbo); ++i) {
		gbo = drm_gem_vram_create(dev, size, 0);
		if (IS_ERR(gbo)) {
			ret = PTR_ERR(gbo);
			goto err_hwc;
		}
		ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM |
					    DRM_GEM_VRAM_PL_FLAG_TOPDOWN);
		if (ret)
			goto err_drm_gem_vram_put;
		ast->cursor.gbo[i] = gbo;
	}

	/*
	 * Create the cursor plane. The plane's destroy callback will release
	 * the backing storages' BO memory.
	 */

965 966 967 968 969 970
	ret = drm_universal_plane_init(dev, cursor_plane, 0x01,
				       &ast_cursor_plane_funcs,
				       ast_cursor_plane_formats,
				       ARRAY_SIZE(ast_cursor_plane_formats),
				       NULL, DRM_PLANE_TYPE_CURSOR, NULL);
	if (ret) {
971 972
		drm_err(dev, "drm_universal_plane failed(): %d\n", ret);
		goto err_hwc;
973 974 975 976
	}
	drm_plane_helper_add(cursor_plane, &ast_cursor_plane_helper_funcs);

	return 0;
977 978 979 980 981 982 983 984 985 986

err_hwc:
	while (i) {
		--i;
		gbo = ast->cursor.gbo[i];
		drm_gem_vram_unpin(gbo);
err_drm_gem_vram_put:
		drm_gem_vram_put(gbo);
	}
	return ret;
987 988
}

T
Thomas Zimmermann 已提交
989 990 991 992
/*
 * CRTC
 */

993 994
static void ast_crtc_dpms(struct drm_crtc *crtc, int mode)
{
995
	struct ast_private *ast = to_ast_private(crtc->dev);
996

997 998 999
	/* TODO: Maybe control display signal generation with
	 *       Sync Enable (bit CR17.7).
	 */
1000 1001 1002 1003
	switch (mode) {
	case DRM_MODE_DPMS_ON:
	case DRM_MODE_DPMS_STANDBY:
	case DRM_MODE_DPMS_SUSPEND:
1004 1005
		if (ast->tx_chip_type == AST_TX_DP501)
			ast_set_dp501_video_output(crtc->dev, 1);
1006 1007
		break;
	case DRM_MODE_DPMS_OFF:
1008 1009
		if (ast->tx_chip_type == AST_TX_DP501)
			ast_set_dp501_video_output(crtc->dev, 0);
1010 1011 1012 1013
		break;
	}
}

1014
static int ast_crtc_helper_atomic_check(struct drm_crtc *crtc,
1015
					struct drm_atomic_state *state)
1016
{
1017 1018
	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
									  crtc);
1019
	struct drm_device *dev = crtc->dev;
1020
	struct ast_crtc_state *ast_state;
1021
	const struct drm_format_info *format;
1022 1023
	bool succ;

1024
	if (!crtc_state->enable)
1025 1026
		return 0; /* no mode checks if CRTC is being disabled */

1027
	ast_state = to_ast_crtc_state(crtc_state);
1028

1029
	format = ast_state->format;
1030 1031
	if (drm_WARN_ON_ONCE(dev, !format))
		return -EINVAL; /* BUG: We didn't set format in primary check(). */
1032

1033 1034
	succ = ast_get_vbios_mode_info(format, &crtc_state->mode,
				       &crtc_state->adjusted_mode,
1035 1036 1037
				       &ast_state->vbios_mode_info);
	if (!succ)
		return -EINVAL;
1038 1039 1040 1041

	return 0;
}

1042
static void
1043 1044
ast_crtc_helper_atomic_flush(struct drm_crtc *crtc,
			     struct drm_atomic_state *state)
1045
{
1046 1047
	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
									  crtc);
1048 1049
	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state,
									      crtc);
1050
	struct ast_private *ast = to_ast_private(crtc->dev);
1051
	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	struct ast_crtc_state *old_ast_crtc_state = to_ast_crtc_state(old_crtc_state);

	/*
	 * The gamma LUT has to be reloaded after changing the primary
	 * plane's color format.
	 */
	if (old_ast_crtc_state->format != ast_crtc_state->format)
		ast_crtc_load_lut(ast, crtc);
}

1062 1063
static void
ast_crtc_helper_atomic_enable(struct drm_crtc *crtc,
1064
			      struct drm_atomic_state *state)
1065
{
1066
	struct drm_device *dev = crtc->dev;
1067
	struct ast_private *ast = to_ast_private(dev);
1068 1069 1070 1071 1072
	struct drm_crtc_state *crtc_state = crtc->state;
	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
	struct ast_vbios_mode_info *vbios_mode_info =
		&ast_crtc_state->vbios_mode_info;
	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
1073

1074
	ast_set_vbios_mode_reg(ast, adjusted_mode, vbios_mode_info);
1075
	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa1, 0x06);
1076 1077 1078
	ast_set_std_reg(ast, adjusted_mode, vbios_mode_info);
	ast_set_crtc_reg(ast, adjusted_mode, vbios_mode_info);
	ast_set_dclk_reg(ast, adjusted_mode, vbios_mode_info);
1079
	ast_set_crtthd_reg(ast);
1080
	ast_set_sync_reg(ast, adjusted_mode, vbios_mode_info);
1081 1082 1083 1084 1085 1086

	ast_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
}

static void
ast_crtc_helper_atomic_disable(struct drm_crtc *crtc,
1087
			       struct drm_atomic_state *state)
1088
{
1089 1090
	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state,
									      crtc);
1091 1092 1093
	struct drm_device *dev = crtc->dev;
	struct ast_private *ast = to_ast_private(dev);

1094
	ast_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

	/*
	 * HW cursors require the underlying primary plane and CRTC to
	 * display a valid mode and image. This is not the case during
	 * full modeset operations. So we temporarily disable any active
	 * plane, including the HW cursor. Each plane's atomic_update()
	 * helper will re-enable it if necessary.
	 *
	 * We only do this during *full* modesets. It does not affect
	 * simple pageflips on the planes.
	 */
	drm_atomic_helper_disable_planes_on_crtc(old_crtc_state, false);

	/*
	 * Ensure that no scanout takes place before reprogramming mode
	 * and format registers.
	 */
	ast_wait_for_vretrace(ast);
1113
}
1114 1115

static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
1116
	.atomic_check = ast_crtc_helper_atomic_check,
1117
	.atomic_flush = ast_crtc_helper_atomic_flush,
1118 1119
	.atomic_enable = ast_crtc_helper_atomic_enable,
	.atomic_disable = ast_crtc_helper_atomic_disable,
1120 1121
};

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
static void ast_crtc_reset(struct drm_crtc *crtc)
{
	struct ast_crtc_state *ast_state =
		kzalloc(sizeof(*ast_state), GFP_KERNEL);

	if (crtc->state)
		crtc->funcs->atomic_destroy_state(crtc, crtc->state);

	__drm_atomic_helper_crtc_reset(crtc, &ast_state->base);
}

1133 1134 1135
static struct drm_crtc_state *
ast_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
{
1136
	struct ast_crtc_state *new_ast_state, *ast_state;
1137
	struct drm_device *dev = crtc->dev;
1138

1139
	if (drm_WARN_ON(dev, !crtc->state))
1140 1141 1142 1143 1144 1145 1146
		return NULL;

	new_ast_state = kmalloc(sizeof(*new_ast_state), GFP_KERNEL);
	if (!new_ast_state)
		return NULL;
	__drm_atomic_helper_crtc_duplicate_state(crtc, &new_ast_state->base);

1147 1148
	ast_state = to_ast_crtc_state(crtc->state);

1149
	new_ast_state->format = ast_state->format;
1150 1151 1152
	memcpy(&new_ast_state->vbios_mode_info, &ast_state->vbios_mode_info,
	       sizeof(new_ast_state->vbios_mode_info));

1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
	return &new_ast_state->base;
}

static void ast_crtc_atomic_destroy_state(struct drm_crtc *crtc,
					  struct drm_crtc_state *state)
{
	struct ast_crtc_state *ast_state = to_ast_crtc_state(state);

	__drm_atomic_helper_crtc_destroy_state(&ast_state->base);
	kfree(ast_state);
}

1165
static const struct drm_crtc_funcs ast_crtc_funcs = {
1166
	.reset = ast_crtc_reset,
1167
	.destroy = drm_crtc_cleanup,
1168 1169
	.set_config = drm_atomic_helper_set_config,
	.page_flip = drm_atomic_helper_page_flip,
1170 1171
	.atomic_duplicate_state = ast_crtc_atomic_duplicate_state,
	.atomic_destroy_state = ast_crtc_atomic_destroy_state,
1172 1173
};

1174
static int ast_crtc_init(struct drm_device *dev)
1175
{
1176
	struct ast_private *ast = to_ast_private(dev);
1177
	struct drm_crtc *crtc = &ast->crtc;
T
Thomas Zimmermann 已提交
1178
	int ret;
1179

1180
	ret = drm_crtc_init_with_planes(dev, crtc, &ast->primary_plane,
T
Thomas Zimmermann 已提交
1181 1182
					&ast->cursor_plane, &ast_crtc_funcs,
					NULL);
T
Thomas Zimmermann 已提交
1183
	if (ret)
1184
		return ret;
T
Thomas Zimmermann 已提交
1185

1186 1187 1188
	drm_mode_crtc_set_gamma_size(crtc, 256);
	drm_crtc_helper_add(crtc, &ast_crtc_helper_funcs);

1189 1190 1191
	return 0;
}

1192 1193 1194 1195
/*
 * Encoder
 */

1196
static int ast_encoder_init(struct drm_device *dev)
1197
{
1198
	struct ast_private *ast = to_ast_private(dev);
T
Thomas Zimmermann 已提交
1199 1200
	struct drm_encoder *encoder = &ast->encoder;
	int ret;
1201

T
Thomas Zimmermann 已提交
1202 1203 1204
	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_DAC);
	if (ret)
		return ret;
1205

T
Thomas Zimmermann 已提交
1206
	encoder->possible_crtcs = 1;
1207 1208 1209 1210

	return 0;
}

1211 1212 1213 1214
/*
 * Connector
 */

1215 1216 1217
static int ast_get_modes(struct drm_connector *connector)
{
	struct ast_connector *ast_connector = to_ast_connector(connector);
1218
	struct ast_private *ast = to_ast_private(connector->dev);
1219 1220
	struct edid *edid;
	int ret;
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	bool flags = false;
	if (ast->tx_chip_type == AST_TX_DP501) {
		ast->dp501_maxclk = 0xff;
		edid = kmalloc(128, GFP_KERNEL);
		if (!edid)
			return -ENOMEM;

		flags = ast_dp501_read_edid(connector->dev, (u8 *)edid);
		if (flags)
			ast->dp501_maxclk = ast_get_dp501_max_clk(connector->dev);
		else
			kfree(edid);
	}
	if (!flags)
		edid = drm_get_edid(connector, &ast_connector->i2c->adapter);
1236
	if (edid) {
1237
		drm_connector_update_edid_property(&ast_connector->base, edid);
1238
		ret = drm_add_edid_modes(connector, edid);
J
Jani Nikula 已提交
1239
		kfree(edid);
1240 1241
		return ret;
	} else
1242
		drm_connector_update_edid_property(&ast_connector->base, NULL);
1243 1244 1245
	return 0;
}

1246
static enum drm_mode_status ast_mode_valid(struct drm_connector *connector,
1247 1248
			  struct drm_display_mode *mode)
{
1249
	struct ast_private *ast = to_ast_private(connector->dev);
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
	int flags = MODE_NOMODE;
	uint32_t jtemp;

	if (ast->support_wide_screen) {
		if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
			return MODE_OK;
		if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
			return MODE_OK;
		if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
			return MODE_OK;
		if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
			return MODE_OK;
		if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
			return MODE_OK;

Y
Y.C. Chen 已提交
1265 1266
		if ((ast->chip == AST2100) || (ast->chip == AST2200) ||
		    (ast->chip == AST2300) || (ast->chip == AST2400) ||
1267
		    (ast->chip == AST2500)) {
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
			if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
				return MODE_OK;

			if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
				jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
				if (jtemp & 0x01)
					return MODE_NOMODE;
				else
					return MODE_OK;
			}
		}
	}
	switch (mode->hdisplay) {
	case 640:
		if (mode->vdisplay == 480) flags = MODE_OK;
		break;
	case 800:
		if (mode->vdisplay == 600) flags = MODE_OK;
		break;
	case 1024:
		if (mode->vdisplay == 768) flags = MODE_OK;
		break;
	case 1280:
		if (mode->vdisplay == 1024) flags = MODE_OK;
		break;
	case 1600:
		if (mode->vdisplay == 1200) flags = MODE_OK;
		break;
	default:
		return flags;
	}

	return flags;
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
}

static void ast_connector_destroy(struct drm_connector *connector)
{
	struct ast_connector *ast_connector = to_ast_connector(connector);
	ast_i2c_destroy(ast_connector->i2c);
	drm_connector_cleanup(connector);
}

static const struct drm_connector_helper_funcs ast_connector_helper_funcs = {
	.get_modes = ast_get_modes,
1312
	.mode_valid = ast_mode_valid,
1313 1314 1315
};

static const struct drm_connector_funcs ast_connector_funcs = {
1316
	.reset = drm_atomic_helper_connector_reset,
1317 1318
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = ast_connector_destroy,
1319 1320
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1321 1322
};

1323
static int ast_connector_init(struct drm_device *dev)
1324
{
1325 1326 1327 1328
	struct ast_private *ast = to_ast_private(dev);
	struct ast_connector *ast_connector = &ast->connector;
	struct drm_connector *connector = &ast_connector->base;
	struct drm_encoder *encoder = &ast->encoder;
1329

1330 1331
	ast_connector->i2c = ast_i2c_create(dev);
	if (!ast_connector->i2c)
1332
		drm_err(dev, "failed to add ddc bus for connector\n");
1333 1334 1335 1336 1337

	drm_connector_init_with_ddc(dev, connector,
				    &ast_connector_funcs,
				    DRM_MODE_CONNECTOR_VGA,
				    &ast_connector->i2c->adapter);
1338 1339 1340 1341 1342 1343 1344 1345

	drm_connector_helper_add(connector, &ast_connector_helper_funcs);

	connector->interlace_allowed = 0;
	connector->doublescan_allowed = 0;

	connector->polled = DRM_CONNECTOR_POLL_CONNECT;

1346
	drm_connector_attach_encoder(connector, encoder);
1347 1348 1349 1350

	return 0;
}

1351 1352 1353 1354
/*
 * Mode config
 */

1355 1356 1357 1358 1359
static const struct drm_mode_config_helper_funcs
ast_mode_config_helper_funcs = {
	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
};

1360 1361 1362 1363 1364 1365 1366 1367
static const struct drm_mode_config_funcs ast_mode_config_funcs = {
	.fb_create = drm_gem_fb_create,
	.mode_valid = drm_vram_helper_mode_valid,
	.atomic_check = drm_atomic_helper_check,
	.atomic_commit = drm_atomic_helper_commit,
};

int ast_mode_config_init(struct ast_private *ast)
1368
{
1369
	struct drm_device *dev = &ast->base;
1370
	struct pci_dev *pdev = to_pci_dev(dev->dev);
T
Thomas Zimmermann 已提交
1371 1372
	int ret;

1373 1374 1375 1376 1377 1378 1379 1380 1381
	ret = drmm_mode_config_init(dev);
	if (ret)
		return ret;

	dev->mode_config.funcs = &ast_mode_config_funcs;
	dev->mode_config.min_width = 0;
	dev->mode_config.min_height = 0;
	dev->mode_config.preferred_depth = 24;
	dev->mode_config.prefer_shadow = 1;
1382
	dev->mode_config.fb_base = pci_resource_start(pdev, 0);
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395

	if (ast->chip == AST2100 ||
	    ast->chip == AST2200 ||
	    ast->chip == AST2300 ||
	    ast->chip == AST2400 ||
	    ast->chip == AST2500) {
		dev->mode_config.max_width = 1920;
		dev->mode_config.max_height = 2048;
	} else {
		dev->mode_config.max_width = 1600;
		dev->mode_config.max_height = 1200;
	}

1396 1397
	dev->mode_config.helper_private = &ast_mode_config_helper_funcs;

1398 1399 1400

	ret = ast_primary_plane_init(ast);
	if (ret)
T
Thomas Zimmermann 已提交
1401 1402
		return ret;

1403 1404
	ret = ast_cursor_plane_init(ast);
	if (ret)
T
Thomas Zimmermann 已提交
1405 1406
		return ret;

1407 1408 1409
	ast_crtc_init(dev);
	ast_encoder_init(dev);
	ast_connector_init(dev);
T
Thomas Zimmermann 已提交
1410

1411 1412
	drm_mode_config_reset(dev);

1413 1414 1415 1416 1417 1418
	return 0;
}

static int get_clock(void *i2c_priv)
{
	struct ast_i2c_chan *i2c = i2c_priv;
1419
	struct ast_private *ast = to_ast_private(i2c->dev);
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
	uint32_t val, val2, count, pass;

	count = 0;
	pass = 0;
	val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
	do {
		val2 = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
		if (val == val2) {
			pass++;
		} else {
			pass = 0;
			val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
		}
	} while ((pass < 5) && (count++ < 0x10000));
1434 1435 1436 1437 1438 1439 1440

	return val & 1 ? 1 : 0;
}

static int get_data(void *i2c_priv)
{
	struct ast_i2c_chan *i2c = i2c_priv;
1441
	struct ast_private *ast = to_ast_private(i2c->dev);
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
	uint32_t val, val2, count, pass;

	count = 0;
	pass = 0;
	val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
	do {
		val2 = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
		if (val == val2) {
			pass++;
		} else {
			pass = 0;
			val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
		}
	} while ((pass < 5) && (count++ < 0x10000));
1456 1457 1458 1459 1460 1461 1462

	return val & 1 ? 1 : 0;
}

static void set_clock(void *i2c_priv, int clock)
{
	struct ast_i2c_chan *i2c = i2c_priv;
1463
	struct ast_private *ast = to_ast_private(i2c->dev);
1464 1465 1466 1467 1468
	int i;
	u8 ujcrb7, jtemp;

	for (i = 0; i < 0x10000; i++) {
		ujcrb7 = ((clock & 0x01) ? 0 : 1);
1469
		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xf4, ujcrb7);
1470 1471 1472 1473 1474 1475 1476 1477 1478
		jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x01);
		if (ujcrb7 == jtemp)
			break;
	}
}

static void set_data(void *i2c_priv, int data)
{
	struct ast_i2c_chan *i2c = i2c_priv;
1479
	struct ast_private *ast = to_ast_private(i2c->dev);
1480 1481 1482 1483 1484
	int i;
	u8 ujcrb7, jtemp;

	for (i = 0; i < 0x10000; i++) {
		ujcrb7 = ((data & 0x01) ? 0 : 1) << 2;
1485
		ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xf1, ujcrb7);
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
		jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x04);
		if (ujcrb7 == jtemp)
			break;
	}
}

static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev)
{
	struct ast_i2c_chan *i2c;
	int ret;

	i2c = kzalloc(sizeof(struct ast_i2c_chan), GFP_KERNEL);
	if (!i2c)
		return NULL;

	i2c->adapter.owner = THIS_MODULE;
	i2c->adapter.class = I2C_CLASS_DDC;
1503
	i2c->adapter.dev.parent = dev->dev;
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
	i2c->dev = dev;
	i2c_set_adapdata(&i2c->adapter, i2c);
	snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
		 "AST i2c bit bus");
	i2c->adapter.algo_data = &i2c->bit;

	i2c->bit.udelay = 20;
	i2c->bit.timeout = 2;
	i2c->bit.data = i2c;
	i2c->bit.setsda = set_data;
	i2c->bit.setscl = set_clock;
	i2c->bit.getsda = get_data;
	i2c->bit.getscl = get_clock;
	ret = i2c_bit_add_bus(&i2c->adapter);
	if (ret) {
1519
		drm_err(dev, "Failed to register bit i2c\n");
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
		goto out_free;
	}

	return i2c;
out_free:
	kfree(i2c);
	return NULL;
}

static void ast_i2c_destroy(struct ast_i2c_chan *i2c)
{
	if (!i2c)
		return;
	i2c_del_adapter(&i2c->adapter);
	kfree(i2c);
}