intel_hdmi.c 30.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->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 337 338 339 340 341
	struct dip_infoframe avi_if = {
		.type = DIP_TYPE_AVI,
		.ver = DIP_VERSION_AVI,
		.len = DIP_LEN_AVI,
	};

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

345 346 347 348 349 350 351
	if (intel_hdmi->rgb_quant_range_selectable) {
		if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
			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;
	}

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

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

357
static void intel_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
358 359 360 361 362 363 364 365 366 367 368 369 370 371
{
	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);
}

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

382 383
	assert_hdmi_port_disabled(intel_hdmi);

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	/* 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);
400
		POSTING_READ(reg);
401 402 403
		return;
	}

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

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

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

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

432 433 434 435 436 437 438
	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)
{
439 440
	struct drm_i915_private *dev_priv = encoder->dev->dev_private;
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
441 442
	struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
	struct intel_hdmi *intel_hdmi = &intel_dig_port->hdmi;
443 444
	u32 reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
	u32 val = I915_READ(reg);
445
	u32 port;
446

447 448
	assert_hdmi_port_disabled(intel_hdmi);

449 450 451 452 453 454 455 456
	/* 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);
457
		POSTING_READ(reg);
458 459 460
		return;
	}

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

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

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

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

493 494 495 496 497 498 499
	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)
{
500 501 502 503 504 505
	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);

506 507
	assert_hdmi_port_disabled(intel_hdmi);

508 509 510 511 512 513 514 515
	/* 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);
516
		POSTING_READ(reg);
517 518 519
		return;
	}

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

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

528 529 530 531 532 533 534
	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)
{
535 536 537 538 539 540
	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);

541 542
	assert_hdmi_port_disabled(intel_hdmi);

543 544 545 546 547 548 549 550
	/* 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);
551
		POSTING_READ(reg);
552 553 554
		return;
	}

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

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

562 563 564 565 566 567 568
	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)
{
569 570 571
	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);
572
	u32 reg = HSW_TVIDEO_DIP_CTL(intel_crtc->cpu_transcoder);
573
	u32 val = I915_READ(reg);
574

575 576
	assert_hdmi_port_disabled(intel_hdmi);

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

583 584 585 586
	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);
587
	POSTING_READ(reg);
588

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

593 594 595 596 597 598
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;
599
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
C
Chris Wilson 已提交
600
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
601
	u32 hdmi_val;
602

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

611
	if (intel_crtc->bpp > 24)
612
		hdmi_val |= HDMI_COLOR_FORMAT_12bpc;
613
	else
614
		hdmi_val |= SDVO_COLOR_FORMAT_8bpc;
615

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

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

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

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

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

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

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

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

660
static void intel_enable_hdmi(struct intel_encoder *encoder)
661
{
662
	struct drm_device *dev = encoder->base.dev;
663
	struct drm_i915_private *dev_priv = dev->dev_private;
664
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
665
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
666
	u32 temp;
667 668 669 670
	u32 enable_bits = SDVO_ENABLE;

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

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

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

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

687 688
	temp |= enable_bits;

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

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

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

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

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

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

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

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

	temp &= ~enable_bits;
744

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

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

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

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

	return MODE_OK;
}

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

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

787 788 789
	if (intel_hdmi->color_range)
		adjusted_mode->private_flags |= INTEL_MODE_LIMITED_COLOR_RANGE;

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

793 794 795
	return true;
}

796
static enum drm_connector_status
797
intel_hdmi_detect(struct drm_connector *connector, bool force)
798
{
799
	struct drm_device *dev = connector->dev;
800
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
801 802 803
	struct intel_digital_port *intel_dig_port =
		hdmi_to_dig_port(intel_hdmi);
	struct intel_encoder *intel_encoder = &intel_dig_port->base;
804
	struct drm_i915_private *dev_priv = dev->dev_private;
805
	struct edid *edid;
806
	enum drm_connector_status status = connector_status_disconnected;
807

C
Chris Wilson 已提交
808
	intel_hdmi->has_hdmi_sink = false;
809
	intel_hdmi->has_audio = false;
810
	intel_hdmi->rgb_quant_range_selectable = false;
811
	edid = drm_get_edid(connector,
812 813
			    intel_gmbus_get_adapter(dev_priv,
						    intel_hdmi->ddc_bus));
814

815
	if (edid) {
816
		if (edid->input & DRM_EDID_INPUT_DIGITAL) {
817
			status = connector_status_connected;
818 819 820
			if (intel_hdmi->force_audio != HDMI_AUDIO_OFF_DVI)
				intel_hdmi->has_hdmi_sink =
						drm_detect_hdmi_monitor(edid);
821
			intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
822 823
			intel_hdmi->rgb_quant_range_selectable =
				drm_rgb_quant_range_selectable(edid);
824 825
		}
		kfree(edid);
826
	}
827

828
	if (status == connector_status_connected) {
829 830 831
		if (intel_hdmi->force_audio != HDMI_AUDIO_AUTO)
			intel_hdmi->has_audio =
				(intel_hdmi->force_audio == HDMI_AUDIO_ON);
832
		intel_encoder->type = INTEL_OUTPUT_HDMI;
833 834
	}

835
	return status;
836 837 838 839
}

static int intel_hdmi_get_modes(struct drm_connector *connector)
{
840
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
841
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
842 843 844 845 846

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

847
	return intel_ddc_get_modes(connector,
848 849
				   intel_gmbus_get_adapter(dev_priv,
							   intel_hdmi->ddc_bus));
850 851
}

852 853 854 855 856 857 858 859 860
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,
861 862
			    intel_gmbus_get_adapter(dev_priv,
						    intel_hdmi->ddc_bus));
863 864 865 866 867 868 869 870 871
	if (edid) {
		if (edid->input & DRM_EDID_INPUT_DIGITAL)
			has_audio = drm_detect_monitor_audio(edid);
		kfree(edid);
	}

	return has_audio;
}

872 873
static int
intel_hdmi_set_property(struct drm_connector *connector,
874 875
			struct drm_property *property,
			uint64_t val)
876 877
{
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
878 879
	struct intel_digital_port *intel_dig_port =
		hdmi_to_dig_port(intel_hdmi);
880
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
881 882
	int ret;

883
	ret = drm_object_property_set_value(&connector->base, property, val);
884 885 886
	if (ret)
		return ret;

887
	if (property == dev_priv->force_audio_property) {
888
		enum hdmi_force_audio i = val;
889 890 891
		bool has_audio;

		if (i == intel_hdmi->force_audio)
892 893
			return 0;

894
		intel_hdmi->force_audio = i;
895

896
		if (i == HDMI_AUDIO_AUTO)
897 898
			has_audio = intel_hdmi_detect_audio(connector);
		else
899
			has_audio = (i == HDMI_AUDIO_ON);
900

901 902
		if (i == HDMI_AUDIO_OFF_DVI)
			intel_hdmi->has_hdmi_sink = 0;
903

904
		intel_hdmi->has_audio = has_audio;
905 906 907
		goto done;
	}

908
	if (property == dev_priv->broadcast_rgb_property) {
909 910 911 912 913 914 915 916 917 918
		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;
919
			intel_hdmi->color_range = HDMI_COLOR_RANGE_16_235;
920 921 922 923
			break;
		default:
			return -EINVAL;
		}
924 925 926
		goto done;
	}

927 928 929
	return -EINVAL;

done:
930 931
	if (intel_dig_port->base.base.crtc)
		intel_crtc_restore_mode(intel_dig_port->base.base.crtc);
932 933 934 935

	return 0;
}

936 937 938 939
static void intel_hdmi_destroy(struct drm_connector *connector)
{
	drm_sysfs_connector_remove(connector);
	drm_connector_cleanup(connector);
940
	kfree(connector);
941 942 943 944 945 946 947
}

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 = {
948
	.dpms = intel_connector_dpms,
949 950
	.detect = intel_hdmi_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
951
	.set_property = intel_hdmi_set_property,
952 953 954 955 956 957
	.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,
958
	.best_encoder = intel_best_encoder,
959 960 961
};

static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
C
Chris Wilson 已提交
962
	.destroy = intel_encoder_destroy,
963 964
};

965 966 967
static void
intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
{
968
	intel_attach_force_audio_property(connector);
969
	intel_attach_broadcast_rgb_property(connector);
970
	intel_hdmi->color_range_auto = true;
971 972
}

P
Paulo Zanoni 已提交
973 974
void intel_hdmi_init_connector(struct intel_digital_port *intel_dig_port,
			       struct intel_connector *intel_connector)
975
{
976 977 978 979
	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;
980
	struct drm_i915_private *dev_priv = dev->dev_private;
981
	enum port port = intel_dig_port->port;
982

983
	drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
984
			   DRM_MODE_CONNECTOR_HDMIA);
985 986
	drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);

987
	connector->polled = DRM_CONNECTOR_POLL_HPD;
988
	connector->interlace_allowed = 1;
989
	connector->doublescan_allowed = 0;
990

991 992
	switch (port) {
	case PORT_B:
993
		intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
994
		intel_encoder->hpd_pin = HPD_PORT_B;
995 996
		break;
	case PORT_C:
997
		intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
998
		intel_encoder->hpd_pin = HPD_PORT_C;
999 1000
		break;
	case PORT_D:
1001
		intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
1002
		intel_encoder->hpd_pin = HPD_PORT_D;
1003 1004
		break;
	case PORT_A:
1005
		intel_encoder->hpd_pin = HPD_PORT_A;
1006 1007
		/* Internal port only for eDP. */
	default:
