intel_hdmi.c 30.5 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->cpu_transcoder);
	u32 data_reg = hsw_infoframe_data_reg(frame, intel_crtc->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->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) && !IS_VALLEYVIEW(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
static void intel_enable_hdmi(struct intel_encoder *encoder)
662
{
663
	struct drm_device *dev = encoder->base.dev;
664
	struct drm_i915_private *dev_priv = dev->dev_private;
665
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
666
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
667
	u32 temp;
668 669 670 671
	u32 enable_bits = SDVO_ENABLE;

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

673
	temp = I915_READ(intel_hdmi->hdmi_reg);
674

675
	/* HW workaround for IBX, we need to move the port to transcoder A
676 677 678
	 * before disabling it, so restore the transcoder select bit here. */
	if (HAS_PCH_IBX(dev))
		enable_bits |= SDVO_PIPE_SEL(intel_crtc->pipe);
679

680 681 682
	/* HW workaround, need to toggle enable bit off and on for 12bpc, but
	 * we do this anyway which shows more stable in testing.
	 */
683
	if (HAS_PCH_SPLIT(dev)) {
684 685
		I915_WRITE(intel_hdmi->hdmi_reg, temp & ~SDVO_ENABLE);
		POSTING_READ(intel_hdmi->hdmi_reg);
686 687
	}

688 689
	temp |= enable_bits;

690 691
	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);
692 693 694 695 696

	/* HW workaround, need to write this twice for issue that may result
	 * in first write getting masked.
	 */
	if (HAS_PCH_SPLIT(dev)) {
697 698
		I915_WRITE(intel_hdmi->hdmi_reg, temp);
		POSTING_READ(intel_hdmi->hdmi_reg);
699
	}
700 701 702 703 704 705 706 707
}

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;
708
	u32 enable_bits = SDVO_ENABLE | SDVO_AUDIO_ENABLE;
709

710
	temp = I915_READ(intel_hdmi->hdmi_reg);
711 712 713 714 715 716 717 718 719

	/* 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;
720 721
			I915_WRITE(intel_hdmi->hdmi_reg, temp);
			POSTING_READ(intel_hdmi->hdmi_reg);
722 723

			/* Again we need to write this twice. */
724 725
			I915_WRITE(intel_hdmi->hdmi_reg, temp);
			POSTING_READ(intel_hdmi->hdmi_reg);
726 727 728 729 730 731 732 733

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

736 737 738 739
	/* 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)) {
740 741
		I915_WRITE(intel_hdmi->hdmi_reg, temp & ~SDVO_ENABLE);
		POSTING_READ(intel_hdmi->hdmi_reg);
742 743 744
	}

	temp &= ~enable_bits;
745

746 747
	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);
748 749 750 751

	/* HW workaround, need to write this twice for issue that may result
	 * in first write getting masked.
	 */
752
	if (HAS_PCH_SPLIT(dev)) {
753 754
		I915_WRITE(intel_hdmi->hdmi_reg, temp);
		POSTING_READ(intel_hdmi->hdmi_reg);
755
	}
756 757 758 759 760 761 762 763
}

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)
764
		return MODE_CLOCK_LOW;
765 766 767 768 769 770 771

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

	return MODE_OK;
}

772 773
bool intel_hdmi_compute_config(struct intel_encoder *encoder,
			       struct intel_crtc_config *pipe_config)
774
{
775 776 777
	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;
778

779 780 781
	if (intel_hdmi->color_range_auto) {
		/* See CEA-861-E - 5.1 Default Encoding Parameters */
		if (intel_hdmi->has_hdmi_sink &&
782
		    drm_match_cea_mode(adjusted_mode) > 1)
783
			intel_hdmi->color_range = HDMI_COLOR_RANGE_16_235;
784 785 786 787
		else
			intel_hdmi->color_range = 0;
	}

788
	if (intel_hdmi->color_range)
789
		pipe_config->limited_color_range = true;
790

791 792 793
	if (HAS_PCH_SPLIT(dev) && !HAS_DDI(dev))
		pipe_config->has_pch_encoder = true;

794 795 796 797 798 799 800 801 802 803 804 805 806
	/*
	 * 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
	 * outputs.
	 */
	if (pipe_config->pipe_bpp > 8*3 && HAS_PCH_SPLIT(dev)) {
		DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
		pipe_config->pipe_bpp = 12*3;
	} else {
		DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
		pipe_config->pipe_bpp = 8*3;
	}

807 808 809
	return true;
}

