intel_hdmi.c 35.2 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 2006 Dave Airlie <airlied@linux.ie>
 * Copyright © 2006-2009 Intel Corporation
 *
 * 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, sublicense,
 * 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 above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 * Authors:
 *	Eric Anholt <eric@anholt.net>
 *	Jesse Barnes <jesse.barnes@intel.com>
 */

#include <linux/i2c.h>
30
#include <linux/slab.h>
31
#include <linux/delay.h>
32 33 34
#include <drm/drmP.h>
#include <drm/drm_crtc.h>
#include <drm/drm_edid.h>
35
#include "intel_drv.h"
36
#include <drm/i915_drm.h>
37 38
#include "i915_drv.h"

39 40
static struct drm_device *intel_hdmi_to_dev(struct intel_hdmi *intel_hdmi)
{
41
	return hdmi_to_dig_port(intel_hdmi)->base.base.dev;
42 43
}

44 45 46
static void
assert_hdmi_port_disabled(struct intel_hdmi *intel_hdmi)
{
47
	struct drm_device *dev = intel_hdmi_to_dev(intel_hdmi);
48 49 50
	struct drm_i915_private *dev_priv = dev->dev_private;
	uint32_t enabled_bits;

P
Paulo Zanoni 已提交
51
	enabled_bits = HAS_DDI(dev) ? DDI_BUF_CTL_ENABLE : SDVO_ENABLE;
52

53
	WARN(I915_READ(intel_hdmi->hdmi_reg) & enabled_bits,
54 55 56
	     "HDMI port enabled, expecting disabled\n");
}

57
struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
C
Chris Wilson 已提交
58
{
59 60 61
	struct intel_digital_port *intel_dig_port =
		container_of(encoder, struct intel_digital_port, base.base);
	return &intel_dig_port->hdmi;
C
Chris Wilson 已提交
62 63
}

64 65
static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
{
66
	return enc_to_intel_hdmi(&intel_attached_encoder(connector)->base);
67 68
}

69
void intel_dip_infoframe_csum(struct dip_infoframe *frame)
70
{
71
	uint8_t *data = (uint8_t *)frame;
72 73 74
	uint8_t sum = 0;
	unsigned i;

75 76
	frame->checksum = 0;
	frame->ecc = 0;
77

78
	for (i = 0; i < frame->len + DIP_HEADER_SIZE; i++)
79 80
		sum += data[i];

81
	frame->checksum = 0x100 - sum;
82 83
}

84
static u32 g4x_infoframe_index(struct dip_infoframe *frame)
85
{
86 87
	switch (frame->type) {
	case DIP_TYPE_AVI:
88
		return VIDEO_DIP_SELECT_AVI;
89
	case DIP_TYPE_SPD:
90
		return VIDEO_DIP_SELECT_SPD;
91 92
	default:
		DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
93
		return 0;
94 95 96
	}
}

97
static u32 g4x_infoframe_enable(struct dip_infoframe *frame)
98 99 100
{
	switch (frame->type) {
	case DIP_TYPE_AVI:
101
		return VIDEO_DIP_ENABLE_AVI;
102
	case DIP_TYPE_SPD:
103
		return VIDEO_DIP_ENABLE_SPD;
104 105
	default:
		DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
106
		return 0;
107 108 109
	}
}

110 111 112 113 114 115 116 117 118 119 120 121 122
static u32 hsw_infoframe_enable(struct dip_infoframe *frame)
{
	switch (frame->type) {
	case DIP_TYPE_AVI:
		return VIDEO_DIP_ENABLE_AVI_HSW;
	case DIP_TYPE_SPD:
		return VIDEO_DIP_ENABLE_SPD_HSW;
	default:
		DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
		return 0;
	}
}

123 124
static u32 hsw_infoframe_data_reg(struct dip_infoframe *frame,
				  enum transcoder cpu_transcoder)
125 126 127
{
	switch (frame->type) {
	case DIP_TYPE_AVI:
128
		return HSW_TVIDEO_DIP_AVI_DATA(cpu_transcoder);
129
	case DIP_TYPE_SPD:
130
		return HSW_TVIDEO_DIP_SPD_DATA(cpu_transcoder);
131 132 133 134 135 136
	default:
		DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
		return 0;
	}
}

137 138
static void g4x_write_infoframe(struct drm_encoder *encoder,
				struct dip_infoframe *frame)
139 140
{
	uint32_t *data = (uint32_t *)frame;
141 142
	struct drm_device *dev = encoder->dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
143
	u32 val = I915_READ(VIDEO_DIP_CTL);
144
	unsigned i, len = DIP_HEADER_SIZE + frame->len;
145

146 147
	WARN(!(val & VIDEO_DIP_ENABLE), "Writing DIP with CTL reg disabled\n");

148
	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
149
	val |= g4x_infoframe_index(frame);
150

151
	val &= ~g4x_infoframe_enable(frame);
152

153
	I915_WRITE(VIDEO_DIP_CTL, val);
154

155
	mmiowb();
156
	for (i = 0; i < len; i += 4) {
157 158 159
		I915_WRITE(VIDEO_DIP_DATA, *data);
		data++;
	}
160 161 162
	/* Write every possible data byte to force correct ECC calculation. */
	for (; i < VIDEO_DIP_DATA_SIZE; i += 4)
		I915_WRITE(VIDEO_DIP_DATA, 0);
163
	mmiowb();
164

165
	val |= g4x_infoframe_enable(frame);
166
	val &= ~VIDEO_DIP_FREQ_MASK;
167
	val |= VIDEO_DIP_FREQ_VSYNC;
168

169
	I915_WRITE(VIDEO_DIP_CTL, val);
170
	POSTING_READ(VIDEO_DIP_CTL);
171 172
}

