intel_hdmi.c 30.8 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 353
	avi_if.body.avi.VIC = drm_mode_cea_vic(adjusted_mode);

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 |= COLOR_FORMAT_12bpc;
613
	else
614
		hdmi_val |= 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;
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 624
		hdmi_val |= SDVO_AUDIO_ENABLE;
		hdmi_val |= SDVO_NULL_PACKETS_DURING_VSYNC;
625
		intel_write_eld(encoder, adjusted_mode);
626
	}
627

628
	if (HAS_PCH_CPT(dev))
629
		hdmi_val |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
630
	else if (intel_crtc->pipe == PIPE_B)
631
		hdmi_val |= SDVO_PIPE_B_SELECT;
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_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
665
	u32 temp;
666 667 668 669
	u32 enable_bits = SDVO_ENABLE;

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

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

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

679 680 681
		/* Restore the transcoder select bit. */
		if (pipe == PIPE_B)
			enable_bits |= SDVO_PIPE_B_SELECT;
682 683
	}

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

692 693
	temp |= enable_bits;

694 695
	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);
696 697 698 699 700

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

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;
712
	u32 enable_bits = SDVO_ENABLE | SDVO_AUDIO_ENABLE;
713

714
	temp = I915_READ(intel_hdmi->hdmi_reg);
715 716 717 718 719 720 721 722 723

	/* 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;
724 725
			I915_WRITE(intel_hdmi->hdmi_reg, temp);
			POSTING_READ(intel_hdmi->hdmi_reg);
726 727

			/* Again we need to write this twice. */
728 729
			I915_WRITE(intel_hdmi->hdmi_reg, temp);
			POSTING_READ(intel_hdmi->hdmi_reg);
730 731 732 733 734 735 736 737

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

740 741 742 743
	/* 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)) {
744 745
		I915_WRITE(intel_hdmi->hdmi_reg, temp & ~SDVO_ENABLE);
		POSTING_READ(intel_hdmi->hdmi_reg);
746 747 748
	}

	temp &= ~enable_bits;
749

750 751
	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);
752 753 754 755

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

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)
768
		return MODE_CLOCK_LOW;
769 770 771 772 773 774 775

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

	return MODE_OK;
}

P
Paulo Zanoni 已提交
776 777 778
bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
			   const struct drm_display_mode *mode,
			   struct drm_display_mode *adjusted_mode)
779
{
780 781
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);

782 783 784 785 786 787 788 789 790
	if (intel_hdmi->color_range_auto) {
		/* See CEA-861-E - 5.1 Default Encoding Parameters */
		if (intel_hdmi->has_hdmi_sink &&
		    drm_mode_cea_vic(adjusted_mode) > 1)
			intel_hdmi->color_range = SDVO_COLOR_RANGE_16_235;
		else
			intel_hdmi->color_range = 0;
	}

791 792 793
	if (intel_hdmi->color_range)
		adjusted_mode->private_flags |= INTEL_MODE_LIMITED_COLOR_RANGE;

794 795 796
	return true;
}

797 798
static bool g4x_hdmi_connected(struct intel_hdmi *intel_hdmi)
{
799
	struct drm_device *dev = intel_hdmi_to_dev(intel_hdmi);
800
	struct drm_i915_private *dev_priv = dev->dev_private;
801
	struct intel_digital_port *intel_dig_port = hdmi_to_dig_port(intel_hdmi);
802 803
	uint32_t bit;

804 805
	switch (intel_dig_port->port) {
	case PORT_B:
806
		bit = PORTB_HOTPLUG_LIVE_STATUS;
807
		break;
808
	case PORT_C:
809
		bit = PORTC_HOTPLUG_LIVE_STATUS;
810 811 812 813 814 815 816 817 818
		break;
	default:
		bit = 0;
		break;
	}

	return I915_READ(PORT_HOTPLUG_STAT) & bit;
}