810
static enum drm_connector_status
811
intel_hdmi_detect(struct drm_connector *connector, bool force)
812
{
813
	struct drm_device *dev = connector->dev;
814
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
815 816 817
	struct intel_digital_port *intel_dig_port =
		hdmi_to_dig_port(intel_hdmi);
	struct intel_encoder *intel_encoder = &intel_dig_port->base;
818
	struct drm_i915_private *dev_priv = dev->dev_private;
819
	struct edid *edid;
820
	enum drm_connector_status status = connector_status_disconnected;
821

C
Chris Wilson 已提交
822
	intel_hdmi->has_hdmi_sink = false;
823
	intel_hdmi->has_audio = false;
824
	intel_hdmi->rgb_quant_range_selectable = false;
825
	edid = drm_get_edid(connector,
826 827
			    intel_gmbus_get_adapter(dev_priv,
						    intel_hdmi->ddc_bus));
828

829
	if (edid) {
830
		if (edid->input & DRM_EDID_INPUT_DIGITAL) {
831
			status = connector_status_connected;
832 833 834
			if (intel_hdmi->force_audio != HDMI_AUDIO_OFF_DVI)
				intel_hdmi->has_hdmi_sink =
						drm_detect_hdmi_monitor(edid);
835
			intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
836 837
			intel_hdmi->rgb_quant_range_selectable =
				drm_rgb_quant_range_selectable(edid);
838 839
		}
		kfree(edid);
840
	}
841

842
	if (status == connector_status_connected) {
843 844 845
		if (intel_hdmi->force_audio != HDMI_AUDIO_AUTO)
			intel_hdmi->has_audio =
				(intel_hdmi->force_audio == HDMI_AUDIO_ON);
846
		intel_encoder->type = INTEL_OUTPUT_HDMI;
847 848
	}

849
	return status;
850 851 852 853
}

static int intel_hdmi_get_modes(struct drm_connector *connector)
{
854
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
855
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
856 857 858 859 860

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

861
	return intel_ddc_get_modes(connector,
862 863
				   intel_gmbus_get_adapter(dev_priv,
							   intel_hdmi->ddc_bus));
864 865
}

866 867 868 869 870 871 872 873 874
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,
875 876
			    intel_gmbus_get_adapter(dev_priv,
						    intel_hdmi->ddc_bus));
877 878 879 880 881 882 883 884 885
	if (edid) {
		if (edid->input & DRM_EDID_INPUT_DIGITAL)
			has_audio = drm_detect_monitor_audio(edid);
		kfree(edid);
	}

	return has_audio;
}

886 887
static int
intel_hdmi_set_property(struct drm_connector *connector,
888 889
			struct drm_property *property,
			uint64_t val)
890 891
{
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
892 893
	struct intel_digital_port *intel_dig_port =
		hdmi_to_dig_port(intel_hdmi);
894
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
895 896
	int ret;

897
	ret = drm_object_property_set_value(&connector->base, property, val);
898 899 900
	if (ret)
		return ret;

901
	if (property == dev_priv->force_audio_property) {
902
		enum hdmi_force_audio i = val;
903 904 905
		bool has_audio;

		if (i == intel_hdmi->force_audio)
906 907
			return 0;

908
		intel_hdmi->force_audio = i;
909

910
		if (i == HDMI_AUDIO_AUTO)
911 912
			has_audio = intel_hdmi_detect_audio(connector);
		else
913
			has_audio = (i == HDMI_AUDIO_ON);
914

915 916
		if (i == HDMI_AUDIO_OFF_DVI)
			intel_hdmi->has_hdmi_sink = 0;
917

918
		intel_hdmi->has_audio = has_audio;
919 920 921
		goto done;
	}

922
	if (property == dev_priv->broadcast_rgb_property) {
923 924 925 926 927 928 929 930 931 932
		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;
933
			intel_hdmi->color_range = HDMI_COLOR_RANGE_16_235;
934 935 936 937
			break;
		default:
			return -EINVAL;
		}
938 939 940
		goto done;
	}

941 942 943
	return -EINVAL;

done:
944 945
	if (intel_dig_port->base.base.crtc)
		intel_crtc_restore_mode(intel_dig_port->base.base.crtc);