173 174 175 176 177 178
static void ibx_write_infoframe(struct drm_encoder *encoder,
				struct dip_infoframe *frame)
{
	uint32_t *data = (uint32_t *)frame;
	struct drm_device *dev = encoder->dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
179
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
180 181 182 183
	int reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
	unsigned i, len = DIP_HEADER_SIZE + frame->len;
	u32 val = I915_READ(reg);

184 185
	WARN(!(val & VIDEO_DIP_ENABLE), "Writing DIP with CTL reg disabled\n");

186
	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
187
	val |= g4x_infoframe_index(frame);
188

189
	val &= ~g4x_infoframe_enable(frame);
190 191 192

	I915_WRITE(reg, val);

193
	mmiowb();
194 195 196 197
	for (i = 0; i < len; i += 4) {
		I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
		data++;
	}
198 199 200
	/* Write every possible data byte to force correct ECC calculation. */
	for (; i < VIDEO_DIP_DATA_SIZE; i += 4)
		I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), 0);
201
	mmiowb();
202

203
	val |= g4x_infoframe_enable(frame);
204
	val &= ~VIDEO_DIP_FREQ_MASK;
205
	val |= VIDEO_DIP_FREQ_VSYNC;
206 207

	I915_WRITE(reg, val);
208
	POSTING_READ(reg);
209 210 211 212
}

static void cpt_write_infoframe(struct drm_encoder *encoder,
				struct dip_infoframe *frame)
213
{
214
	uint32_t *data = (uint32_t *)frame;
215 216
	struct drm_device *dev = encoder->dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
217
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
218
	int reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
219
	unsigned i, len = DIP_HEADER_SIZE + frame->len;
220
	u32 val = I915_READ(reg);
221

222 223
	WARN(!(val & VIDEO_DIP_ENABLE), "Writing DIP with CTL reg disabled\n");

224
	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
225
	val |= g4x_infoframe_index(frame);
226

227 228
	/* The DIP control register spec says that we need to update the AVI
	 * infoframe without clearing its enable bit */
229
	if (frame->type != DIP_TYPE_AVI)
230
		val &= ~g4x_infoframe_enable(frame);
231

232
	I915_WRITE(reg, val);
233

234
	mmiowb();
235
	for (i = 0; i < len; i += 4) {
236 237 238
		I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
		data++;
	}
239 240 241
	/* Write every possible data byte to force correct ECC calculation. */
	for (; i < VIDEO_DIP_DATA_SIZE; i += 4)
		I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), 0);
242
	mmiowb();
243

244
	val |= g4x_infoframe_enable(frame);
245
	val &= ~VIDEO_DIP_FREQ_MASK;
246
	val |= VIDEO_DIP_FREQ_VSYNC;
247

248
	I915_WRITE(reg, val);
249
	POSTING_READ(reg);
250
}
251 252 253 254 255 256 257

static void vlv_write_infoframe(struct drm_encoder *encoder,
				     struct dip_infoframe *frame)
{
	uint32_t *data = (uint32_t *)frame;
	struct drm_device *dev = encoder->dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
258
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
259 260
	int reg = VLV_TVIDEO_DIP_CTL(intel_crtc->pipe);
	unsigned i, len = DIP_HEADER_SIZE + frame->len;
261
	u32 val = I915_READ(reg);
262

263 264
	WARN(!(val & VIDEO_DIP_ENABLE), "Writing DIP with CTL reg disabled\n");

265
	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
266
	val |= g4x_infoframe_index(frame);
267

268
	val &= ~g4x_infoframe_enable(frame);
269

270
	I915_WRITE(reg, val);
271

272
	mmiowb();
273 274 275 276
	for (i = 0; i < len; i += 4) {
		I915_WRITE(VLV_TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
		data++;
	}
277 278 279
	/* Write every possible data byte to force correct ECC calculation. */
	for (; i < VIDEO_DIP_DATA_SIZE; i += 4)
		I915_WRITE(VLV_TVIDEO_DIP_DATA(intel_crtc->pipe), 0);
280
	mmiowb();
281

282
	val |= g4x_infoframe_enable(frame);
283
	val &= ~VIDEO_DIP_FREQ_MASK;
284
	val |= VIDEO_DIP_FREQ_VSYNC;
285

286
	I915_WRITE(reg, val);
287
	POSTING_READ(reg);
288 289
}

290
static void hsw_write_infoframe(struct drm_encoder *encoder,
291
				struct dip_infoframe *frame)
292
{
293 294 295 296
	uint32_t *data = (uint32_t *)frame;
	struct drm_device *dev = encoder->dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
297 298
	u32 ctl_reg = HSW_TVIDEO_DIP_CTL(intel_crtc->config.cpu_transcoder);
	u32 data_reg = hsw_infoframe_data_reg(frame, intel_crtc->config.cpu_transcoder);
299 300
	unsigned int i, len = DIP_HEADER_SIZE + frame->len;
	u32 val = I915_READ(ctl_reg);
301

302 303 304 305 306 307
	if (data_reg == 0)
		return;

	val &= ~hsw_infoframe_enable(frame);
	I915_WRITE(ctl_reg, val);

308
	mmiowb();
309 310 311 312
	for (i = 0; i < len; i += 4) {
		I915_WRITE(data_reg + i, *data);
		data++;
	}
313 314 315
	/* Write every possible data byte to force correct ECC calculation. */
	for (; i < VIDEO_DIP_DATA_SIZE; i += 4)
		I915_WRITE(data_reg + i, 0);
316
	mmiowb();
317

318 319
	val |= hsw_infoframe_enable(frame);
	I915_WRITE(ctl_reg, val);
320
	POSTING_READ(ctl_reg);
321 322
}

323 324 325 326 327 328 329 330 331
static void intel_set_infoframe(struct drm_encoder *encoder,
				struct dip_infoframe *frame)
{
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);

	intel_dip_infoframe_csum(frame);
	intel_hdmi->write_infoframe(encoder, frame);
}

332
static void intel_hdmi_set_avi_infoframe(struct drm_encoder *encoder,
P
Paulo Zanoni 已提交
333
					 struct drm_display_mode *adjusted_mode)