819
static enum drm_connector_status
820
intel_hdmi_detect(struct drm_connector *connector, bool force)
821
{
822
	struct drm_device *dev = connector->dev;
823
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
824 825 826
	struct intel_digital_port *intel_dig_port =
		hdmi_to_dig_port(intel_hdmi);
	struct intel_encoder *intel_encoder = &intel_dig_port->base;
827
	struct drm_i915_private *dev_priv = dev->dev_private;
828
	struct edid *edid;
829
	enum drm_connector_status status = connector_status_disconnected;
830

831 832

	if (IS_G4X(dev) && !g4x_hdmi_connected(intel_hdmi))
833
		return status;
834 835 836
	else if (HAS_PCH_SPLIT(dev) &&
		 !ibx_digital_port_connected(dev_priv, intel_dig_port))
		 return status;
837

C
Chris Wilson 已提交
838
	intel_hdmi->has_hdmi_sink = false;
839
	intel_hdmi->has_audio = false;
840
	intel_hdmi->rgb_quant_range_selectable = false;
841
	edid = drm_get_edid(connector,
842 843
			    intel_gmbus_get_adapter(dev_priv,
						    intel_hdmi->ddc_bus));
844

845
	if (edid) {
846
		if (edid->input & DRM_EDID_INPUT_DIGITAL) {
847
			status = connector_status_connected;
848 849 850
			if (intel_hdmi->force_audio != HDMI_AUDIO_OFF_DVI)
				intel_hdmi->has_hdmi_sink =
						drm_detect_hdmi_monitor(edid);
851
			intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
852 853
			intel_hdmi->rgb_quant_range_selectable =
				drm_rgb_quant_range_selectable(edid);
854 855
		}
		kfree(edid);
856
	}
857

858
	if (status == connector_status_connected) {
859 860 861
		if (intel_hdmi->force_audio != HDMI_AUDIO_AUTO)
			intel_hdmi->has_audio =
				(intel_hdmi->force_audio == HDMI_AUDIO_ON);
862
		intel_encoder->type = INTEL_OUTPUT_HDMI;
863 864
	}

865
	return status;
866 867 868 869
}

static int intel_hdmi_get_modes(struct drm_connector *connector)
{
870
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
871
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
872 873 874 875 876

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

877
	return intel_ddc_get_modes(connector,
878 879
				   intel_gmbus_get_adapter(dev_priv,
							   intel_hdmi->ddc_bus));
880 881
}

882 883 884 885 886 887 888 889 890
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,
891 892
			    intel_gmbus_get_adapter(dev_priv,
						    intel_hdmi->ddc_bus));
893 894 895 896 897 898 899 900 901
	if (edid) {
		if (edid->input & DRM_EDID_INPUT_DIGITAL)
			has_audio = drm_detect_monitor_audio(edid);
		kfree(edid);
	}

	return has_audio;
}

902 903
static int
intel_hdmi_set_property(struct drm_connector *connector,
904 905
			struct drm_property *property,
			uint64_t val)
906 907
{
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
908 909
	struct intel_digital_port *intel_dig_port =
		hdmi_to_dig_port(intel_hdmi);
910
	struct drm_i915_private *dev_priv = connector->dev->dev_private;
911 912
	int ret;

913
	ret = drm_object_property_set_value(&connector->base, property, val);
914 915 916
	if (ret)
		return ret;

917
	if (property == dev_priv->force_audio_property) {
918
		enum hdmi_force_audio i = val;
919 920 921
		bool has_audio;

		if (i == intel_hdmi->force_audio)
922 923
			return 0;

924
		intel_hdmi->force_audio = i;
925

926
		if (i == HDMI_AUDIO_AUTO)
927 928
			has_audio = intel_hdmi_detect_audio(connector);
		else
929
			has_audio = (i == HDMI_AUDIO_ON);
930

931 932
		if (i == HDMI_AUDIO_OFF_DVI)
			intel_hdmi->has_hdmi_sink = 0;
933

934
		intel_hdmi->has_audio = has_audio;
935 936 937
		goto done;
	}

938
	if (property == dev_priv->broadcast_rgb_property) {
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
		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;
			intel_hdmi->color_range = SDVO_COLOR_RANGE_16_235;
			break;
		default:
			return -EINVAL;
		}
954 955 956
		goto done;
	}

957 958 959
	return -EINVAL;

done:
960 961
	if (intel_dig_port->base.base.crtc)
		intel_crtc_restore_mode(intel_dig_port->base.base.crtc);
962 963 964 965

	return 0;
}

966 967 968 969
static void intel_hdmi_destroy(struct drm_connector *connector)
{
	drm_sysfs_connector_remove(connector);
	drm_connector_cleanup(connector);
970
	kfree(connector);
971 972 973 974 975
}