946 947 948 949

	return 0;
}

950 951 952 953
static void intel_hdmi_destroy(struct drm_connector *connector)
{
	drm_sysfs_connector_remove(connector);
	drm_connector_cleanup(connector);
954
	kfree(connector);
955 956 957 958 959 960 961
}

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 = {
962
	.dpms = intel_connector_dpms,
963 964
	.detect = intel_hdmi_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
965
	.set_property = intel_hdmi_set_property,
966 967 968 969 970 971
	.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,
972
	.best_encoder = intel_best_encoder,
973 974 975
};

static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
C
Chris Wilson 已提交
976
	.destroy = intel_encoder_destroy,
977 978
};

979 980 981
static void
intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
{
982
	intel_attach_force_audio_property(connector);
983
	intel_attach_broadcast_rgb_property(connector);
984
	intel_hdmi->color_range_auto = true;
985 986
}

P
Paulo Zanoni 已提交
987 988
void intel_hdmi_init_connector(struct intel_digital_port *intel_dig_port,
			       struct intel_connector *intel_connector)
989
{
990 991 992 993
	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;
994
	struct drm_i915_private *dev_priv = dev->dev_private;
995
	enum port port = intel_dig_port->port;
996

997
	drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
998
			   DRM_MODE_CONNECTOR_HDMIA);
999 1000
	drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);

1001
	connector->interlace_allowed = 1;
1002
	connector->doublescan_allowed = 0;
1003

1004 1005
	switch (port) {
	case PORT_B:
1006
		intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
1007
		intel_encoder->hpd_pin = HPD_PORT_B;
1008 1009
		break;
	case PORT_C:
1010
		intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
1011
		intel_encoder->hpd_pin = HPD_PORT_C;
1012 1013
		break;
	case PORT_D:
1014
		intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
1015
		intel_encoder->hpd_pin = HPD_PORT_D;
1016 1017
		break;
	case PORT_A:
1018
		intel_encoder->hpd_pin = HPD_PORT_A;
1019 1020
		/* Internal port only for eDP. */
	default:
1021
		BUG();
1022
	}
1023

1024
	if (IS_VALLEYVIEW(dev)) {
1025
		intel_hdmi->write_infoframe = vlv_write_infoframe;
1026
		intel_hdmi->set_infoframes = vlv_set_infoframes;
1027 1028 1029
	} else if (!HAS_PCH_SPLIT(dev)) {
		intel_hdmi->write_infoframe = g4x_write_infoframe;
		intel_hdmi->set_infoframes = g4x_set_infoframes;
1030
	} else if (HAS_DDI(dev)) {
1031
		intel_hdmi->write_infoframe = hsw_write_infoframe;
1032
		intel_hdmi->set_infoframes = hsw_set_infoframes;
1033 1034
	} else if (HAS_PCH_IBX(dev)) {
		intel_hdmi->write_infoframe = ibx_write_infoframe;
1035
		intel_hdmi->set_infoframes = ibx_set_infoframes;
1036 1037
	} else {
		intel_hdmi->write_infoframe = cpt_write_infoframe;
1038
		intel_hdmi->set_infoframes = cpt_set_infoframes;
1039
	}
1040

P
Paulo Zanoni 已提交
1041
	if (HAS_DDI(dev))
1042 1043 1044
		intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
	else
		intel_connector->get_hw_state = intel_connector_get_hw_state;
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

	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);
	}
}

1061
void intel_hdmi_init(struct drm_device *dev, int hdmi_reg, enum port port)
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
{
	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 已提交
1083 1084
	drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);

1085
	intel_encoder->compute_config = intel_hdmi_compute_config;
P
Paulo Zanoni 已提交
1086 1087 1088
	intel_encoder->enable = intel_enable_hdmi;
	intel_encoder->disable = intel_disable_hdmi;
	intel_encoder->get_hw_state = intel_hdmi_get_hw_state;
1089

1090 1091 1092
	intel_encoder->type = INTEL_OUTPUT_HDMI;
	intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
	intel_encoder->cloneable = false;
1093

1094
	intel_dig_port->port = port;
1095
	intel_dig_port->hdmi.hdmi_reg = hdmi_reg;
1096
	intel_dig_port->dp.output_reg = 0;
1097

1098
	intel_hdmi_init_connector(intel_dig_port, intel_connector);
1099
}