334
{
335
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
336
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
337 338 339 340 341 342
	struct dip_infoframe avi_if = {
		.type = DIP_TYPE_AVI,
		.ver = DIP_VERSION_AVI,
		.len = DIP_LEN_AVI,
	};

P
Paulo Zanoni 已提交
343 344 345
	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
		avi_if.body.avi.YQ_CN_PR |= DIP_AVI_PR_2;

346
	if (intel_hdmi->rgb_quant_range_selectable) {
347
		if (intel_crtc->config.limited_color_range)
348 349 350 351 352
			avi_if.body.avi.ITC_EC_Q_SC |= DIP_AVI_RGB_QUANT_RANGE_LIMITED;
		else
			avi_if.body.avi.ITC_EC_Q_SC |= DIP_AVI_RGB_QUANT_RANGE_FULL;
	}

353
	avi_if.body.avi.VIC = drm_match_cea_mode(adjusted_mode);
354

355
	intel_set_infoframe(encoder, &avi_if);
356 357
}

358
static void intel_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
359 360 361 362 363 364 365 366 367 368 369 370 371 372
{
	struct dip_infoframe spd_if;

	memset(&spd_if, 0, sizeof(spd_if));
	spd_if.type = DIP_TYPE_SPD;
	spd_if.ver = DIP_VERSION_SPD;
	spd_if.len = DIP_LEN_SPD;
	strcpy(spd_if.body.spd.vn, "Intel");
	strcpy(spd_if.body.spd.pd, "Integrated gfx");
	spd_if.body.spd.sdi = DIP_SPD_PC;

	intel_set_infoframe(encoder, &spd_if);
}

373 374 375
static void g4x_set_infoframes(struct drm_encoder *encoder,
			       struct drm_display_mode *adjusted_mode)
{
376
	struct drm_i915_private *dev_priv = encoder->dev->dev_private;
377 378
	struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
	struct intel_hdmi *intel_hdmi = &intel_dig_port->hdmi;
379 380
	u32 reg = VIDEO_DIP_CTL;
	u32 val = I915_READ(reg);
381
	u32 port;
382

383 384
	assert_hdmi_port_disabled(intel_hdmi);

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	/* If the registers were not initialized yet, they might be zeroes,
	 * which means we're selecting the AVI DIP and we're setting its
	 * frequency to once. This seems to really confuse the HW and make
	 * things stop working (the register spec says the AVI always needs to
	 * be sent every VSync). So here we avoid writing to the register more
	 * than we need and also explicitly select the AVI DIP and explicitly
	 * set its frequency to every VSync. Avoiding to write it twice seems to
	 * be enough to solve the problem, but being defensive shouldn't hurt us
	 * either. */
	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;

	if (!intel_hdmi->has_hdmi_sink) {
		if (!(val & VIDEO_DIP_ENABLE))
			return;
		val &= ~VIDEO_DIP_ENABLE;
		I915_WRITE(reg, val);
401
		POSTING_READ(reg);
402 403 404
		return;
	}

405 406
	switch (intel_dig_port->port) {
	case PORT_B:
407
		port = VIDEO_DIP_PORT_B;
408
		break;
409
	case PORT_C:
410
		port = VIDEO_DIP_PORT_C;
411 412
		break;
	default:
413
		BUG();
414 415 416
		return;
	}

417 418 419 420
	if (port != (val & VIDEO_DIP_PORT_MASK)) {
		if (val & VIDEO_DIP_ENABLE) {
			val &= ~VIDEO_DIP_ENABLE;
			I915_WRITE(reg, val);
421
			POSTING_READ(reg);
422 423 424 425 426
		}
		val &= ~VIDEO_DIP_PORT_MASK;
		val |= port;
	}

427
	val |= VIDEO_DIP_ENABLE;
428
	val &= ~VIDEO_DIP_ENABLE_VENDOR;
429

430
	I915_WRITE(reg, val);
431
	POSTING_READ(reg);
432

433 434 435 436 437 438 439
	intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
	intel_hdmi_set_spd_infoframe(encoder);
}

static void ibx_set_infoframes(struct drm_encoder *encoder,
			       struct drm_display_mode *adjusted_mode)
{
440 441
	struct drm_i915_private *dev_priv = encoder->dev->dev_private;
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
442 443
	struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
	struct intel_hdmi *intel_hdmi = &intel_dig_port->hdmi;
444 445
	u32 reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
	u32 val = I915_READ(reg);
446
	u32 port;
447

448 449
	assert_hdmi_port_disabled(intel_hdmi);

450 451 452 453 454 455 456 457
	/* See the big comment in g4x_set_infoframes() */
	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;

	if (!intel_hdmi->has_hdmi_sink) {
		if (!(val & VIDEO_DIP_ENABLE))
			return;
		val &= ~VIDEO_DIP_ENABLE;
		I915_WRITE(reg, val);
458
		POSTING_READ(reg);
459 460 461
		return;
	}

462 463
	switch (intel_dig_port->port) {
	case PORT_B:
464
		port = VIDEO_DIP_PORT_B;
465
		break;
466
	case PORT_C:
467
		port = VIDEO_DIP_PORT_C;
468
		break;
469
	case PORT_D:
470
		port = VIDEO_DIP_PORT_D;
471 472
		break;
	default:
473
		BUG();
474 475 476
		return;
	}

477 478 479 480
	if (port != (val & VIDEO_DIP_PORT_MASK)) {
		if (val & VIDEO_DIP_ENABLE) {
			val &= ~VIDEO_DIP_ENABLE;
			I915_WRITE(reg, val);
481
			POSTING_READ(reg);
482 483 484 485 486
		}
		val &= ~VIDEO_DIP_PORT_MASK;
		val |= port;
	}

487
	val |= VIDEO_DIP_ENABLE;
488 489
	val &= ~(VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
		 VIDEO_DIP_ENABLE_GCP);
490

491
	I915_WRITE(reg, val);
492
	POSTING_READ(reg);
493

494 495 496 497 498 499 500
	intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
	intel_hdmi_set_spd_infoframe(encoder);
}