static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
	.mode_fixup = intel_hdmi_mode_fixup,
	.mode_set = intel_hdmi_mode_set,
976
	.disable = intel_encoder_noop,
977 978 979
};

static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
980
	.dpms = intel_connector_dpms,
981 982
	.detect = intel_hdmi_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
983
	.set_property = intel_hdmi_set_property,
984 985 986 987 988 989
	.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,
990
	.best_encoder = intel_best_encoder,
991 992 993
};

static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
C
Chris Wilson 已提交
994
	.destroy = intel_encoder_destroy,
995 996
};

997 998 999
static void
intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
{
1000
	intel_attach_force_audio_property(connector);
1001
	intel_attach_broadcast_rgb_property(connector);
1002
	intel_hdmi->color_range_auto = true;
1003 1004
}

P
Paulo Zanoni 已提交
1005 1006
void intel_hdmi_init_connector(struct intel_digital_port *intel_dig_port,
			       struct intel_connector *intel_connector)
1007
{
1008 1009 1010 1011
	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;
1012
	struct drm_i915_private *dev_priv = dev->dev_private;
1013
	enum port port = intel_dig_port->port;
1014

1015
	drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
1016
			   DRM_MODE_CONNECTOR_HDMIA);
1017 1018
	drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);

1019
	connector->polled = DRM_CONNECTOR_POLL_HPD;
1020
	connector->interlace_allowed = 1;
1021
	connector->doublescan_allowed = 0;
1022

1023 1024
	switch (port) {
	case PORT_B:
1025
		intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
1026
		dev_priv->hotplug_supported_mask |= PORTB_HOTPLUG_INT_STATUS;
1027 1028
		break;
	case PORT_C:
1029
		intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
1030
		dev_priv->hotplug_supported_mask |= PORTC_HOTPLUG_INT_STATUS;
1031 1032
		break;
	case PORT_D:
1033
		intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
1034
		dev_priv->hotplug_supported_mask |= PORTD_HOTPLUG_INT_STATUS;
1035 1036 1037 1038
		break;
	case PORT_A:
		/* Internal port only for eDP. */
	default:
1039
		BUG();
1040
	}
1041

1042
	if (!HAS_PCH_SPLIT(dev)) {
1043
		intel_hdmi->write_infoframe = g4x_write_infoframe;
1044
		intel_hdmi->set_infoframes = g4x_set_infoframes;
1045 1046
	} else if (IS_VALLEYVIEW(dev)) {
		intel_hdmi->write_infoframe = vlv_write_infoframe;
1047
		intel_hdmi->set_infoframes = vlv_set_infoframes;
1048
	} else if (HAS_DDI(dev)) {
1049
		intel_hdmi->write_infoframe = hsw_write_infoframe;
1050
		intel_hdmi->set_infoframes = hsw_set_infoframes;
1051 1052
	} else if (HAS_PCH_IBX(dev)) {
		intel_hdmi->write_infoframe = ibx_write_infoframe;
1053
		intel_hdmi->set_infoframes = ibx_set_infoframes;
1054 1055
	} else {
		intel_hdmi->write_infoframe = cpt_write_infoframe;
1056
		intel_hdmi->set_infoframes = cpt_set_infoframes;
1057
	}
1058

P
Paulo Zanoni 已提交
1059
	if (HAS_DDI(dev))
1060 1061 1062
		intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
	else
		intel_connector->get_hw_state = intel_connector_get_hw_state;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078

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

1079
void intel_hdmi_init(struct drm_device *dev, int hdmi_reg, enum port port)
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
{
	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 已提交
1101 1102 1103 1104 1105
	drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);

	intel_encoder->enable = intel_enable_hdmi;
	intel_encoder->disable = intel_disable_hdmi;
	intel_encoder->get_hw_state = intel_hdmi_get_hw_state;
1106

1107 1108 1109
	intel_encoder->type = INTEL_OUTPUT_HDMI;
	intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
	intel_encoder->cloneable = false;
1110

1111
	intel_dig_port->port = port;
1112
	intel_dig_port->hdmi.hdmi_reg = hdmi_reg;
1113
	intel_dig_port->dp.output_reg = 0;
1114

1115
	intel_hdmi_init_connector(intel_dig_port, intel_connector);
1116
}