1008
		BUG();
1009
	}
1010

1011
	if (IS_VALLEYVIEW(dev)) {
1012
		intel_hdmi->write_infoframe = vlv_write_infoframe;
1013
		intel_hdmi->set_infoframes = vlv_set_infoframes;
1014 1015 1016
	} else if (!HAS_PCH_SPLIT(dev)) {
		intel_hdmi->write_infoframe = g4x_write_infoframe;
		intel_hdmi->set_infoframes = g4x_set_infoframes;
1017
	} else if (HAS_DDI(dev)) {
1018
		intel_hdmi->write_infoframe = hsw_write_infoframe;
1019
		intel_hdmi->set_infoframes = hsw_set_infoframes;
1020 1021
	} else if (HAS_PCH_IBX(dev)) {
		intel_hdmi->write_infoframe = ibx_write_infoframe;
1022
		intel_hdmi->set_infoframes = ibx_set_infoframes;
1023 1024
	} else {
		intel_hdmi->write_infoframe = cpt_write_infoframe;
1025
		intel_hdmi->set_infoframes = cpt_set_infoframes;
1026
	}
1027

P
Paulo Zanoni 已提交
1028
	if (HAS_DDI(dev))
1029 1030 1031
		intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
	else
		intel_connector->get_hw_state = intel_connector_get_hw_state;
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

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

1048
void intel_hdmi_init(struct drm_device *dev, int hdmi_reg, enum port port)
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
{
	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 已提交
1070 1071
	drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);

1072
	intel_encoder->compute_config = intel_hdmi_compute_config;
P
Paulo Zanoni 已提交
1073 1074 1075
	intel_encoder->enable = intel_enable_hdmi;
	intel_encoder->disable = intel_disable_hdmi;
	intel_encoder->get_hw_state = intel_hdmi_get_hw_state;
1076

1077 1078 1079
	intel_encoder->type = INTEL_OUTPUT_HDMI;
	intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
	intel_encoder->cloneable = false;
1080

1081
	intel_dig_port->port = port;
1082
	intel_dig_port->hdmi.hdmi_reg = hdmi_reg;
1083
	intel_dig_port->dp.output_reg = 0;
1084

1085
	intel_hdmi_init_connector(intel_dig_port, intel_connector);
1086
}