static void cpt_set_infoframes(struct drm_encoder *encoder,
			       struct drm_display_mode *adjusted_mode)
{
501 502 503 504 505 506
	struct drm_i915_private *dev_priv = encoder->dev->dev_private;
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
	u32 reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
	u32 val = I915_READ(reg);

507 508
	assert_hdmi_port_disabled(intel_hdmi);

509 510 511 512 513 514 515 516
	/* See the big comment in g4x_set_infoframes() */
	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;

	if (!intel_hdmi->has_hdmi_sink) {
		if (!(val & VIDEO_DIP_ENABLE))
			return;
		val &= ~(VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI);
		I915_WRITE(reg, val);
517
		POSTING_READ(reg);
518 519 520
		return;
	}

521 522
	/* Set both together, unset both together: see the spec. */
	val |= VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI;
523 524
	val &= ~(VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
		 VIDEO_DIP_ENABLE_GCP);
525 526

	I915_WRITE(reg, val);
527
	POSTING_READ(reg);
528

529 530 531 532 533 534 535
	intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
	intel_hdmi_set_spd_infoframe(encoder);
}

static void vlv_set_infoframes(struct drm_encoder *encoder,
			       struct drm_display_mode *adjusted_mode)
{
536 537 538 539 540 541
	struct drm_i915_private *dev_priv = encoder->dev->dev_private;
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
	u32 reg = VLV_TVIDEO_DIP_CTL(intel_crtc->pipe);
	u32 val = I915_READ(reg);

542 543
	assert_hdmi_port_disabled(intel_hdmi);

544 545 546 547 548 549 550 551
	/* See the big comment in g4x_set_infoframes() */
	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;

	if (!intel_hdmi->has_hdmi_sink) {
		if (!(val & VIDEO_DIP_ENABLE))
			return;
		val &= ~VIDEO_DIP_ENABLE;
		I915_WRITE(reg, val);
552
		POSTING_READ(reg);
553 554 555
		return;
	}

556
	val |= VIDEO_DIP_ENABLE;
557 558
	val &= ~(VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
		 VIDEO_DIP_ENABLE_GCP);
559 560

	I915_WRITE(reg, val);
561
	POSTING_READ(reg);
562

563 564 565 566 567 568 569
	intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
	intel_hdmi_set_spd_infoframe(encoder);
}

static void hsw_set_infoframes(struct drm_encoder *encoder,
			       struct drm_display_mode *adjusted_mode)
{
570 571 572
	struct drm_i915_private *dev_priv = encoder->dev->dev_private;
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
573
	u32 reg = HSW_TVIDEO_DIP_CTL(intel_crtc->config.cpu_transcoder);
574
	u32 val = I915_READ(reg);
575

576 577
	assert_hdmi_port_disabled(intel_hdmi);

578 579
	if (!intel_hdmi->has_hdmi_sink) {
		I915_WRITE(reg, 0);
580
		POSTING_READ(reg);
581 582 583
		return;
	}

584 585 586 587
	val &= ~(VIDEO_DIP_ENABLE_VSC_HSW | VIDEO_DIP_ENABLE_GCP_HSW |
		 VIDEO_DIP_ENABLE_VS_HSW | VIDEO_DIP_ENABLE_GMP_HSW);

	I915_WRITE(reg, val);
588
	POSTING_READ(reg);
589

590 591 592 593
	intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
	intel_hdmi_set_spd_infoframe(encoder);
}

594 595 596 597 598 599
static void intel_hdmi_mode_set(struct drm_encoder *encoder,
				struct drm_display_mode *mode,
				struct drm_display_mode *adjusted_mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
600
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
C
Chris Wilson 已提交
601
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
602
	u32 hdmi_val;
603

604
	hdmi_val = SDVO_ENCODING_HDMI;
605
	if (!HAS_PCH_SPLIT(dev))
606
		hdmi_val |= intel_hdmi->color_range;
607
	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
608
		hdmi_val |= SDVO_VSYNC_ACTIVE_HIGH;
609
	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
610
		hdmi_val |= SDVO_HSYNC_ACTIVE_HIGH;
611

612
	if (intel_crtc->config.pipe_bpp > 24)
613
		hdmi_val |= HDMI_COLOR_FORMAT_12bpc;
614
	else
615
		hdmi_val |= SDVO_COLOR_FORMAT_8bpc;
616

617 618
	/* Required on CPT */
	if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
619
		hdmi_val |= HDMI_MODE_SELECT_HDMI;
620

621
	if (intel_hdmi->has_audio) {
622 623
		DRM_DEBUG_DRIVER("Enabling HDMI audio on pipe %c\n",
				 pipe_name(intel_crtc->pipe));
624
		hdmi_val |= SDVO_AUDIO_ENABLE;
625
		hdmi_val |= HDMI_MODE_SELECT_HDMI;
626
		intel_write_eld(encoder, adjusted_mode);
627
	}
628

629
	if (HAS_PCH_CPT(dev))
630 631 632
		hdmi_val |= SDVO_PIPE_SEL_CPT(intel_crtc->pipe);
	else
		hdmi_val |= SDVO_PIPE_SEL(intel_crtc->pipe);
633

634 635
	I915_WRITE(intel_hdmi->hdmi_reg, hdmi_val);
	POSTING_READ(intel_hdmi->hdmi_reg);
636

637
	intel_hdmi->set_infoframes(encoder, adjusted_mode);
638 639
}

640 641
static bool intel_hdmi_get_hw_state(struct intel_encoder *encoder,
				    enum pipe *pipe)
642
{
643
	struct drm_device *dev = encoder->base.dev;
644
	struct drm_i915_private *dev_priv = dev->dev_private;
645 646 647
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
	u32 tmp;

648
	tmp = I915_READ(intel_hdmi->hdmi_reg);
649 650 651 652 653 654 655 656 657 658 659 660

	if (!(tmp & SDVO_ENABLE))
		return false;

	if (HAS_PCH_CPT(dev))
		*pipe = PORT_TO_PIPE_CPT(tmp);
	else
		*pipe = PORT_TO_PIPE(tmp);

	return true;
}

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
static void intel_hdmi_get_config(struct intel_encoder *encoder,
				  struct intel_crtc_config *pipe_config)
{
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
	u32 tmp, flags = 0;

	tmp = I915_READ(intel_hdmi->hdmi_reg);

	if (tmp & SDVO_HSYNC_ACTIVE_HIGH)
		flags |= DRM_MODE_FLAG_PHSYNC;
	else
		flags |= DRM_MODE_FLAG_NHSYNC;

	if (tmp & SDVO_VSYNC_ACTIVE_HIGH)
		flags |= DRM_MODE_FLAG_PVSYNC;
	else
		flags |= DRM_MODE_FLAG_NVSYNC;

	pipe_config->adjusted_mode.flags |= flags;
}

683
static void intel_enable_hdmi(struct intel_encoder *encoder)
684
{
685
	struct drm_device *dev = encoder->base.dev;
686
	struct drm_i915_private *dev_priv = dev->dev_private;
687
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
688
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
689
	u32 temp;
690 691 692 693
	u32 enable_bits = SDVO_ENABLE;

	if (intel_hdmi->has_audio)
		enable_bits |= SDVO_AUDIO_ENABLE;
694

695
	temp = I915_READ(intel_hdmi->hdmi_reg);
696

697
	/* HW workaround for IBX, we need to move the port to transcoder A
698 699 700
	 * before disabling it, so restore the transcoder select bit here. */
	if (HAS_PCH_IBX(dev))
		enable_bits |= SDVO_PIPE_SEL(intel_crtc->pipe);
701

702 703 704
	/* HW workaround, need to toggle enable bit off and on for 12bpc, but
	 * we do this anyway which shows more stable in testing.
	 */
705
	if (HAS_PCH_SPLIT(dev)) {
706 707
		I915_WRITE(intel_hdmi->hdmi_reg, temp & ~SDVO_ENABLE);
		POSTING_READ(intel_hdmi->hdmi_reg);
708 709
	}

710 711
	temp |= enable_bits;

712 713
	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);
714 715 716 717 718

	/* HW workaround, need to write this twice for issue that may result
	 * in first write getting masked.
	 */
	if (HAS_PCH_SPLIT(dev)) {
719 720
		I915_WRITE(intel_hdmi->hdmi_reg, temp);
		POSTING_READ(intel_hdmi->hdmi_reg);
721
	}
722 723 724 725 726 727 728 729

	if (IS_VALLEYVIEW(dev)) {
		struct intel_digital_port *dport =
			enc_to_dig_port(&encoder->base);
		int channel = vlv_dport_to_channel(dport);

		vlv_wait_port_ready(dev_priv, channel);
	}
730 731 732 733 734 735 736 737
}

static void intel_disable_hdmi(struct intel_encoder *encoder)
{
	struct drm_device *dev = encoder->base.dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
	u32 temp;
738
	u32 enable_bits = SDVO_ENABLE | SDVO_AUDIO_ENABLE;
739

740
	temp = I915_READ(intel_hdmi->hdmi_reg);
741 742 743 744 745 746 747 748 749

	/* HW workaround for IBX, we need to move the port to transcoder A
	 * before disabling it. */
	if (HAS_PCH_IBX(dev)) {
		struct drm_crtc *crtc = encoder->base.crtc;
		int pipe = crtc ? to_intel_crtc(crtc)->pipe : -1;

		if (temp & SDVO_PIPE_B_SELECT) {
			temp &= ~SDVO_PIPE_B_SELECT;
750 751
			I915_WRITE(intel_hdmi->hdmi_reg, temp);
			POSTING_READ(intel_hdmi->hdmi_reg);
752 753

			/* Again we need to write this twice. */
754 755
			I915_WRITE(intel_hdmi->hdmi_reg, temp);
			POSTING_READ(intel_hdmi->hdmi_reg);
756 757 758 759 760 761 762 763

			/* Transcoder selection bits only update
			 * effectively on vblank. */
			if (crtc)
				intel_wait_for_vblank(dev, pipe);
			else
				msleep(50);
		}
764
	}
765

766 767 768 769
	/* HW workaround, need to toggle enable bit off and on for 12bpc, but
	 * we do this anyway which shows more stable in testing.
	 */
	if (HAS_PCH_SPLIT(dev)) {
770 771
		I915_WRITE(intel_hdmi->hdmi_reg, temp & ~SDVO_ENABLE);
		POSTING_READ(intel_hdmi->hdmi_reg);
772 773 774
	}

	temp &= ~enable_bits;
775

776 777
	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);
778 779 780 781

	/* HW workaround, need to write this twice for issue that may result
	 * in first write getting masked.
	 */
782
	if (HAS_PCH_SPLIT(dev)) {
783 784
		I915_WRITE(intel_hdmi->hdmi_reg, temp);
		POSTING_READ(intel_hdmi->hdmi_reg);
785
	}
786 787 788 789 790 791 792 793
}

static int intel_hdmi_mode_valid(struct drm_connector *connector,
				 struct drm_display_mode *mode)
{
	if (mode->clock > 165000)
		return MODE_CLOCK_HIGH;
	if (mode->clock < 20000)
794
		return MODE_CLOCK_LOW;
795 796 797 798 799 800 801

	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
		return MODE_NO_DBLESCAN;

	return MODE_OK;
}

802 803
bool intel_hdmi_compute_config(struct intel_encoder *encoder,
			       struct intel_crtc_config *pipe_config)
804
{
805 806 807
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
	struct drm_device *dev = encoder->base.dev;
	struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
808
	int clock_12bpc = pipe_config->requested_mode.clock * 3 / 2;
809
	int desired_bpp;
810

811 812 813
	if (intel_hdmi->color_range_auto) {
		/* See CEA-861-E - 5.1 Default Encoding Parameters */
		if (intel_hdmi->has_hdmi_sink &&
814
		    drm_match_cea_mode(adjusted_mode) > 1)
815
			intel_hdmi->color_range = HDMI_COLOR_RANGE_16_235;
816 817 818 819
		else
			intel_hdmi->color_range = 0;
	}

820
	if (intel_hdmi->color_range)
821
		pipe_config->limited_color_range = true;
822

823 824 825
	if (HAS_PCH_SPLIT(dev) && !HAS_DDI(dev))
		pipe_config->has_pch_encoder = true;

826 827 828
	/*
	 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
	 * through, clamp it down. Note that g4x/vlv don't support 12bpc hdmi
829 830
	 * outputs. We also need to check that the higher clock still fits
	 * within limits.
831
	 */
832 833
	if (pipe_config->pipe_bpp > 8*3 && clock_12bpc <= 225000
	    && HAS_PCH_SPLIT(dev)) {
834 835
		DRM_DEBUG_KMS("picking bpc to 12 for HDMI output\n");
		desired_bpp = 12*3;
836 837

		/* Need to adjust the port link by 1.5x for 12bpc. */
838
		pipe_config->port_clock = clock_12bpc;
839
	} else {
840 841 842 843 844 845 846
		DRM_DEBUG_KMS("picking bpc to 8 for HDMI output\n");
		desired_bpp = 8*3;
	}

	if (!pipe_config->bw_constrained) {
		DRM_DEBUG_KMS("forcing pipe bpc to %i for HDMI\n", desired_bpp);
		pipe_config->pipe_bpp = desired_bpp;
847 848
	}

849 850 851 852 853
	if (adjusted_mode->clock > 225000) {
		DRM_DEBUG_KMS("too high HDMI clock, rejecting mode\n");
		return false;
	}

854 855 856
	return true;
}

857
static enum drm_connector_status
858
intel_hdmi_detect(struct drm_connector *connector, bool force)
859
{
860
	struct drm_device *dev = connector->dev;
861
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
862 863 864
	struct intel_digital_port *intel_dig_port =
		hdmi_to_dig_port(intel_hdmi);
	struct intel_encoder *intel_encoder = &intel_dig_port->base;
865
	struct drm_i915_private *dev_priv = dev->dev_private;
866
	struct edid *edid;
867
	enum drm_connector_status status = connector_status_disconnected;
868

869 870 871
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
		      connector->base.id, drm_get_connector_name(connector));

C
Chris Wilson 已提交
872
	intel_hdmi->has_hdmi_sink = false;
873
	intel_hdmi->has_audio = false;
874
	intel_hdmi->rgb_quant_range_selectable = false;
875
	edid = drm_get_edid(connector,
876 877
			    intel_gmbus_get_adapter(dev_priv,
						    intel_hdmi->ddc_bus));
878

879
	if (edid) {
880
		if (edid->input & DRM_EDID_INPUT_DIGITAL) {
881
			status = connector_status_connected;
882 883 884
			if (intel_hdmi->force_audio != HDMI_AUDIO_OFF_DVI)
				intel_hdmi->has_hdmi_sink =
						drm_detect_hdmi_monitor(edid);
885
			intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
886 887
			intel_hdmi->rgb_quant_range_selectable =
				drm_rgb_quant_range_selectable(edid);
888 889
		}
		kfree(edid);
890
	}
891

892
	if (status == connector_status_connected) {
893 894 895
		if (intel_hdmi->force_audio != HDMI_AUDIO_AUTO)
			intel_hdmi->has_audio =
				(intel_hdmi->force_audio == HDMI_AUDIO_ON);
896
		intel_encoder->type = INTEL_OUTPUT_HDMI;
897 898
	}

899
	return status;
900 901 902 903
}

static int intel_hdmi_get_modes(struct drm_connector *connector)
{
904
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
905
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
906 907 908 909 910

	/* We should parse the EDID data and find out if it's an HDMI sink so
	 * we can send audio to it.
	 */

911
	return intel_ddc_get_modes(connector,
912 913
				   intel_gmbus_get_adapter(dev_priv,
							   intel_hdmi->ddc_bus));
914 915
}

916 917 918 919 920 921 922 923 924
static bool
intel_hdmi_detect_audio(struct drm_connector *connector)
{
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
	struct edid *edid;
	bool has_audio = false;

	edid = drm_get_edid(connector,
925 926
			    intel_gmbus_get_adapter(dev_priv,
						    intel_hdmi->ddc_bus));
927 928 929 930 931 932 933 934 935
	if (edid) {
		if (edid->input & DRM_EDID_INPUT_DIGITAL)
			has_audio = drm_detect_monitor_audio(edid);
		kfree(edid);
	}

	return has_audio;
}

936 937
static int
intel_hdmi_set_property(struct drm_connector *connector,
938 939
			struct drm_property *property,
			uint64_t val)
940 941
{
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
942 943
	struct intel_digital_port *intel_dig_port =
		hdmi_to_dig_port(intel_hdmi);
944
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
945 946
	int ret;

947
	ret = drm_object_property_set_value(&connector->base, property, val);
948 949 950
	if (ret)
		return ret;

951
	if (property == dev_priv->force_audio_property) {
952
		enum hdmi_force_audio i = val;
953 954 955
		bool has_audio;

		if (i == intel_hdmi->force_audio)
956 957
			return 0;

958
		intel_hdmi->force_audio = i;
959

960
		if (i == HDMI_AUDIO_AUTO)
961 962
			has_audio = intel_hdmi_detect_audio(connector);
		else
963
			has_audio = (i == HDMI_AUDIO_ON);
964

965 966
		if (i == HDMI_AUDIO_OFF_DVI)
			intel_hdmi->has_hdmi_sink = 0;
967

968
		intel_hdmi->has_audio = has_audio;
969 970 971
		goto done;
	}

972
	if (property == dev_priv->broadcast_rgb_property) {
973 974 975
		bool old_auto = intel_hdmi->color_range_auto;
		uint32_t old_range = intel_hdmi->color_range;

976 977 978 979 980 981 982 983 984 985
		switch (val) {
		case INTEL_BROADCAST_RGB_AUTO:
			intel_hdmi->color_range_auto = true;
			break;
		case INTEL_BROADCAST_RGB_FULL:
			intel_hdmi->color_range_auto = false;
			intel_hdmi->color_range = 0;
			break;
		case INTEL_BROADCAST_RGB_LIMITED:
			intel_hdmi->color_range_auto = false;
986
			intel_hdmi->color_range = HDMI_COLOR_RANGE_16_235;
987 988 989 990
			break;
		default:
			return -EINVAL;
		}
991 992 993 994 995

		if (old_auto == intel_hdmi->color_range_auto &&
		    old_range == intel_hdmi->color_range)
			return 0;

996 997 998
		goto done;
	}

999 1000 1001
	return -EINVAL;

done:
1002 1003
	if (intel_dig_port->base.base.crtc)
		intel_crtc_restore_mode(intel_dig_port->base.base.crtc);
1004 1005 1006 1007

	return 0;
}

1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
static void intel_hdmi_pre_enable(struct intel_encoder *encoder)
{
	struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
	struct drm_device *dev = encoder->base.dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
	struct intel_crtc *intel_crtc =
		to_intel_crtc(encoder->base.crtc);
	int port = vlv_dport_to_channel(dport);
	int pipe = intel_crtc->pipe;
	u32 val;

	if (!IS_VALLEYVIEW(dev))
		return;

	/* Enable clock channels for this port */
1023
	val = vlv_dpio_read(dev_priv, DPIO_DATA_LANE_A(port));
1024 1025 1026 1027 1028 1029
	val = 0;
	if (pipe)
		val |= (1<<21);
	else
		val &= ~(1<<21);
	val |= 0x001000c4;
1030
	vlv_dpio_write(dev_priv, DPIO_DATA_CHANNEL(port), val);
1031 1032

	/* HDMI 1.0V-2dB */
1033 1034
	vlv_dpio_write(dev_priv, DPIO_TX_OCALINIT(port), 0);
	vlv_dpio_write(dev_priv, DPIO_TX_SWING_CTL4(port),
1035
			 0x2b245f5f);
1036
	vlv_dpio_write(dev_priv, DPIO_TX_SWING_CTL2(port),
1037
			 0x5578b83a);
1038
	vlv_dpio_write(dev_priv, DPIO_TX_SWING_CTL3(port),
1039
			 0x0c782040);
1040
	vlv_dpio_write(dev_priv, DPIO_TX3_SWING_CTL4(port),
1041
			 0x2b247878);
1042 1043
	vlv_dpio_write(dev_priv, DPIO_PCS_STAGGER0(port), 0x00030000);
	vlv_dpio_write(dev_priv, DPIO_PCS_CTL_OVER1(port),
1044
			 0x00002000);
1045
	vlv_dpio_write(dev_priv, DPIO_TX_OCALINIT(port),
1046 1047 1048
			 DPIO_TX_OCALINIT_EN);

	/* Program lane clock */
1049
	vlv_dpio_write(dev_priv, DPIO_PCS_CLOCKBUF0(port),
1050
			 0x00760018);
1051
	vlv_dpio_write(dev_priv, DPIO_PCS_CLOCKBUF8(port),
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
			 0x00400888);
}

static void intel_hdmi_pre_pll_enable(struct intel_encoder *encoder)
{
	struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
	struct drm_device *dev = encoder->base.dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
	int port = vlv_dport_to_channel(dport);

	if (!IS_VALLEYVIEW(dev))
		return;

	/* Program Tx lane resets to default */
1066
	vlv_dpio_write(dev_priv, DPIO_PCS_TX(port),
1067 1068
			 DPIO_PCS_TX_LANE2_RESET |
			 DPIO_PCS_TX_LANE1_RESET);
1069
	vlv_dpio_write(dev_priv, DPIO_PCS_CLK(port),
1070 1071 1072 1073 1074 1075
			 DPIO_PCS_CLK_CRI_RXEB_EIOS_EN |
			 DPIO_PCS_CLK_CRI_RXDIGFILTSG_EN |
			 (1<<DPIO_PCS_CLK_DATAWIDTH_SHIFT) |
			 DPIO_PCS_CLK_SOFT_RESET);

	/* Fix up inter-pair skew failure */
1076 1077 1078
	vlv_dpio_write(dev_priv, DPIO_PCS_STAGGER1(port), 0x00750f00);
	vlv_dpio_write(dev_priv, DPIO_TX_CTL(port), 0x00001500);
	vlv_dpio_write(dev_priv, DPIO_TX_LANE(port), 0x40400000);
1079

1080
	vlv_dpio_write(dev_priv, DPIO_PCS_CTL_OVER1(port),
1081
			 0x00002000);
1082
	vlv_dpio_write(dev_priv, DPIO_TX_OCALINIT(port),
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
			 DPIO_TX_OCALINIT_EN);
}

static void intel_hdmi_post_disable(struct intel_encoder *encoder)
{
	struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
	int port = vlv_dport_to_channel(dport);

	/* Reset lanes to avoid HDMI flicker (VLV w/a) */
	mutex_lock(&dev_priv->dpio_lock);
1094 1095
	vlv_dpio_write(dev_priv, DPIO_PCS_TX(port), 0x00000000);
	vlv_dpio_write(dev_priv, DPIO_PCS_CLK(port), 0x00e00060);
1096 1097 1098
	mutex_unlock(&dev_priv->dpio_lock);
}

1099 1100 1101 1102
static void intel_hdmi_destroy(struct drm_connector *connector)
{
	drm_sysfs_connector_remove(connector);
	drm_connector_cleanup(connector);
1103
	kfree(connector);
1104 1105 1106 1107 1108 1109 1110
}

static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
	.mode_set = intel_hdmi_mode_set,
};

static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
1111
	.dpms = intel_connector_dpms,
1112 1113
	.detect = intel_hdmi_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
1114
	.set_property = intel_hdmi_set_property,
1115 1116 1117 1118 1119 1120
	.destroy = intel_hdmi_destroy,
};

static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
	.get_modes = intel_hdmi_get_modes,
	.mode_valid = intel_hdmi_mode_valid,
1121
	.best_encoder = intel_best_encoder,
1122 1123 1124
};

static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
C
Chris Wilson 已提交
1125
	.destroy = intel_encoder_destroy,
1126 1127
};

1128 1129 1130
static void
intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
{
1131
	intel_attach_force_audio_property(connector);
1132
	intel_attach_broadcast_rgb_property(connector);
1133
	intel_hdmi->color_range_auto = true;
1134 1135
}

P
Paulo Zanoni 已提交
1136 1137
void intel_hdmi_init_connector(struct intel_digital_port *intel_dig_port,
			       struct intel_connector *intel_connector)
1138
{
1139 1140 1141 1142
	struct drm_connector *connector = &intel_connector->base;
	struct intel_hdmi *intel_hdmi = &intel_dig_port->hdmi;
	struct intel_encoder *intel_encoder = &intel_dig_port->base;
	struct drm_device *dev = intel_encoder->base.dev;
1143
	struct drm_i915_private *dev_priv = dev->dev_private;
1144
	enum port port = intel_dig_port->port;
1145

1146
	drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
1147
			   DRM_MODE_CONNECTOR_HDMIA);
1148 1149
	drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);

1150
	connector->interlace_allowed = 1;
1151
	connector->doublescan_allowed = 0;
1152

1153 1154
	switch (port) {
	case PORT_B:
1155
		intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
1156
		intel_encoder->hpd_pin = HPD_PORT_B;
1157 1158
		break;
	case PORT_C:
1159
		intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
1160
		intel_encoder->hpd_pin = HPD_PORT_C;
1161 1162
		break;
	case PORT_D:
1163
		intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
1164
		intel_encoder->hpd_pin = HPD_PORT_D;
1165 1166
		break;
	case PORT_A:
1167
		intel_encoder->hpd_pin = HPD_PORT_A;
1168 1169
		/* Internal port only for eDP. */
	default:
1170
		BUG();
1171
	}
1172

1173
	if (IS_VALLEYVIEW(dev)) {
1174
		intel_hdmi->write_infoframe = vlv_write_infoframe;
1175
		intel_hdmi->set_infoframes = vlv_set_infoframes;
1176 1177 1178
	} else if (!HAS_PCH_SPLIT(dev)) {
		intel_hdmi->write_infoframe = g4x_write_infoframe;
		intel_hdmi->set_infoframes = g4x_set_infoframes;
1179
	} else if (HAS_DDI(dev)) {
1180
		intel_hdmi->write_infoframe = hsw_write_infoframe;
1181
		intel_hdmi->set_infoframes = hsw_set_infoframes;
1182 1183
	} else if (HAS_PCH_IBX(dev)) {
		intel_hdmi->write_infoframe = ibx_write_infoframe;
1184
		intel_hdmi->set_infoframes = ibx_set_infoframes;
1185 1186
	} else {
		intel_hdmi->write_infoframe = cpt_write_infoframe;
1187
		intel_hdmi->set_infoframes = cpt_set_infoframes;
1188
	}
1189

P
Paulo Zanoni 已提交
1190
	if (HAS_DDI(dev))
1191 1192 1193
		intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
	else
		intel_connector->get_hw_state = intel_connector_get_hw_state;
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

	intel_hdmi_add_properties(intel_hdmi, connector);

	intel_connector_attach_encoder(intel_connector, intel_encoder);
	drm_sysfs_connector_add(connector);

	/* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
	 * 0xd.  Failure to do so will result in spurious interrupts being
	 * generated on the port when a cable is not attached.
	 */
	if (IS_G4X(dev) && !IS_GM45(dev)) {
		u32 temp = I915_READ(PEG_BAND_GAP_DATA);
		I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
	}
}

1210
void intel_hdmi_init(struct drm_device *dev, int hdmi_reg, enum port port)
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
{
	struct intel_digital_port *intel_dig_port;
	struct intel_encoder *intel_encoder;
	struct drm_encoder *encoder;
	struct intel_connector *intel_connector;

	intel_dig_port = kzalloc(sizeof(struct intel_digital_port), GFP_KERNEL);
	if (!intel_dig_port)
		return;

	intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
	if (!intel_connector) {
		kfree(intel_dig_port);
		return;
	}

	intel_encoder = &intel_dig_port->base;
	encoder = &intel_encoder->base;

	drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
			 DRM_MODE_ENCODER_TMDS);
P
Paulo Zanoni 已提交
1232 1233
	drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);

1234
	intel_encoder->compute_config = intel_hdmi_compute_config;
P
Paulo Zanoni 已提交
1235 1236 1237
	intel_encoder->enable = intel_enable_hdmi;
	intel_encoder->disable = intel_disable_hdmi;
	intel_encoder->get_hw_state = intel_hdmi_get_hw_state;
1238
	intel_encoder->get_config = intel_hdmi_get_config;
1239 1240 1241 1242 1243
	if (IS_VALLEYVIEW(dev)) {
		intel_encoder->pre_enable = intel_hdmi_pre_enable;
		intel_encoder->pre_pll_enable = intel_hdmi_pre_pll_enable;
		intel_encoder->post_disable = intel_hdmi_post_disable;
	}
1244

1245 1246 1247
	intel_encoder->type = INTEL_OUTPUT_HDMI;
	intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
	intel_encoder->cloneable = false;
1248

1249
	intel_dig_port->port = port;
1250
	intel_dig_port->hdmi.hdmi_reg = hdmi_reg;
1251
	intel_dig_port->dp.output_reg = 0;
1252

1253
	intel_hdmi_init_connector(intel_dig_port, intel_connector);
1254
}