intel_hdmi.c 70.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
#include <linux/hdmi.h>
33
#include <drm/drmP.h>
34
#include <drm/drm_atomic_helper.h>
35 36
#include <drm/drm_crtc.h>
#include <drm/drm_edid.h>
S
Sean Paul 已提交
37
#include <drm/drm_hdcp.h>
S
Shashank Sharma 已提交
38
#include <drm/drm_scdc_helper.h>
39
#include "intel_drv.h"
40
#include <drm/i915_drm.h>
41
#include <drm/intel_lpe_audio.h>
42 43
#include "i915_drv.h"

44 45
static struct drm_device *intel_hdmi_to_dev(struct intel_hdmi *intel_hdmi)
{
46
	return hdmi_to_dig_port(intel_hdmi)->base.base.dev;
47 48
}

49 50 51
static void
assert_hdmi_port_disabled(struct intel_hdmi *intel_hdmi)
{
52
	struct drm_device *dev = intel_hdmi_to_dev(intel_hdmi);
53
	struct drm_i915_private *dev_priv = to_i915(dev);
54 55
	uint32_t enabled_bits;

56
	enabled_bits = HAS_DDI(dev_priv) ? DDI_BUF_CTL_ENABLE : SDVO_ENABLE;
57

58
	WARN(I915_READ(intel_hdmi->hdmi_reg) & enabled_bits,
59 60 61
	     "HDMI port enabled, expecting disabled\n");
}

62
struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
C
Chris Wilson 已提交
63
{
64 65 66
	struct intel_digital_port *intel_dig_port =
		container_of(encoder, struct intel_digital_port, base.base);
	return &intel_dig_port->hdmi;
C
Chris Wilson 已提交
67 68
}

69 70
static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
{
71
	return enc_to_intel_hdmi(&intel_attached_encoder(connector)->base);
72 73
}

74
static u32 g4x_infoframe_index(unsigned int type)
75
{
76 77
	switch (type) {
	case HDMI_INFOFRAME_TYPE_AVI:
78
		return VIDEO_DIP_SELECT_AVI;
79
	case HDMI_INFOFRAME_TYPE_SPD:
80
		return VIDEO_DIP_SELECT_SPD;
81 82
	case HDMI_INFOFRAME_TYPE_VENDOR:
		return VIDEO_DIP_SELECT_VENDOR;
83
	default:
84
		MISSING_CASE(type);
85
		return 0;
86 87 88
	}
}

89
static u32 g4x_infoframe_enable(unsigned int type)
90
{
91 92
	switch (type) {
	case HDMI_INFOFRAME_TYPE_AVI:
93
		return VIDEO_DIP_ENABLE_AVI;
94
	case HDMI_INFOFRAME_TYPE_SPD:
95
		return VIDEO_DIP_ENABLE_SPD;
96 97
	case HDMI_INFOFRAME_TYPE_VENDOR:
		return VIDEO_DIP_ENABLE_VENDOR;
98
	default:
99
		MISSING_CASE(type);
100
		return 0;
101 102 103
	}
}

104
static u32 hsw_infoframe_enable(unsigned int type)
105
{
106
	switch (type) {
107 108
	case DP_SDP_VSC:
		return VIDEO_DIP_ENABLE_VSC_HSW;
109
	case HDMI_INFOFRAME_TYPE_AVI:
110
		return VIDEO_DIP_ENABLE_AVI_HSW;
111
	case HDMI_INFOFRAME_TYPE_SPD:
112
		return VIDEO_DIP_ENABLE_SPD_HSW;
113 114
	case HDMI_INFOFRAME_TYPE_VENDOR:
		return VIDEO_DIP_ENABLE_VS_HSW;
115
	default:
116
		MISSING_CASE(type);
117 118 119 120
		return 0;
	}
}

121 122 123
static i915_reg_t
hsw_dip_data_reg(struct drm_i915_private *dev_priv,
		 enum transcoder cpu_transcoder,
124
		 unsigned int type,
125
		 int i)
126
{
127
	switch (type) {
128 129
	case DP_SDP_VSC:
		return HSW_TVIDEO_DIP_VSC_DATA(cpu_transcoder, i);
130
	case HDMI_INFOFRAME_TYPE_AVI:
131
		return HSW_TVIDEO_DIP_AVI_DATA(cpu_transcoder, i);
132
	case HDMI_INFOFRAME_TYPE_SPD:
133
		return HSW_TVIDEO_DIP_SPD_DATA(cpu_transcoder, i);
134
	case HDMI_INFOFRAME_TYPE_VENDOR:
135
		return HSW_TVIDEO_DIP_VS_DATA(cpu_transcoder, i);
136
	default:
137
		MISSING_CASE(type);
138
		return INVALID_MMIO_REG;
139 140 141
	}
}

142
static void g4x_write_infoframe(struct drm_encoder *encoder,
143
				const struct intel_crtc_state *crtc_state,
144
				unsigned int type,
145
				const void *frame, ssize_t len)
146
{
147
	const uint32_t *data = frame;
148
	struct drm_device *dev = encoder->dev;
149
	struct drm_i915_private *dev_priv = to_i915(dev);
150
	u32 val = I915_READ(VIDEO_DIP_CTL);
151
	int i;
152

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

155
	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
156
	val |= g4x_infoframe_index(type);
157

158
	val &= ~g4x_infoframe_enable(type);
159

160
	I915_WRITE(VIDEO_DIP_CTL, val);
161

162
	mmiowb();
163
	for (i = 0; i < len; i += 4) {
164 165 166
		I915_WRITE(VIDEO_DIP_DATA, *data);
		data++;
	}
167 168 169
	/* Write every possible data byte to force correct ECC calculation. */
	for (; i < VIDEO_DIP_DATA_SIZE; i += 4)
		I915_WRITE(VIDEO_DIP_DATA, 0);
170
	mmiowb();
171

172
	val |= g4x_infoframe_enable(type);
173
	val &= ~VIDEO_DIP_FREQ_MASK;
174
	val |= VIDEO_DIP_FREQ_VSYNC;
175

176
	I915_WRITE(VIDEO_DIP_CTL, val);
177
	POSTING_READ(VIDEO_DIP_CTL);
178 179
}

180 181
static bool g4x_infoframe_enabled(struct drm_encoder *encoder,
				  const struct intel_crtc_state *pipe_config)
182
{
183
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
184
	struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
185 186
	u32 val = I915_READ(VIDEO_DIP_CTL);

187 188
	if ((val & VIDEO_DIP_ENABLE) == 0)
		return false;
189

190
	if ((val & VIDEO_DIP_PORT_MASK) != VIDEO_DIP_PORT(intel_dig_port->base.port))
191 192 193 194
		return false;

	return val & (VIDEO_DIP_ENABLE_AVI |
		      VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_SPD);
195 196
}

197
static void ibx_write_infoframe(struct drm_encoder *encoder,
198
				const struct intel_crtc_state *crtc_state,
199
				unsigned int type,
200
				const void *frame, ssize_t len)
201
{
202
	const uint32_t *data = frame;
203
	struct drm_device *dev = encoder->dev;
204
	struct drm_i915_private *dev_priv = to_i915(dev);
205
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
206
	i915_reg_t reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
207
	u32 val = I915_READ(reg);
208
	int i;
209

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

212
	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
213
	val |= g4x_infoframe_index(type);
214

215
	val &= ~g4x_infoframe_enable(type);
216 217 218

	I915_WRITE(reg, val);

219
	mmiowb();
220 221 222 223
	for (i = 0; i < len; i += 4) {
		I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
		data++;
	}
224 225 226
	/* 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);
227
	mmiowb();
228

229
	val |= g4x_infoframe_enable(type);
230
	val &= ~VIDEO_DIP_FREQ_MASK;
231
	val |= VIDEO_DIP_FREQ_VSYNC;
232 233

	I915_WRITE(reg, val);
234
	POSTING_READ(reg);
235 236
}

237 238
static bool ibx_infoframe_enabled(struct drm_encoder *encoder,
				  const struct intel_crtc_state *pipe_config)
239
{
240
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
241
	struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
242 243
	enum pipe pipe = to_intel_crtc(pipe_config->base.crtc)->pipe;
	i915_reg_t reg = TVIDEO_DIP_CTL(pipe);
244 245
	u32 val = I915_READ(reg);

246 247 248
	if ((val & VIDEO_DIP_ENABLE) == 0)
		return false;

249
	if ((val & VIDEO_DIP_PORT_MASK) != VIDEO_DIP_PORT(intel_dig_port->base.port))
250
		return false;
251

252 253 254
	return val & (VIDEO_DIP_ENABLE_AVI |
		      VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
		      VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
255 256
}

257
static void cpt_write_infoframe(struct drm_encoder *encoder,
258
				const struct intel_crtc_state *crtc_state,
259
				unsigned int type,
260
				const void *frame, ssize_t len)
261
{
262
	const uint32_t *data = frame;
263
	struct drm_device *dev = encoder->dev;
264
	struct drm_i915_private *dev_priv = to_i915(dev);
265
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
266
	i915_reg_t reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
267
	u32 val = I915_READ(reg);
268
	int i;
269

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

272
	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
273
	val |= g4x_infoframe_index(type);
274

275 276
	/* The DIP control register spec says that we need to update the AVI
	 * infoframe without clearing its enable bit */
277 278
	if (type != HDMI_INFOFRAME_TYPE_AVI)
		val &= ~g4x_infoframe_enable(type);
279

280
	I915_WRITE(reg, val);
281

282
	mmiowb();
283
	for (i = 0; i < len; i += 4) {
284 285 286
		I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
		data++;
	}
287 288 289
	/* 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);
290
	mmiowb();
291

292
	val |= g4x_infoframe_enable(type);
293
	val &= ~VIDEO_DIP_FREQ_MASK;
294
	val |= VIDEO_DIP_FREQ_VSYNC;
295

296
	I915_WRITE(reg, val);
297
	POSTING_READ(reg);
298
}
299

300 301
static bool cpt_infoframe_enabled(struct drm_encoder *encoder,
				  const struct intel_crtc_state *pipe_config)
302
{
303 304 305
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
	enum pipe pipe = to_intel_crtc(pipe_config->base.crtc)->pipe;
	u32 val = I915_READ(TVIDEO_DIP_CTL(pipe));
306

307 308 309 310 311 312
	if ((val & VIDEO_DIP_ENABLE) == 0)
		return false;

	return val & (VIDEO_DIP_ENABLE_AVI |
		      VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
		      VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
313 314
}

315
static void vlv_write_infoframe(struct drm_encoder *encoder,
316
				const struct intel_crtc_state *crtc_state,
317
				unsigned int type,
318
				const void *frame, ssize_t len)
319
{
320
	const uint32_t *data = frame;
321
	struct drm_device *dev = encoder->dev;
322
	struct drm_i915_private *dev_priv = to_i915(dev);
323
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
324
	i915_reg_t reg = VLV_TVIDEO_DIP_CTL(intel_crtc->pipe);
325
	u32 val = I915_READ(reg);
326
	int i;
327

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

330
	val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
331
	val |= g4x_infoframe_index(type);
332

333
	val &= ~g4x_infoframe_enable(type);
334

335
	I915_WRITE(reg, val);
336

337
	mmiowb();
338 339 340 341
	for (i = 0; i < len; i += 4) {
		I915_WRITE(VLV_TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
		data++;
	}
342 343 344
	/* 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);
345
	mmiowb();
346

347
	val |= g4x_infoframe_enable(type);
348
	val &= ~VIDEO_DIP_FREQ_MASK;
349
	val |= VIDEO_DIP_FREQ_VSYNC;
350

351
	I915_WRITE(reg, val);
352
	POSTING_READ(reg);
353 354
}

355 356
static bool vlv_infoframe_enabled(struct drm_encoder *encoder,
				  const struct intel_crtc_state *pipe_config)
357
{
358
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
359
	struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
360 361
	enum pipe pipe = to_intel_crtc(pipe_config->base.crtc)->pipe;
	u32 val = I915_READ(VLV_TVIDEO_DIP_CTL(pipe));
362

363 364 365
	if ((val & VIDEO_DIP_ENABLE) == 0)
		return false;

366
	if ((val & VIDEO_DIP_PORT_MASK) != VIDEO_DIP_PORT(intel_dig_port->base.port))
367
		return false;
368

369 370 371
	return val & (VIDEO_DIP_ENABLE_AVI |
		      VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
		      VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
372 373
}

374
static void hsw_write_infoframe(struct drm_encoder *encoder,
375
				const struct intel_crtc_state *crtc_state,
376
				unsigned int type,
377
				const void *frame, ssize_t len)
378
{
379
	const uint32_t *data = frame;
380
	struct drm_device *dev = encoder->dev;
381
	struct drm_i915_private *dev_priv = to_i915(dev);
382
	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
383 384
	i915_reg_t ctl_reg = HSW_TVIDEO_DIP_CTL(cpu_transcoder);
	i915_reg_t data_reg;
385 386
	int data_size = type == DP_SDP_VSC ?
		VIDEO_DIP_VSC_DATA_SIZE : VIDEO_DIP_DATA_SIZE;
387
	int i;
388
	u32 val = I915_READ(ctl_reg);
389

390
	data_reg = hsw_dip_data_reg(dev_priv, cpu_transcoder, type, 0);
391

392
	val &= ~hsw_infoframe_enable(type);
393 394
	I915_WRITE(ctl_reg, val);

395
	mmiowb();
396
	for (i = 0; i < len; i += 4) {
397 398
		I915_WRITE(hsw_dip_data_reg(dev_priv, cpu_transcoder,
					    type, i >> 2), *data);
399 400
		data++;
	}
401
	/* Write every possible data byte to force correct ECC calculation. */
402
	for (; i < data_size; i += 4)
403 404
		I915_WRITE(hsw_dip_data_reg(dev_priv, cpu_transcoder,
					    type, i >> 2), 0);
405
	mmiowb();
406

407
	val |= hsw_infoframe_enable(type);
408
	I915_WRITE(ctl_reg, val);
409
	POSTING_READ(ctl_reg);
410 411
}

412 413
static bool hsw_infoframe_enabled(struct drm_encoder *encoder,
				  const struct intel_crtc_state *pipe_config)
414
{
415 416
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
	u32 val = I915_READ(HSW_TVIDEO_DIP_CTL(pipe_config->cpu_transcoder));
417

418 419 420
	return val & (VIDEO_DIP_ENABLE_VSC_HSW | VIDEO_DIP_ENABLE_AVI_HSW |
		      VIDEO_DIP_ENABLE_GCP_HSW | VIDEO_DIP_ENABLE_VS_HSW |
		      VIDEO_DIP_ENABLE_GMP_HSW | VIDEO_DIP_ENABLE_SPD_HSW);
421 422
}

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
/*
 * The data we write to the DIP data buffer registers is 1 byte bigger than the
 * HDMI infoframe size because of an ECC/reserved byte at position 3 (starting
 * at 0). It's also a byte used by DisplayPort so the same DIP registers can be
 * used for both technologies.
 *
 * DW0: Reserved/ECC/DP | HB2 | HB1 | HB0
 * DW1:       DB3       | DB2 | DB1 | DB0
 * DW2:       DB7       | DB6 | DB5 | DB4
 * DW3: ...
 *
 * (HB is Header Byte, DB is Data Byte)
 *
 * The hdmi pack() functions don't know about that hardware specific hole so we
 * trick them by giving an offset into the buffer and moving back the header
 * bytes by one.
 */
440
static void intel_write_infoframe(struct drm_encoder *encoder,
441
				  const struct intel_crtc_state *crtc_state,
442
				  union hdmi_infoframe *frame)
443
{
444
	struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
445 446
	uint8_t buffer[VIDEO_DIP_DATA_SIZE];
	ssize_t len;
447

448 449 450 451 452 453 454 455 456 457 458
	/* see comment above for the reason for this offset */
	len = hdmi_infoframe_pack(frame, buffer + 1, sizeof(buffer) - 1);
	if (len < 0)
		return;

	/* Insert the 'hole' (see big comment above) at position 3 */
	buffer[0] = buffer[1];
	buffer[1] = buffer[2];
	buffer[2] = buffer[3];
	buffer[3] = 0;
	len++;
459

460
	intel_dig_port->write_infoframe(encoder, crtc_state, frame->any.type, buffer, len);
461 462
}

463
static void intel_hdmi_set_avi_infoframe(struct drm_encoder *encoder,
464
					 const struct intel_crtc_state *crtc_state)
465
{
466
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
467 468
	const struct drm_display_mode *adjusted_mode =
		&crtc_state->base.adjusted_mode;
469 470
	struct drm_connector *connector = &intel_hdmi->attached_connector->base;
	bool is_hdmi2_sink = connector->display_info.hdmi.scdc.supported;
471 472
	union hdmi_infoframe frame;
	int ret;
473

474
	ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
475 476
						       adjusted_mode,
						       is_hdmi2_sink);
477 478 479 480
	if (ret < 0) {
		DRM_ERROR("couldn't fill AVI infoframe\n");
		return;
	}
P
Paulo Zanoni 已提交
481

482 483 484 485 486
	if (crtc_state->ycbcr420)
		frame.avi.colorspace = HDMI_COLORSPACE_YUV420;
	else
		frame.avi.colorspace = HDMI_COLORSPACE_RGB;

487
	drm_hdmi_avi_infoframe_quant_range(&frame.avi, adjusted_mode,
488 489 490
					   crtc_state->limited_color_range ?
					   HDMI_QUANTIZATION_RANGE_LIMITED :
					   HDMI_QUANTIZATION_RANGE_FULL,
491 492
					   intel_hdmi->rgb_quant_range_selectable,
					   is_hdmi2_sink);
493

494
	/* TODO: handle pixel repetition for YCBCR420 outputs */
495
	intel_write_infoframe(encoder, crtc_state, &frame);
496 497
}

498 499
static void intel_hdmi_set_spd_infoframe(struct drm_encoder *encoder,
					 const struct intel_crtc_state *crtc_state)
500
{
501 502 503 504 505 506 507 508
	union hdmi_infoframe frame;
	int ret;

	ret = hdmi_spd_infoframe_init(&frame.spd, "Intel", "Integrated gfx");
	if (ret < 0) {
		DRM_ERROR("couldn't fill SPD infoframe\n");
		return;
	}
509

510
	frame.spd.sdi = HDMI_SPD_SDI_PC;
511

512
	intel_write_infoframe(encoder, crtc_state, &frame);
513 514
}

515 516
static void
intel_hdmi_set_hdmi_infoframe(struct drm_encoder *encoder,
517 518
			      const struct intel_crtc_state *crtc_state,
			      const struct drm_connector_state *conn_state)
519 520 521 522 523
{
	union hdmi_infoframe frame;
	int ret;

	ret = drm_hdmi_vendor_infoframe_from_display_mode(&frame.vendor.hdmi,
524
							  conn_state->connector,
525
							  &crtc_state->base.adjusted_mode);
526 527 528
	if (ret < 0)
		return;

529
	intel_write_infoframe(encoder, crtc_state, &frame);
530 531
}

532
static void g4x_set_infoframes(struct drm_encoder *encoder,
533
			       bool enable,
534 535
			       const struct intel_crtc_state *crtc_state,
			       const struct drm_connector_state *conn_state)
536
{
537
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
538 539
	struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
	struct intel_hdmi *intel_hdmi = &intel_dig_port->hdmi;
540
	i915_reg_t reg = VIDEO_DIP_CTL;
541
	u32 val = I915_READ(reg);
542
	u32 port = VIDEO_DIP_PORT(intel_dig_port->base.port);
543

544 545
	assert_hdmi_port_disabled(intel_hdmi);

546 547 548 549 550 551 552 553 554 555 556
	/* 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;

557
	if (!enable) {
558 559
		if (!(val & VIDEO_DIP_ENABLE))
			return;
560 561 562 563 564 565 566
		if (port != (val & VIDEO_DIP_PORT_MASK)) {
			DRM_DEBUG_KMS("video DIP still enabled on port %c\n",
				      (val & VIDEO_DIP_PORT_MASK) >> 29);
			return;
		}
		val &= ~(VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI |
			 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_SPD);
567
		I915_WRITE(reg, val);
568
		POSTING_READ(reg);
569 570 571
		return;
	}

572 573
	if (port != (val & VIDEO_DIP_PORT_MASK)) {
		if (val & VIDEO_DIP_ENABLE) {
574 575 576
			DRM_DEBUG_KMS("video DIP already enabled on port %c\n",
				      (val & VIDEO_DIP_PORT_MASK) >> 29);
			return;
577 578 579 580 581
		}
		val &= ~VIDEO_DIP_PORT_MASK;
		val |= port;
	}

582
	val |= VIDEO_DIP_ENABLE;
583 584
	val &= ~(VIDEO_DIP_ENABLE_AVI |
		 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_SPD);
585

586
	I915_WRITE(reg, val);
587
	POSTING_READ(reg);
588

589 590
	intel_hdmi_set_avi_infoframe(encoder, crtc_state);
	intel_hdmi_set_spd_infoframe(encoder, crtc_state);
591
	intel_hdmi_set_hdmi_infoframe(encoder, crtc_state, conn_state);
592 593
}

594
static bool hdmi_sink_is_deep_color(const struct drm_connector_state *conn_state)
595
{
596
	struct drm_connector *connector = conn_state->connector;
597 598 599 600 601 602 603

	/*
	 * HDMI cloning is only supported on g4x which doesn't
	 * support deep color or GCP infoframes anyway so no
	 * need to worry about multiple HDMI sinks here.
	 */

604
	return connector->display_info.bpc > 8;
605 606
}

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
/*
 * Determine if default_phase=1 can be indicated in the GCP infoframe.
 *
 * From HDMI specification 1.4a:
 * - The first pixel of each Video Data Period shall always have a pixel packing phase of 0
 * - The first pixel following each Video Data Period shall have a pixel packing phase of 0
 * - The PP bits shall be constant for all GCPs and will be equal to the last packing phase
 * - The first pixel following every transition of HSYNC or VSYNC shall have a pixel packing
 *   phase of 0
 */
static bool gcp_default_phase_possible(int pipe_bpp,
				       const struct drm_display_mode *mode)
{
	unsigned int pixels_per_group;

	switch (pipe_bpp) {
	case 30:
		/* 4 pixels in 5 clocks */
		pixels_per_group = 4;
		break;
	case 36:
		/* 2 pixels in 3 clocks */
		pixels_per_group = 2;
		break;
	case 48:
		/* 1 pixel in 2 clocks */
		pixels_per_group = 1;
		break;
	default:
		/* phase information not relevant for 8bpc */
		return false;
	}

	return mode->crtc_hdisplay % pixels_per_group == 0 &&
		mode->crtc_htotal % pixels_per_group == 0 &&
		mode->crtc_hblank_start % pixels_per_group == 0 &&
		mode->crtc_hblank_end % pixels_per_group == 0 &&
		mode->crtc_hsync_start % pixels_per_group == 0 &&
		mode->crtc_hsync_end % pixels_per_group == 0 &&
		((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0 ||
		 mode->crtc_htotal/2 % pixels_per_group == 0);
}

650 651 652
static bool intel_hdmi_set_gcp_infoframe(struct drm_encoder *encoder,
					 const struct intel_crtc_state *crtc_state,
					 const struct drm_connector_state *conn_state)
653
{
654
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
655
	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
656 657
	i915_reg_t reg;
	u32 val = 0;
658 659

	if (HAS_DDI(dev_priv))
660
		reg = HSW_TVIDEO_DIP_GCP(crtc_state->cpu_transcoder);
661
	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
662
		reg = VLV_TVIDEO_DIP_GCP(crtc->pipe);
663
	else if (HAS_PCH_SPLIT(dev_priv))
664 665 666 667 668
		reg = TVIDEO_DIP_GCP(crtc->pipe);
	else
		return false;

	/* Indicate color depth whenever the sink supports deep color */
669
	if (hdmi_sink_is_deep_color(conn_state))
670 671
		val |= GCP_COLOR_INDICATION;

672
	/* Enable default_phase whenever the display mode is suitably aligned */
673 674
	if (gcp_default_phase_possible(crtc_state->pipe_bpp,
				       &crtc_state->base.adjusted_mode))
675 676
		val |= GCP_DEFAULT_PHASE_ENABLE;

677 678 679 680 681
	I915_WRITE(reg, val);

	return val != 0;
}

682
static void ibx_set_infoframes(struct drm_encoder *encoder,
683
			       bool enable,
684 685
			       const struct intel_crtc_state *crtc_state,
			       const struct drm_connector_state *conn_state)
686
{
687
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
688
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
689 690
	struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
	struct intel_hdmi *intel_hdmi = &intel_dig_port->hdmi;
691
	i915_reg_t reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
692
	u32 val = I915_READ(reg);
693
	u32 port = VIDEO_DIP_PORT(intel_dig_port->base.port);
694

695 696
	assert_hdmi_port_disabled(intel_hdmi);

697 698 699
	/* See the big comment in g4x_set_infoframes() */
	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;

700
	if (!enable) {
701 702
		if (!(val & VIDEO_DIP_ENABLE))
			return;
703 704 705
		val &= ~(VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI |
			 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
			 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
706
		I915_WRITE(reg, val);
707
		POSTING_READ(reg);
708 709 710
		return;
	}

711
	if (port != (val & VIDEO_DIP_PORT_MASK)) {
712 713 714
		WARN(val & VIDEO_DIP_ENABLE,
		     "DIP already enabled on port %c\n",
		     (val & VIDEO_DIP_PORT_MASK) >> 29);
715 716 717 718
		val &= ~VIDEO_DIP_PORT_MASK;
		val |= port;
	}

719
	val |= VIDEO_DIP_ENABLE;
720 721 722
	val &= ~(VIDEO_DIP_ENABLE_AVI |
		 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
		 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
723

724
	if (intel_hdmi_set_gcp_infoframe(encoder, crtc_state, conn_state))
725 726
		val |= VIDEO_DIP_ENABLE_GCP;

727
	I915_WRITE(reg, val);
728
	POSTING_READ(reg);
729

730 731
	intel_hdmi_set_avi_infoframe(encoder, crtc_state);
	intel_hdmi_set_spd_infoframe(encoder, crtc_state);
732
	intel_hdmi_set_hdmi_infoframe(encoder, crtc_state, conn_state);
733 734 735
}

static void cpt_set_infoframes(struct drm_encoder *encoder,
736
			       bool enable,
737 738
			       const struct intel_crtc_state *crtc_state,
			       const struct drm_connector_state *conn_state)
739
{
740
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
741
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
742
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
743
	i915_reg_t reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
744 745
	u32 val = I915_READ(reg);

746 747
	assert_hdmi_port_disabled(intel_hdmi);

748 749 750
	/* See the big comment in g4x_set_infoframes() */
	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;

751
	if (!enable) {
752 753
		if (!(val & VIDEO_DIP_ENABLE))
			return;
754 755 756
		val &= ~(VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI |
			 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
			 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
757
		I915_WRITE(reg, val);
758
		POSTING_READ(reg);
759 760 761
		return;
	}

762 763
	/* Set both together, unset both together: see the spec. */
	val |= VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI;
764
	val &= ~(VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
765
		 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
766

767
	if (intel_hdmi_set_gcp_infoframe(encoder, crtc_state, conn_state))
768 769
		val |= VIDEO_DIP_ENABLE_GCP;

770
	I915_WRITE(reg, val);
771
	POSTING_READ(reg);
772

773 774
	intel_hdmi_set_avi_infoframe(encoder, crtc_state);
	intel_hdmi_set_spd_infoframe(encoder, crtc_state);
775
	intel_hdmi_set_hdmi_infoframe(encoder, crtc_state, conn_state);
776 777 778
}

static void vlv_set_infoframes(struct drm_encoder *encoder,
779
			       bool enable,
780 781
			       const struct intel_crtc_state *crtc_state,
			       const struct drm_connector_state *conn_state)
782
{
783
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
784
	struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
785
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
786
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
787
	i915_reg_t reg = VLV_TVIDEO_DIP_CTL(intel_crtc->pipe);
788
	u32 val = I915_READ(reg);
789
	u32 port = VIDEO_DIP_PORT(intel_dig_port->base.port);
790

791 792
	assert_hdmi_port_disabled(intel_hdmi);

793 794 795
	/* See the big comment in g4x_set_infoframes() */
	val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;

796
	if (!enable) {
797 798
		if (!(val & VIDEO_DIP_ENABLE))
			return;
799 800 801
		val &= ~(VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI |
			 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
			 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
802
		I915_WRITE(reg, val);
803
		POSTING_READ(reg);
804 805 806
		return;
	}

807
	if (port != (val & VIDEO_DIP_PORT_MASK)) {
808 809 810
		WARN(val & VIDEO_DIP_ENABLE,
		     "DIP already enabled on port %c\n",
		     (val & VIDEO_DIP_PORT_MASK) >> 29);
811 812 813 814
		val &= ~VIDEO_DIP_PORT_MASK;
		val |= port;
	}

815
	val |= VIDEO_DIP_ENABLE;
816 817 818
	val &= ~(VIDEO_DIP_ENABLE_AVI |
		 VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
		 VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_ENABLE_GCP);
819

820
	if (intel_hdmi_set_gcp_infoframe(encoder, crtc_state, conn_state))
821 822
		val |= VIDEO_DIP_ENABLE_GCP;

823
	I915_WRITE(reg, val);
824
	POSTING_READ(reg);
825

826 827
	intel_hdmi_set_avi_infoframe(encoder, crtc_state);
	intel_hdmi_set_spd_infoframe(encoder, crtc_state);
828
	intel_hdmi_set_hdmi_infoframe(encoder, crtc_state, conn_state);
829 830 831
}

static void hsw_set_infoframes(struct drm_encoder *encoder,
832
			       bool enable,
833 834
			       const struct intel_crtc_state *crtc_state,
			       const struct drm_connector_state *conn_state)
835
{
836
	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
837
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
838
	i915_reg_t reg = HSW_TVIDEO_DIP_CTL(crtc_state->cpu_transcoder);
839
	u32 val = I915_READ(reg);
840

841 842
	assert_hdmi_port_disabled(intel_hdmi);

843 844 845 846
	val &= ~(VIDEO_DIP_ENABLE_VSC_HSW | VIDEO_DIP_ENABLE_AVI_HSW |
		 VIDEO_DIP_ENABLE_GCP_HSW | VIDEO_DIP_ENABLE_VS_HSW |
		 VIDEO_DIP_ENABLE_GMP_HSW | VIDEO_DIP_ENABLE_SPD_HSW);

847
	if (!enable) {
848
		I915_WRITE(reg, val);
849
		POSTING_READ(reg);
850 851 852
		return;
	}

853
	if (intel_hdmi_set_gcp_infoframe(encoder, crtc_state, conn_state))
854 855
		val |= VIDEO_DIP_ENABLE_GCP_HSW;

856
	I915_WRITE(reg, val);
857
	POSTING_READ(reg);
858

859 860
	intel_hdmi_set_avi_infoframe(encoder, crtc_state);
	intel_hdmi_set_spd_infoframe(encoder, crtc_state);
861
	intel_hdmi_set_hdmi_infoframe(encoder, crtc_state, conn_state);
862 863
}

864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
void intel_dp_dual_mode_set_tmds_output(struct intel_hdmi *hdmi, bool enable)
{
	struct drm_i915_private *dev_priv = to_i915(intel_hdmi_to_dev(hdmi));
	struct i2c_adapter *adapter =
		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);

	if (hdmi->dp_dual_mode.type < DRM_DP_DUAL_MODE_TYPE2_DVI)
		return;

	DRM_DEBUG_KMS("%s DP dual mode adaptor TMDS output\n",
		      enable ? "Enabling" : "Disabling");

	drm_dp_dual_mode_set_tmds_output(hdmi->dp_dual_mode.type,
					 adapter, enable);
}

S
Sean Paul 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
static int intel_hdmi_hdcp_read(struct intel_digital_port *intel_dig_port,
				unsigned int offset, void *buffer, size_t size)
{
	struct intel_hdmi *hdmi = &intel_dig_port->hdmi;
	struct drm_i915_private *dev_priv =
		intel_dig_port->base.base.dev->dev_private;
	struct i2c_adapter *adapter = intel_gmbus_get_adapter(dev_priv,
							      hdmi->ddc_bus);
	int ret;
	u8 start = offset & 0xff;
	struct i2c_msg msgs[] = {
		{
			.addr = DRM_HDCP_DDC_ADDR,
			.flags = 0,
			.len = 1,
			.buf = &start,
		},
		{
			.addr = DRM_HDCP_DDC_ADDR,
			.flags = I2C_M_RD,
			.len = size,
			.buf = buffer
		}
	};
	ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
	if (ret == ARRAY_SIZE(msgs))
		return 0;
	return ret >= 0 ? -EIO : ret;
}

static int intel_hdmi_hdcp_write(struct intel_digital_port *intel_dig_port,
				 unsigned int offset, void *buffer, size_t size)
{
	struct intel_hdmi *hdmi = &intel_dig_port->hdmi;
	struct drm_i915_private *dev_priv =
		intel_dig_port->base.base.dev->dev_private;
	struct i2c_adapter *adapter = intel_gmbus_get_adapter(dev_priv,
							      hdmi->ddc_bus);
	int ret;
	u8 *write_buf;
	struct i2c_msg msg;

	write_buf = kzalloc(size + 1, GFP_KERNEL);
	if (!write_buf)
		return -ENOMEM;

	write_buf[0] = offset & 0xff;
	memcpy(&write_buf[1], buffer, size);

	msg.addr = DRM_HDCP_DDC_ADDR;
	msg.flags = 0,
	msg.len = size + 1,
	msg.buf = write_buf;

	ret = i2c_transfer(adapter, &msg, 1);
	if (ret == 1)
		return 0;
	return ret >= 0 ? -EIO : ret;
}

static
int intel_hdmi_hdcp_write_an_aksv(struct intel_digital_port *intel_dig_port,
				  u8 *an)
{
	struct intel_hdmi *hdmi = &intel_dig_port->hdmi;
	struct drm_i915_private *dev_priv =
		intel_dig_port->base.base.dev->dev_private;
	struct i2c_adapter *adapter = intel_gmbus_get_adapter(dev_priv,
							      hdmi->ddc_bus);
	int ret;

	ret = intel_hdmi_hdcp_write(intel_dig_port, DRM_HDCP_DDC_AN, an,
				    DRM_HDCP_AN_LEN);
	if (ret) {
		DRM_ERROR("Write An over DDC failed (%d)\n", ret);
		return ret;
	}

	ret = intel_gmbus_output_aksv(adapter);
	if (ret < 0) {
		DRM_ERROR("Failed to output aksv (%d)\n", ret);
		return ret;
	}
	return 0;
}

static int intel_hdmi_hdcp_read_bksv(struct intel_digital_port *intel_dig_port,
				     u8 *bksv)
{
	int ret;
	ret = intel_hdmi_hdcp_read(intel_dig_port, DRM_HDCP_DDC_BKSV, bksv,
				   DRM_HDCP_KSV_LEN);
	if (ret)
		DRM_ERROR("Read Bksv over DDC failed (%d)\n", ret);
	return ret;
}

static
int intel_hdmi_hdcp_read_bstatus(struct intel_digital_port *intel_dig_port,
				 u8 *bstatus)
{
	int ret;
	ret = intel_hdmi_hdcp_read(intel_dig_port, DRM_HDCP_DDC_BSTATUS,
				   bstatus, DRM_HDCP_BSTATUS_LEN);
	if (ret)
		DRM_ERROR("Read bstatus over DDC failed (%d)\n", ret);
	return ret;
}

static
int intel_hdmi_hdcp_repeater_present(struct intel_digital_port *intel_dig_port,
				     bool *repeater_present)
{
	int ret;
	u8 val;

	ret = intel_hdmi_hdcp_read(intel_dig_port, DRM_HDCP_DDC_BCAPS, &val, 1);
	if (ret) {
		DRM_ERROR("Read bcaps over DDC failed (%d)\n", ret);
		return ret;
	}
	*repeater_present = val & DRM_HDCP_DDC_BCAPS_REPEATER_PRESENT;
	return 0;
}

static
int intel_hdmi_hdcp_read_ri_prime(struct intel_digital_port *intel_dig_port,
				  u8 *ri_prime)
{
	int ret;
	ret = intel_hdmi_hdcp_read(intel_dig_port, DRM_HDCP_DDC_RI_PRIME,
				   ri_prime, DRM_HDCP_RI_LEN);
	if (ret)
		DRM_ERROR("Read Ri' over DDC failed (%d)\n", ret);
	return ret;
}

static
int intel_hdmi_hdcp_read_ksv_ready(struct intel_digital_port *intel_dig_port,
				   bool *ksv_ready)
{
	int ret;
	u8 val;

	ret = intel_hdmi_hdcp_read(intel_dig_port, DRM_HDCP_DDC_BCAPS, &val, 1);
	if (ret) {
		DRM_ERROR("Read bcaps over DDC failed (%d)\n", ret);
		return ret;
	}
	*ksv_ready = val & DRM_HDCP_DDC_BCAPS_KSV_FIFO_READY;
	return 0;
}

static
int intel_hdmi_hdcp_read_ksv_fifo(struct intel_digital_port *intel_dig_port,
				  int num_downstream, u8 *ksv_fifo)
{
	int ret;
	ret = intel_hdmi_hdcp_read(intel_dig_port, DRM_HDCP_DDC_KSV_FIFO,
				   ksv_fifo, num_downstream * DRM_HDCP_KSV_LEN);
	if (ret) {
		DRM_ERROR("Read ksv fifo over DDC failed (%d)\n", ret);
		return ret;
	}
	return 0;
}

static
int intel_hdmi_hdcp_read_v_prime_part(struct intel_digital_port *intel_dig_port,
				      int i, u32 *part)
{
	int ret;

	if (i >= DRM_HDCP_V_PRIME_NUM_PARTS)
		return -EINVAL;

	ret = intel_hdmi_hdcp_read(intel_dig_port, DRM_HDCP_DDC_V_PRIME(i),
				   part, DRM_HDCP_V_PRIME_PART_LEN);
	if (ret)
		DRM_ERROR("Read V'[%d] over DDC failed (%d)\n", i, ret);
	return ret;
}

static
int intel_hdmi_hdcp_toggle_signalling(struct intel_digital_port *intel_dig_port,
				      bool enable)
{
	int ret;

	if (!enable)
		usleep_range(6, 60); /* Bspec says >= 6us */

	ret = intel_ddi_toggle_hdcp_signalling(&intel_dig_port->base, enable);
	if (ret) {
		DRM_ERROR("%s HDCP signalling failed (%d)\n",
			  enable ? "Enable" : "Disable", ret);
		return ret;
	}
	return 0;
}

static
bool intel_hdmi_hdcp_check_link(struct intel_digital_port *intel_dig_port)
{
	struct drm_i915_private *dev_priv =
		intel_dig_port->base.base.dev->dev_private;
	enum port port = intel_dig_port->base.port;
	int ret;
	union {
		u32 reg;
		u8 shim[DRM_HDCP_RI_LEN];
	} ri;

	ret = intel_hdmi_hdcp_read_ri_prime(intel_dig_port, ri.shim);
	if (ret)
		return false;

	I915_WRITE(PORT_HDCP_RPRIME(port), ri.reg);

	/* Wait for Ri prime match */
	if (wait_for(I915_READ(PORT_HDCP_STATUS(port)) &
		     (HDCP_STATUS_RI_MATCH | HDCP_STATUS_ENC), 1)) {
		DRM_ERROR("Ri' mismatch detected, link check failed (%x)\n",
			  I915_READ(PORT_HDCP_STATUS(port)));
		return false;
	}
	return true;
}

static const struct intel_hdcp_shim intel_hdmi_hdcp_shim = {
	.write_an_aksv = intel_hdmi_hdcp_write_an_aksv,
	.read_bksv = intel_hdmi_hdcp_read_bksv,
	.read_bstatus = intel_hdmi_hdcp_read_bstatus,
	.repeater_present = intel_hdmi_hdcp_repeater_present,
	.read_ri_prime = intel_hdmi_hdcp_read_ri_prime,
	.read_ksv_ready = intel_hdmi_hdcp_read_ksv_ready,
	.read_ksv_fifo = intel_hdmi_hdcp_read_ksv_fifo,
	.read_v_prime_part = intel_hdmi_hdcp_read_v_prime_part,
	.toggle_signalling = intel_hdmi_hdcp_toggle_signalling,
	.check_link = intel_hdmi_hdcp_check_link,
};

1122 1123
static void intel_hdmi_prepare(struct intel_encoder *encoder,
			       const struct intel_crtc_state *crtc_state)
1124
{
1125
	struct drm_device *dev = encoder->base.dev;
1126
	struct drm_i915_private *dev_priv = to_i915(dev);
1127
	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1128
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
1129
	const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
1130
	u32 hdmi_val;
1131

1132 1133
	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);

1134
	hdmi_val = SDVO_ENCODING_HDMI;
1135
	if (!HAS_PCH_SPLIT(dev_priv) && crtc_state->limited_color_range)
1136
		hdmi_val |= HDMI_COLOR_RANGE_16_235;
1137
	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
1138
		hdmi_val |= SDVO_VSYNC_ACTIVE_HIGH;
1139
	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
1140
		hdmi_val |= SDVO_HSYNC_ACTIVE_HIGH;
1141

1142
	if (crtc_state->pipe_bpp > 24)
1143
		hdmi_val |= HDMI_COLOR_FORMAT_12bpc;
1144
	else
1145
		hdmi_val |= SDVO_COLOR_FORMAT_8bpc;
1146

1147
	if (crtc_state->has_hdmi_sink)
1148
		hdmi_val |= HDMI_MODE_SELECT_HDMI;
1149

1150
	if (HAS_PCH_CPT(dev_priv))
1151
		hdmi_val |= SDVO_PIPE_SEL_CPT(crtc->pipe);
1152
	else if (IS_CHERRYVIEW(dev_priv))
1153
		hdmi_val |= SDVO_PIPE_SEL_CHV(crtc->pipe);
1154
	else
1155
		hdmi_val |= SDVO_PIPE_SEL(crtc->pipe);
1156

1157 1158
	I915_WRITE(intel_hdmi->hdmi_reg, hdmi_val);
	POSTING_READ(intel_hdmi->hdmi_reg);
1159 1160
}

1161 1162
static bool intel_hdmi_get_hw_state(struct intel_encoder *encoder,
				    enum pipe *pipe)
1163
{
1164
	struct drm_device *dev = encoder->base.dev;
1165
	struct drm_i915_private *dev_priv = to_i915(dev);
1166 1167
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
	u32 tmp;
1168
	bool ret;
1169

1170 1171
	if (!intel_display_power_get_if_enabled(dev_priv,
						encoder->power_domain))
1172 1173
		return false;

1174 1175
	ret = false;

1176
	tmp = I915_READ(intel_hdmi->hdmi_reg);
1177 1178

	if (!(tmp & SDVO_ENABLE))
1179
		goto out;
1180

1181
	if (HAS_PCH_CPT(dev_priv))
1182
		*pipe = PORT_TO_PIPE_CPT(tmp);
1183
	else if (IS_CHERRYVIEW(dev_priv))
1184
		*pipe = SDVO_PORT_TO_PIPE_CHV(tmp);
1185 1186 1187
	else
		*pipe = PORT_TO_PIPE(tmp);

1188 1189 1190
	ret = true;

out:
1191
	intel_display_power_put(dev_priv, encoder->power_domain);
1192 1193

	return ret;
1194 1195
}

1196
static void intel_hdmi_get_config(struct intel_encoder *encoder,
1197
				  struct intel_crtc_state *pipe_config)
1198 1199
{
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
1200
	struct intel_digital_port *intel_dig_port = hdmi_to_dig_port(intel_hdmi);
1201
	struct drm_device *dev = encoder->base.dev;
1202
	struct drm_i915_private *dev_priv = to_i915(dev);
1203
	u32 tmp, flags = 0;
1204
	int dotclock;
1205

1206 1207
	pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);

1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
	tmp = I915_READ(intel_hdmi->hdmi_reg);

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

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

1220 1221 1222
	if (tmp & HDMI_MODE_SELECT_HDMI)
		pipe_config->has_hdmi_sink = true;

1223
	if (intel_dig_port->infoframe_enabled(&encoder->base, pipe_config))
1224 1225
		pipe_config->has_infoframe = true;

1226
	if (tmp & SDVO_AUDIO_ENABLE)
1227 1228
		pipe_config->has_audio = true;

1229
	if (!HAS_PCH_SPLIT(dev_priv) &&
1230 1231 1232
	    tmp & HDMI_COLOR_RANGE_16_235)
		pipe_config->limited_color_range = true;

1233
	pipe_config->base.adjusted_mode.flags |= flags;
1234 1235 1236 1237 1238 1239

	if ((tmp & SDVO_COLOR_FORMAT_MASK) == HDMI_COLOR_FORMAT_12bpc)
		dotclock = pipe_config->port_clock * 2 / 3;
	else
		dotclock = pipe_config->port_clock;

1240 1241 1242
	if (pipe_config->pixel_multiplier)
		dotclock /= pipe_config->pixel_multiplier;

1243
	pipe_config->base.adjusted_mode.crtc_clock = dotclock;
1244 1245

	pipe_config->lane_count = 4;
1246 1247
}

1248
static void intel_enable_hdmi_audio(struct intel_encoder *encoder,
1249 1250
				    const struct intel_crtc_state *pipe_config,
				    const struct drm_connector_state *conn_state)
1251
{
1252
	struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
1253

1254
	WARN_ON(!pipe_config->has_hdmi_sink);
1255 1256
	DRM_DEBUG_DRIVER("Enabling HDMI audio on pipe %c\n",
			 pipe_name(crtc->pipe));
1257
	intel_audio_codec_enable(encoder, pipe_config, conn_state);
1258 1259
}

1260
static void g4x_enable_hdmi(struct intel_encoder *encoder,
1261 1262
			    const struct intel_crtc_state *pipe_config,
			    const struct drm_connector_state *conn_state)
1263
{
1264
	struct drm_device *dev = encoder->base.dev;
1265
	struct drm_i915_private *dev_priv = to_i915(dev);
1266
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
1267 1268
	u32 temp;

1269
	temp = I915_READ(intel_hdmi->hdmi_reg);
1270

1271
	temp |= SDVO_ENABLE;
1272
	if (pipe_config->has_audio)
1273
		temp |= SDVO_AUDIO_ENABLE;
1274

1275 1276 1277
	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);

1278 1279
	if (pipe_config->has_audio)
		intel_enable_hdmi_audio(encoder, pipe_config, conn_state);
1280 1281
}

1282
static void ibx_enable_hdmi(struct intel_encoder *encoder,
1283 1284
			    const struct intel_crtc_state *pipe_config,
			    const struct drm_connector_state *conn_state)
1285 1286
{
	struct drm_device *dev = encoder->base.dev;
1287
	struct drm_i915_private *dev_priv = to_i915(dev);
1288 1289 1290 1291
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
	u32 temp;

	temp = I915_READ(intel_hdmi->hdmi_reg);
1292

1293
	temp |= SDVO_ENABLE;
1294
	if (pipe_config->has_audio)
1295
		temp |= SDVO_AUDIO_ENABLE;
1296

1297 1298 1299 1300 1301 1302
	/*
	 * HW workaround, need to write this twice for issue
	 * that may result in first write getting masked.
	 */
	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);
1303 1304
	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);
1305

1306 1307 1308 1309 1310 1311
	/*
	 * HW workaround, need to toggle enable bit off and on
	 * for 12bpc with pixel repeat.
	 *
	 * FIXME: BSpec says this should be done at the end of
	 * of the modeset sequence, so not sure if this isn't too soon.
1312
	 */
1313 1314
	if (pipe_config->pipe_bpp > 24 &&
	    pipe_config->pixel_multiplier > 1) {
1315 1316 1317 1318 1319 1320 1321 1322 1323
		I915_WRITE(intel_hdmi->hdmi_reg, temp & ~SDVO_ENABLE);
		POSTING_READ(intel_hdmi->hdmi_reg);

		/*
		 * HW workaround, need to write this twice for issue
		 * that may result in first write getting masked.
		 */
		I915_WRITE(intel_hdmi->hdmi_reg, temp);
		POSTING_READ(intel_hdmi->hdmi_reg);
1324 1325
		I915_WRITE(intel_hdmi->hdmi_reg, temp);
		POSTING_READ(intel_hdmi->hdmi_reg);
1326
	}
1327

1328 1329
	if (pipe_config->has_audio)
		intel_enable_hdmi_audio(encoder, pipe_config, conn_state);
1330 1331
}

1332
static void cpt_enable_hdmi(struct intel_encoder *encoder,
1333 1334
			    const struct intel_crtc_state *pipe_config,
			    const struct drm_connector_state *conn_state)
1335 1336
{
	struct drm_device *dev = encoder->base.dev;
1337
	struct drm_i915_private *dev_priv = to_i915(dev);
1338
	struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
1339 1340 1341 1342 1343 1344 1345
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
	enum pipe pipe = crtc->pipe;
	u32 temp;

	temp = I915_READ(intel_hdmi->hdmi_reg);

	temp |= SDVO_ENABLE;
1346
	if (pipe_config->has_audio)
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
		temp |= SDVO_AUDIO_ENABLE;

	/*
	 * WaEnableHDMI8bpcBefore12bpc:snb,ivb
	 *
	 * The procedure for 12bpc is as follows:
	 * 1. disable HDMI clock gating
	 * 2. enable HDMI with 8bpc
	 * 3. enable HDMI with 12bpc
	 * 4. enable HDMI clock gating
	 */

1359
	if (pipe_config->pipe_bpp > 24) {
1360 1361 1362 1363 1364 1365
		I915_WRITE(TRANS_CHICKEN1(pipe),
			   I915_READ(TRANS_CHICKEN1(pipe)) |
			   TRANS_CHICKEN1_HDMIUNIT_GC_DISABLE);

		temp &= ~SDVO_COLOR_FORMAT_MASK;
		temp |= SDVO_COLOR_FORMAT_8bpc;
1366
	}
1367 1368 1369 1370

	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);

1371
	if (pipe_config->pipe_bpp > 24) {
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
		temp &= ~SDVO_COLOR_FORMAT_MASK;
		temp |= HDMI_COLOR_FORMAT_12bpc;

		I915_WRITE(intel_hdmi->hdmi_reg, temp);
		POSTING_READ(intel_hdmi->hdmi_reg);

		I915_WRITE(TRANS_CHICKEN1(pipe),
			   I915_READ(TRANS_CHICKEN1(pipe)) &
			   ~TRANS_CHICKEN1_HDMIUNIT_GC_DISABLE);
	}

1383 1384
	if (pipe_config->has_audio)
		intel_enable_hdmi_audio(encoder, pipe_config, conn_state);
1385
}
1386

1387
static void vlv_enable_hdmi(struct intel_encoder *encoder,
1388 1389
			    const struct intel_crtc_state *pipe_config,
			    const struct drm_connector_state *conn_state)
1390
{
1391 1392
}

1393
static void intel_disable_hdmi(struct intel_encoder *encoder,
1394 1395
			       const struct intel_crtc_state *old_crtc_state,
			       const struct drm_connector_state *old_conn_state)
1396 1397
{
	struct drm_device *dev = encoder->base.dev;
1398
	struct drm_i915_private *dev_priv = to_i915(dev);
1399
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
1400 1401
	struct intel_digital_port *intel_dig_port =
		hdmi_to_dig_port(intel_hdmi);
1402
	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
1403 1404
	u32 temp;

1405
	temp = I915_READ(intel_hdmi->hdmi_reg);
1406

1407
	temp &= ~(SDVO_ENABLE | SDVO_AUDIO_ENABLE);
1408 1409
	I915_WRITE(intel_hdmi->hdmi_reg, temp);
	POSTING_READ(intel_hdmi->hdmi_reg);
1410 1411 1412 1413 1414 1415

	/*
	 * HW workaround for IBX, we need to move the port
	 * to transcoder A after disabling it to allow the
	 * matching DP port to be enabled on transcoder A.
	 */
1416
	if (HAS_PCH_IBX(dev_priv) && crtc->pipe == PIPE_B) {
1417 1418 1419 1420 1421 1422 1423
		/*
		 * We get CPU/PCH FIFO underruns on the other pipe when
		 * doing the workaround. Sweep them under the rug.
		 */
		intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, false);
		intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
		temp &= ~SDVO_PIPE_B_SELECT;
		temp |= SDVO_ENABLE;
		/*
		 * HW workaround, need to write this twice for issue
		 * that may result in first write getting masked.
		 */
		I915_WRITE(intel_hdmi->hdmi_reg, temp);
		POSTING_READ(intel_hdmi->hdmi_reg);
		I915_WRITE(intel_hdmi->hdmi_reg, temp);
		POSTING_READ(intel_hdmi->hdmi_reg);

		temp &= ~SDVO_ENABLE;
		I915_WRITE(intel_hdmi->hdmi_reg, temp);
		POSTING_READ(intel_hdmi->hdmi_reg);
1438

1439
		intel_wait_for_vblank_if_active(dev_priv, PIPE_A);
1440 1441
		intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, true);
		intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
1442
	}
1443

1444 1445
	intel_dig_port->set_infoframes(&encoder->base, false,
				       old_crtc_state, old_conn_state);
1446 1447

	intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
1448 1449
}

1450
static void g4x_disable_hdmi(struct intel_encoder *encoder,
1451 1452
			     const struct intel_crtc_state *old_crtc_state,
			     const struct drm_connector_state *old_conn_state)
1453
{
1454
	if (old_crtc_state->has_audio)
1455 1456
		intel_audio_codec_disable(encoder,
					  old_crtc_state, old_conn_state);
1457

1458
	intel_disable_hdmi(encoder, old_crtc_state, old_conn_state);
1459 1460
}

1461
static void pch_disable_hdmi(struct intel_encoder *encoder,
1462 1463
			     const struct intel_crtc_state *old_crtc_state,
			     const struct drm_connector_state *old_conn_state)
1464
{
1465
	if (old_crtc_state->has_audio)
1466 1467
		intel_audio_codec_disable(encoder,
					  old_crtc_state, old_conn_state);
1468 1469
}

1470
static void pch_post_disable_hdmi(struct intel_encoder *encoder,
1471 1472
				  const struct intel_crtc_state *old_crtc_state,
				  const struct drm_connector_state *old_conn_state)
1473
{
1474
	intel_disable_hdmi(encoder, old_crtc_state, old_conn_state);
1475 1476
}

1477
static int intel_hdmi_source_max_tmds_clock(struct intel_encoder *encoder)
1478
{
1479 1480 1481 1482 1483
	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
	const struct ddi_vbt_port_info *info =
		&dev_priv->vbt.ddi_port_info[encoder->port];
	int max_tmds_clock;

1484
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
1485 1486 1487 1488 1489
		max_tmds_clock = 594000;
	else if (INTEL_GEN(dev_priv) >= 8 || IS_HASWELL(dev_priv))
		max_tmds_clock = 300000;
	else if (INTEL_GEN(dev_priv) >= 5)
		max_tmds_clock = 225000;
1490
	else
1491 1492 1493 1494 1495 1496
		max_tmds_clock = 165000;

	if (info->max_tmds_clock)
		max_tmds_clock = min(max_tmds_clock, info->max_tmds_clock);

	return max_tmds_clock;
1497 1498
}

1499
static int hdmi_port_clock_limit(struct intel_hdmi *hdmi,
1500 1501
				 bool respect_downstream_limits,
				 bool force_dvi)
1502
{
1503 1504
	struct intel_encoder *encoder = &hdmi_to_dig_port(hdmi)->base;
	int max_tmds_clock = intel_hdmi_source_max_tmds_clock(encoder);
1505 1506

	if (respect_downstream_limits) {
1507 1508 1509
		struct intel_connector *connector = hdmi->attached_connector;
		const struct drm_display_info *info = &connector->base.display_info;

1510 1511 1512
		if (hdmi->dp_dual_mode.max_tmds_clock)
			max_tmds_clock = min(max_tmds_clock,
					     hdmi->dp_dual_mode.max_tmds_clock);
1513 1514 1515 1516

		if (info->max_tmds_clock)
			max_tmds_clock = min(max_tmds_clock,
					     info->max_tmds_clock);
1517
		else if (!hdmi->has_hdmi_sink || force_dvi)
1518 1519 1520 1521 1522 1523
			max_tmds_clock = min(max_tmds_clock, 165000);
	}

	return max_tmds_clock;
}

1524 1525
static enum drm_mode_status
hdmi_port_clock_valid(struct intel_hdmi *hdmi,
1526 1527
		      int clock, bool respect_downstream_limits,
		      bool force_dvi)
1528
{
1529
	struct drm_i915_private *dev_priv = to_i915(intel_hdmi_to_dev(hdmi));
1530 1531 1532

	if (clock < 25000)
		return MODE_CLOCK_LOW;
1533
	if (clock > hdmi_port_clock_limit(hdmi, respect_downstream_limits, force_dvi))
1534 1535
		return MODE_CLOCK_HIGH;

1536
	/* BXT DPLL can't generate 223-240 MHz */
1537
	if (IS_GEN9_LP(dev_priv) && clock > 223333 && clock < 240000)
1538 1539 1540
		return MODE_CLOCK_RANGE;

	/* CHV DPLL can't generate 216-240 MHz */
1541
	if (IS_CHERRYVIEW(dev_priv) && clock > 216000 && clock < 240000)
1542 1543 1544 1545 1546
		return MODE_CLOCK_RANGE;

	return MODE_OK;
}

1547 1548 1549
static enum drm_mode_status
intel_hdmi_mode_valid(struct drm_connector *connector,
		      struct drm_display_mode *mode)
1550
{
1551 1552
	struct intel_hdmi *hdmi = intel_attached_hdmi(connector);
	struct drm_device *dev = intel_hdmi_to_dev(hdmi);
1553
	struct drm_i915_private *dev_priv = to_i915(dev);
1554 1555
	enum drm_mode_status status;
	int clock;
M
Mika Kahola 已提交
1556
	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
1557 1558
	bool force_dvi =
		READ_ONCE(to_intel_digital_connector_state(connector->state)->force_audio) == HDMI_AUDIO_OFF_DVI;
1559 1560

	clock = mode->clock;
M
Mika Kahola 已提交
1561 1562 1563 1564 1565 1566 1567

	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
		clock *= 2;

	if (clock > max_dotclk)
		return MODE_CLOCK_HIGH;

1568 1569 1570
	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
		clock *= 2;

1571 1572 1573
	if (drm_mode_is_420_only(&connector->display_info, mode))
		clock /= 2;

1574
	/* check if we can do 8bpc */
1575
	status = hdmi_port_clock_valid(hdmi, clock, true, force_dvi);
1576

1577
	/* if we can't do 8bpc we may still be able to do 12bpc */
1578 1579
	if (!HAS_GMCH_DISPLAY(dev_priv) && status != MODE_OK && hdmi->has_hdmi_sink && !force_dvi)
		status = hdmi_port_clock_valid(hdmi, clock * 3 / 2, true, force_dvi);
1580

1581
	return status;
1582 1583
}

1584
static bool hdmi_12bpc_possible(const struct intel_crtc_state *crtc_state)
1585
{
1586 1587 1588 1589 1590 1591
	struct drm_i915_private *dev_priv =
		to_i915(crtc_state->base.crtc->dev);
	struct drm_atomic_state *state = crtc_state->base.state;
	struct drm_connector_state *connector_state;
	struct drm_connector *connector;
	int i;
1592

1593
	if (HAS_GMCH_DISPLAY(dev_priv))
1594 1595
		return false;

1596 1597 1598 1599 1600 1601
	if (crtc_state->pipe_bpp <= 8*3)
		return false;

	if (!crtc_state->has_hdmi_sink)
		return false;

1602 1603 1604 1605
	/*
	 * HDMI 12bpc affects the clocks, so it's only possible
	 * when not cloning with other encoder types.
	 */
1606 1607 1608
	if (crtc_state->output_types != 1 << INTEL_OUTPUT_HDMI)
		return false;

1609
	for_each_new_connector_in_state(state, connector, connector_state, i) {
1610 1611 1612 1613 1614
		const struct drm_display_info *info = &connector->display_info;

		if (connector_state->crtc != crtc_state->base.crtc)
			continue;

1615 1616 1617 1618 1619 1620 1621 1622 1623
		if (crtc_state->ycbcr420) {
			const struct drm_hdmi_info *hdmi = &info->hdmi;

			if (!(hdmi->y420_dc_modes & DRM_EDID_YCBCR420_DC_36))
				return false;
		} else {
			if (!(info->edid_hdmi_dc_modes & DRM_EDID_HDMI_DC_36))
				return false;
		}
1624 1625
	}

L
Lucas De Marchi 已提交
1626
	/* Display WA #1139: glk */
1627 1628 1629 1630
	if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1) &&
	    crtc_state->base.adjusted_mode.htotal > 5460)
		return false;

1631
	return true;
1632 1633
}

1634 1635 1636 1637 1638
static bool
intel_hdmi_ycbcr420_config(struct drm_connector *connector,
			   struct intel_crtc_state *config,
			   int *clock_12bpc, int *clock_8bpc)
{
1639 1640
	struct intel_crtc *intel_crtc = to_intel_crtc(config->base.crtc);

1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
	if (!connector->ycbcr_420_allowed) {
		DRM_ERROR("Platform doesn't support YCBCR420 output\n");
		return false;
	}

	/* YCBCR420 TMDS rate requirement is half the pixel clock */
	config->port_clock /= 2;
	*clock_12bpc /= 2;
	*clock_8bpc /= 2;
	config->ycbcr420 = true;
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660

	/* YCBCR 420 output conversion needs a scaler */
	if (skl_update_scaler_crtc(config)) {
		DRM_DEBUG_KMS("Scaler allocation for output failed\n");
		return false;
	}

	intel_pch_panel_fitting(intel_crtc, config,
				DRM_MODE_SCALE_FULLSCREEN);

1661 1662 1663
	return true;
}

1664
bool intel_hdmi_compute_config(struct intel_encoder *encoder,
1665 1666
			       struct intel_crtc_state *pipe_config,
			       struct drm_connector_state *conn_state)
1667
{
1668
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
1669
	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1670
	struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
1671 1672
	struct drm_connector *connector = conn_state->connector;
	struct drm_scdc *scdc = &connector->display_info.hdmi.scdc;
1673 1674
	struct intel_digital_connector_state *intel_conn_state =
		to_intel_digital_connector_state(conn_state);
1675 1676
	int clock_8bpc = pipe_config->base.adjusted_mode.crtc_clock;
	int clock_12bpc = clock_8bpc * 3 / 2;
1677
	int desired_bpp;
1678
	bool force_dvi = intel_conn_state->force_audio == HDMI_AUDIO_OFF_DVI;
1679

1680
	pipe_config->has_hdmi_sink = !force_dvi && intel_hdmi->has_hdmi_sink;
1681

1682 1683 1684
	if (pipe_config->has_hdmi_sink)
		pipe_config->has_infoframe = true;

1685
	if (intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
1686
		/* See CEA-861-E - 5.1 Default Encoding Parameters */
1687 1688
		pipe_config->limited_color_range =
			pipe_config->has_hdmi_sink &&
1689 1690
			drm_default_rgb_quant_range(adjusted_mode) ==
			HDMI_QUANTIZATION_RANGE_LIMITED;
1691 1692
	} else {
		pipe_config->limited_color_range =
1693
			intel_conn_state->broadcast_rgb == INTEL_BROADCAST_RGB_LIMITED;
1694 1695
	}

1696 1697
	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK) {
		pipe_config->pixel_multiplier = 2;
1698
		clock_8bpc *= 2;
1699
		clock_12bpc *= 2;
1700 1701
	}

1702 1703 1704 1705 1706 1707 1708 1709
	if (drm_mode_is_420_only(&connector->display_info, adjusted_mode)) {
		if (!intel_hdmi_ycbcr420_config(connector, pipe_config,
						&clock_12bpc, &clock_8bpc)) {
			DRM_ERROR("Can't support YCBCR420 output\n");
			return false;
		}
	}

1710
	if (HAS_PCH_SPLIT(dev_priv) && !HAS_DDI(dev_priv))
1711 1712
		pipe_config->has_pch_encoder = true;

1713 1714 1715 1716 1717 1718 1719
	if (pipe_config->has_hdmi_sink) {
		if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
			pipe_config->has_audio = intel_hdmi->has_audio;
		else
			pipe_config->has_audio =
				intel_conn_state->force_audio == HDMI_AUDIO_ON;
	}
1720

1721 1722 1723
	/*
	 * 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
1724 1725
	 * outputs. We also need to check that the higher clock still fits
	 * within limits.
1726
	 */
1727 1728
	if (hdmi_12bpc_possible(pipe_config) &&
	    hdmi_port_clock_valid(intel_hdmi, clock_12bpc, true, force_dvi) == MODE_OK) {
1729 1730
		DRM_DEBUG_KMS("picking bpc to 12 for HDMI output\n");
		desired_bpp = 12*3;
1731 1732

		/* Need to adjust the port link by 1.5x for 12bpc. */
1733
		pipe_config->port_clock = clock_12bpc;
1734
	} else {
1735 1736
		DRM_DEBUG_KMS("picking bpc to 8 for HDMI output\n");
		desired_bpp = 8*3;
1737 1738

		pipe_config->port_clock = clock_8bpc;
1739 1740 1741
	}

	if (!pipe_config->bw_constrained) {
1742
		DRM_DEBUG_KMS("forcing pipe bpp to %i for HDMI\n", desired_bpp);
1743
		pipe_config->pipe_bpp = desired_bpp;
1744 1745
	}

1746
	if (hdmi_port_clock_valid(intel_hdmi, pipe_config->port_clock,
1747
				  false, force_dvi) != MODE_OK) {
1748
		DRM_DEBUG_KMS("unsupported HDMI clock, rejecting mode\n");
1749 1750 1751
		return false;
	}

1752
	/* Set user selected PAR to incoming mode's member */
1753
	adjusted_mode->picture_aspect_ratio = conn_state->picture_aspect_ratio;
1754

1755 1756
	pipe_config->lane_count = 4;

1757 1758
	if (scdc->scrambling.supported && (INTEL_GEN(dev_priv) >= 10 ||
					   IS_GEMINILAKE(dev_priv))) {
S
Shashank Sharma 已提交
1759 1760 1761 1762 1763 1764 1765 1766 1767
		if (scdc->scrambling.low_rates)
			pipe_config->hdmi_scrambling = true;

		if (pipe_config->port_clock > 340000) {
			pipe_config->hdmi_scrambling = true;
			pipe_config->hdmi_high_tmds_clock_ratio = true;
		}
	}

1768 1769 1770
	return true;
}

1771 1772
static void
intel_hdmi_unset_edid(struct drm_connector *connector)
1773
{
1774
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
1775

1776 1777 1778 1779
	intel_hdmi->has_hdmi_sink = false;
	intel_hdmi->has_audio = false;
	intel_hdmi->rgb_quant_range_selectable = false;

1780 1781 1782
	intel_hdmi->dp_dual_mode.type = DRM_DP_DUAL_MODE_NONE;
	intel_hdmi->dp_dual_mode.max_tmds_clock = 0;

1783 1784 1785 1786
	kfree(to_intel_connector(connector)->detect_edid);
	to_intel_connector(connector)->detect_edid = NULL;
}

1787
static void
1788
intel_hdmi_dp_dual_mode_detect(struct drm_connector *connector, bool has_edid)
1789 1790 1791
{
	struct drm_i915_private *dev_priv = to_i915(connector->dev);
	struct intel_hdmi *hdmi = intel_attached_hdmi(connector);
1792
	enum port port = hdmi_to_dig_port(hdmi)->base.port;
1793 1794 1795 1796
	struct i2c_adapter *adapter =
		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
	enum drm_dp_dual_mode_type type = drm_dp_dual_mode_detect(adapter);

1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
	/*
	 * Type 1 DVI adaptors are not required to implement any
	 * registers, so we can't always detect their presence.
	 * Ideally we should be able to check the state of the
	 * CONFIG1 pin, but no such luck on our hardware.
	 *
	 * The only method left to us is to check the VBT to see
	 * if the port is a dual mode capable DP port. But let's
	 * only do that when we sucesfully read the EDID, to avoid
	 * confusing log messages about DP dual mode adaptors when
	 * there's nothing connected to the port.
	 */
	if (type == DRM_DP_DUAL_MODE_UNKNOWN) {
1810 1811 1812 1813
		/* An overridden EDID imply that we want this port for testing.
		 * Make sure not to set limits for that port.
		 */
		if (has_edid && !connector->override_edid &&
1814 1815 1816 1817 1818 1819 1820 1821 1822
		    intel_bios_is_port_dp_dual_mode(dev_priv, port)) {
			DRM_DEBUG_KMS("Assuming DP dual mode adaptor presence based on VBT\n");
			type = DRM_DP_DUAL_MODE_TYPE1_DVI;
		} else {
			type = DRM_DP_DUAL_MODE_NONE;
		}
	}

	if (type == DRM_DP_DUAL_MODE_NONE)
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
		return;

	hdmi->dp_dual_mode.type = type;
	hdmi->dp_dual_mode.max_tmds_clock =
		drm_dp_dual_mode_max_tmds_clock(type, adapter);

	DRM_DEBUG_KMS("DP dual mode adaptor (%s) detected (max TMDS clock: %d kHz)\n",
		      drm_dp_get_dual_mode_type_name(type),
		      hdmi->dp_dual_mode.max_tmds_clock);
}

1834
static bool
1835
intel_hdmi_set_edid(struct drm_connector *connector)
1836 1837 1838
{
	struct drm_i915_private *dev_priv = to_i915(connector->dev);
	struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
1839
	struct edid *edid;
1840
	bool connected = false;
1841
	struct i2c_adapter *i2c;
1842

1843
	intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
1844

1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
	i2c = intel_gmbus_get_adapter(dev_priv, intel_hdmi->ddc_bus);

	edid = drm_get_edid(connector, i2c);

	if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
		DRM_DEBUG_KMS("HDMI GMBUS EDID read failed, retry using GPIO bit-banging\n");
		intel_gmbus_force_bit(i2c, true);
		edid = drm_get_edid(connector, i2c);
		intel_gmbus_force_bit(i2c, false);
	}
1855

1856
	intel_hdmi_dp_dual_mode_detect(connector, edid != NULL);
1857

1858
	intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
1859

1860 1861 1862 1863 1864 1865
	to_intel_connector(connector)->detect_edid = edid;
	if (edid && edid->input & DRM_EDID_INPUT_DIGITAL) {
		intel_hdmi->rgb_quant_range_selectable =
			drm_rgb_quant_range_selectable(edid);

		intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
1866
		intel_hdmi->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
1867 1868

		connected = true;
1869 1870
	}

1871 1872 1873
	return connected;
}

1874 1875
static enum drm_connector_status
intel_hdmi_detect(struct drm_connector *connector, bool force)
1876
{
1877 1878
	enum drm_connector_status status;
	struct drm_i915_private *dev_priv = to_i915(connector->dev);
1879

1880 1881 1882
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
		      connector->base.id, connector->name);

1883 1884
	intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);

1885
	intel_hdmi_unset_edid(connector);
1886

1887
	if (intel_hdmi_set_edid(connector))
1888
		status = connector_status_connected;
1889
	else
1890
		status = connector_status_disconnected;
1891

1892 1893
	intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);

1894
	return status;
1895 1896
}

1897 1898
static void
intel_hdmi_force(struct drm_connector *connector)
1899
{
1900 1901
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
		      connector->base.id, connector->name);
1902

1903
	intel_hdmi_unset_edid(connector);
1904

1905 1906
	if (connector->status != connector_status_connected)
		return;
1907

1908
	intel_hdmi_set_edid(connector);
1909
}
1910

1911 1912 1913 1914 1915 1916 1917
static int intel_hdmi_get_modes(struct drm_connector *connector)
{
	struct edid *edid;

	edid = to_intel_connector(connector)->detect_edid;
	if (edid == NULL)
		return 0;
1918

1919
	return intel_connector_update_modes(connector, edid);
1920 1921
}

1922
static void intel_hdmi_pre_enable(struct intel_encoder *encoder,
1923 1924
				  const struct intel_crtc_state *pipe_config,
				  const struct drm_connector_state *conn_state)
1925
{
1926 1927
	struct intel_digital_port *intel_dig_port =
		enc_to_dig_port(&encoder->base);
1928

1929
	intel_hdmi_prepare(encoder, pipe_config);
1930

1931 1932 1933
	intel_dig_port->set_infoframes(&encoder->base,
				       pipe_config->has_infoframe,
				       pipe_config, conn_state);
1934 1935
}

1936
static void vlv_hdmi_pre_enable(struct intel_encoder *encoder,
1937 1938
				const struct intel_crtc_state *pipe_config,
				const struct drm_connector_state *conn_state)
1939 1940
{
	struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
1941
	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1942

1943
	vlv_phy_pre_encoder_enable(encoder, pipe_config);
1944

1945 1946 1947 1948
	/* HDMI 1.0V-2dB */
	vlv_set_phy_signal_level(encoder, 0x2b245f5f, 0x00002000, 0x5578b83a,
				 0x2b247878);

1949 1950 1951
	dport->set_infoframes(&encoder->base,
			      pipe_config->has_infoframe,
			      pipe_config, conn_state);
1952

1953
	g4x_enable_hdmi(encoder, pipe_config, conn_state);
1954

1955
	vlv_wait_port_ready(dev_priv, dport, 0x0);
1956 1957
}

1958
static void vlv_hdmi_pre_pll_enable(struct intel_encoder *encoder,
1959 1960
				    const struct intel_crtc_state *pipe_config,
				    const struct drm_connector_state *conn_state)
1961
{
1962
	intel_hdmi_prepare(encoder, pipe_config);
1963

1964
	vlv_phy_pre_pll_enable(encoder, pipe_config);
1965 1966
}

1967
static void chv_hdmi_pre_pll_enable(struct intel_encoder *encoder,
1968 1969
				    const struct intel_crtc_state *pipe_config,
				    const struct drm_connector_state *conn_state)
1970
{
1971
	intel_hdmi_prepare(encoder, pipe_config);
1972

1973
	chv_phy_pre_pll_enable(encoder, pipe_config);
1974 1975
}

1976
static void chv_hdmi_post_pll_disable(struct intel_encoder *encoder,
1977 1978
				      const struct intel_crtc_state *old_crtc_state,
				      const struct drm_connector_state *old_conn_state)
1979
{
1980
	chv_phy_post_pll_disable(encoder, old_crtc_state);
1981 1982
}

1983
static void vlv_hdmi_post_disable(struct intel_encoder *encoder,
1984 1985
				  const struct intel_crtc_state *old_crtc_state,
				  const struct drm_connector_state *old_conn_state)
1986 1987
{
	/* Reset lanes to avoid HDMI flicker (VLV w/a) */
1988
	vlv_phy_reset_lanes(encoder, old_crtc_state);
1989 1990
}

1991
static void chv_hdmi_post_disable(struct intel_encoder *encoder,
1992 1993
				  const struct intel_crtc_state *old_crtc_state,
				  const struct drm_connector_state *old_conn_state)
1994 1995
{
	struct drm_device *dev = encoder->base.dev;
1996
	struct drm_i915_private *dev_priv = to_i915(dev);
1997

V
Ville Syrjälä 已提交
1998
	mutex_lock(&dev_priv->sb_lock);
1999

2000
	/* Assert data lane reset */
2001
	chv_data_lane_soft_reset(encoder, old_crtc_state, true);
2002

V
Ville Syrjälä 已提交
2003
	mutex_unlock(&dev_priv->sb_lock);
2004 2005
}

2006
static void chv_hdmi_pre_enable(struct intel_encoder *encoder,
2007 2008
				const struct intel_crtc_state *pipe_config,
				const struct drm_connector_state *conn_state)
2009 2010 2011
{
	struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
	struct drm_device *dev = encoder->base.dev;
2012
	struct drm_i915_private *dev_priv = to_i915(dev);
2013

2014
	chv_phy_pre_encoder_enable(encoder, pipe_config);
2015

2016 2017
	/* FIXME: Program the support xxx V-dB */
	/* Use 800mV-0dB */
2018
	chv_set_phy_signal_level(encoder, 128, 102, false);
2019

2020 2021 2022
	dport->set_infoframes(&encoder->base,
			      pipe_config->has_infoframe,
			      pipe_config, conn_state);
2023

2024
	g4x_enable_hdmi(encoder, pipe_config, conn_state);
2025

2026
	vlv_wait_port_ready(dev_priv, dport, 0x0);
2027 2028

	/* Second common lane will stay alive on its own now */
2029
	chv_phy_release_cl2_override(encoder);
2030 2031
}

2032 2033
static void intel_hdmi_destroy(struct drm_connector *connector)
{
2034
	kfree(to_intel_connector(connector)->detect_edid);
2035
	drm_connector_cleanup(connector);
2036
	kfree(connector);
2037 2038 2039 2040
}

static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
	.detect = intel_hdmi_detect,
2041
	.force = intel_hdmi_force,
2042
	.fill_modes = drm_helper_probe_single_connector_modes,
2043 2044
	.atomic_get_property = intel_digital_connector_atomic_get_property,
	.atomic_set_property = intel_digital_connector_atomic_set_property,
2045
	.late_register = intel_connector_register,
2046
	.early_unregister = intel_connector_unregister,
2047
	.destroy = intel_hdmi_destroy,
2048
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
2049
	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
2050 2051 2052 2053 2054
};

static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
	.get_modes = intel_hdmi_get_modes,
	.mode_valid = intel_hdmi_mode_valid,
2055
	.atomic_check = intel_digital_connector_atomic_check,
2056 2057 2058
};

static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
C
Chris Wilson 已提交
2059
	.destroy = intel_encoder_destroy,
2060 2061
};

2062 2063 2064
static void
intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
{
2065
	intel_attach_force_audio_property(connector);
2066
	intel_attach_broadcast_rgb_property(connector);
2067
	intel_attach_aspect_ratio_property(connector);
2068
	connector->state->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
2069 2070
}

S
Shashank Sharma 已提交
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
/*
 * intel_hdmi_handle_sink_scrambling: handle sink scrambling/clock ratio setup
 * @encoder: intel_encoder
 * @connector: drm_connector
 * @high_tmds_clock_ratio = bool to indicate if the function needs to set
 *  or reset the high tmds clock ratio for scrambling
 * @scrambling: bool to Indicate if the function needs to set or reset
 *  sink scrambling
 *
 * This function handles scrambling on HDMI 2.0 capable sinks.
 * If required clock rate is > 340 Mhz && scrambling is supported by sink
 * it enables scrambling. This should be called before enabling the HDMI
 * 2.0 port, as the sink can choose to disable the scrambling if it doesn't
 * detect a scrambled clock within 100 ms.
2085 2086 2087
 *
 * Returns:
 * True on success, false on failure.
S
Shashank Sharma 已提交
2088
 */
2089
bool intel_hdmi_handle_sink_scrambling(struct intel_encoder *encoder,
S
Shashank Sharma 已提交
2090 2091 2092 2093
				       struct drm_connector *connector,
				       bool high_tmds_clock_ratio,
				       bool scrambling)
{
2094
	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
S
Shashank Sharma 已提交
2095 2096
	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
	struct drm_scrambling *sink_scrambling =
2097 2098 2099
		&connector->display_info.hdmi.scdc.scrambling;
	struct i2c_adapter *adapter =
		intel_gmbus_get_adapter(dev_priv, intel_hdmi->ddc_bus);
S
Shashank Sharma 已提交
2100 2101

	if (!sink_scrambling->supported)
2102
		return true;
S
Shashank Sharma 已提交
2103

2104 2105 2106
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] scrambling=%s, TMDS bit clock ratio=1/%d\n",
		      connector->base.id, connector->name,
		      yesno(scrambling), high_tmds_clock_ratio ? 40 : 10);
S
Shashank Sharma 已提交
2107

2108 2109 2110 2111
	/* Set TMDS bit clock ratio to 1/40 or 1/10, and enable/disable scrambling */
	return drm_scdc_set_high_tmds_clock_ratio(adapter,
						  high_tmds_clock_ratio) &&
		drm_scdc_set_scrambling(adapter, scrambling);
S
Shashank Sharma 已提交
2112 2113
}

2114
static u8 chv_port_to_ddc_pin(struct drm_i915_private *dev_priv, enum port port)
2115 2116 2117
{
	u8 ddc_pin;

2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
	switch (port) {
	case PORT_B:
		ddc_pin = GMBUS_PIN_DPB;
		break;
	case PORT_C:
		ddc_pin = GMBUS_PIN_DPC;
		break;
	case PORT_D:
		ddc_pin = GMBUS_PIN_DPD_CHV;
		break;
	default:
		MISSING_CASE(port);
		ddc_pin = GMBUS_PIN_DPB;
		break;
2132
	}
2133 2134 2135 2136 2137 2138
	return ddc_pin;
}

static u8 bxt_port_to_ddc_pin(struct drm_i915_private *dev_priv, enum port port)
{
	u8 ddc_pin;
2139 2140 2141

	switch (port) {
	case PORT_B:
2142
		ddc_pin = GMBUS_PIN_1_BXT;
2143 2144
		break;
	case PORT_C:
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
		ddc_pin = GMBUS_PIN_2_BXT;
		break;
	default:
		MISSING_CASE(port);
		ddc_pin = GMBUS_PIN_1_BXT;
		break;
	}
	return ddc_pin;
}

static u8 cnp_port_to_ddc_pin(struct drm_i915_private *dev_priv,
			      enum port port)
{
	u8 ddc_pin;

	switch (port) {
	case PORT_B:
		ddc_pin = GMBUS_PIN_1_BXT;
		break;
	case PORT_C:
		ddc_pin = GMBUS_PIN_2_BXT;
2166 2167
		break;
	case PORT_D:
2168 2169
		ddc_pin = GMBUS_PIN_4_CNP;
		break;
2170 2171 2172
	case PORT_F:
		ddc_pin = GMBUS_PIN_3_BXT;
		break;
2173 2174 2175 2176 2177 2178 2179 2180
	default:
		MISSING_CASE(port);
		ddc_pin = GMBUS_PIN_1_BXT;
		break;
	}
	return ddc_pin;
}

2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
static u8 icl_port_to_ddc_pin(struct drm_i915_private *dev_priv, enum port port)
{
	u8 ddc_pin;

	switch (port) {
	case PORT_A:
		ddc_pin = GMBUS_PIN_1_BXT;
		break;
	case PORT_B:
		ddc_pin = GMBUS_PIN_2_BXT;
		break;
	case PORT_C:
		ddc_pin = GMBUS_PIN_9_TC1_ICP;
		break;
	case PORT_D:
		ddc_pin = GMBUS_PIN_10_TC2_ICP;
		break;
	case PORT_E:
		ddc_pin = GMBUS_PIN_11_TC3_ICP;
		break;
	case PORT_F:
		ddc_pin = GMBUS_PIN_12_TC4_ICP;
		break;
	default:
		MISSING_CASE(port);
		ddc_pin = GMBUS_PIN_2_BXT;
		break;
	}
	return ddc_pin;
}

2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
static u8 g4x_port_to_ddc_pin(struct drm_i915_private *dev_priv,
			      enum port port)
{
	u8 ddc_pin;

	switch (port) {
	case PORT_B:
		ddc_pin = GMBUS_PIN_DPB;
		break;
	case PORT_C:
		ddc_pin = GMBUS_PIN_DPC;
		break;
	case PORT_D:
		ddc_pin = GMBUS_PIN_DPD;
2226 2227 2228 2229 2230 2231
		break;
	default:
		MISSING_CASE(port);
		ddc_pin = GMBUS_PIN_DPB;
		break;
	}
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253
	return ddc_pin;
}

static u8 intel_hdmi_ddc_pin(struct drm_i915_private *dev_priv,
			     enum port port)
{
	const struct ddi_vbt_port_info *info =
		&dev_priv->vbt.ddi_port_info[port];
	u8 ddc_pin;

	if (info->alternate_ddc_pin) {
		DRM_DEBUG_KMS("Using DDC pin 0x%x for port %c (VBT)\n",
			      info->alternate_ddc_pin, port_name(port));
		return info->alternate_ddc_pin;
	}

	if (IS_CHERRYVIEW(dev_priv))
		ddc_pin = chv_port_to_ddc_pin(dev_priv, port);
	else if (IS_GEN9_LP(dev_priv))
		ddc_pin = bxt_port_to_ddc_pin(dev_priv, port);
	else if (HAS_PCH_CNP(dev_priv))
		ddc_pin = cnp_port_to_ddc_pin(dev_priv, port);
2254 2255
	else if (IS_ICELAKE(dev_priv))
		ddc_pin = icl_port_to_ddc_pin(dev_priv, port);
2256 2257
	else
		ddc_pin = g4x_port_to_ddc_pin(dev_priv, port);
2258 2259 2260 2261 2262 2263 2264

	DRM_DEBUG_KMS("Using DDC pin 0x%x for port %c (platform default)\n",
		      ddc_pin, port_name(port));

	return ddc_pin;
}

2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
void intel_infoframe_init(struct intel_digital_port *intel_dig_port)
{
	struct drm_i915_private *dev_priv =
		to_i915(intel_dig_port->base.base.dev);

	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
		intel_dig_port->write_infoframe = vlv_write_infoframe;
		intel_dig_port->set_infoframes = vlv_set_infoframes;
		intel_dig_port->infoframe_enabled = vlv_infoframe_enabled;
	} else if (IS_G4X(dev_priv)) {
		intel_dig_port->write_infoframe = g4x_write_infoframe;
		intel_dig_port->set_infoframes = g4x_set_infoframes;
		intel_dig_port->infoframe_enabled = g4x_infoframe_enabled;
	} else if (HAS_DDI(dev_priv)) {
		intel_dig_port->write_infoframe = hsw_write_infoframe;
		intel_dig_port->set_infoframes = hsw_set_infoframes;
		intel_dig_port->infoframe_enabled = hsw_infoframe_enabled;
	} else if (HAS_PCH_IBX(dev_priv)) {
		intel_dig_port->write_infoframe = ibx_write_infoframe;
		intel_dig_port->set_infoframes = ibx_set_infoframes;
		intel_dig_port->infoframe_enabled = ibx_infoframe_enabled;
	} else {
		intel_dig_port->write_infoframe = cpt_write_infoframe;
		intel_dig_port->set_infoframes = cpt_set_infoframes;
		intel_dig_port->infoframe_enabled = cpt_infoframe_enabled;
	}
}

P
Paulo Zanoni 已提交
2293 2294
void intel_hdmi_init_connector(struct intel_digital_port *intel_dig_port,
			       struct intel_connector *intel_connector)
2295
{
2296 2297 2298 2299
	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;
2300
	struct drm_i915_private *dev_priv = to_i915(dev);
2301
	enum port port = intel_encoder->port;
2302

2303 2304 2305
	DRM_DEBUG_KMS("Adding HDMI connector on port %c\n",
		      port_name(port));

2306 2307 2308 2309 2310
	if (WARN(intel_dig_port->max_lanes < 4,
		 "Not enough lanes (%d) for HDMI on port %c\n",
		 intel_dig_port->max_lanes, port_name(port)))
		return;

2311
	drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
2312
			   DRM_MODE_CONNECTOR_HDMIA);
2313 2314
	drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);

2315
	connector->interlace_allowed = 1;
2316
	connector->doublescan_allowed = 0;
2317
	connector->stereo_allowed = 1;
2318

2319
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
2320 2321
		connector->ycbcr_420_allowed = true;

2322 2323
	intel_hdmi->ddc_bus = intel_hdmi_ddc_pin(dev_priv, port);

2324
	if (WARN_ON(port == PORT_A))
2325
		return;
2326
	intel_encoder->hpd_pin = intel_hpd_pin_default(dev_priv, port);
2327

2328
	if (HAS_DDI(dev_priv))
2329 2330 2331
		intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
	else
		intel_connector->get_hw_state = intel_connector_get_hw_state;
2332 2333 2334

	intel_hdmi_add_properties(intel_hdmi, connector);

2335
	if (is_hdcp_supported(dev_priv, port)) {
S
Sean Paul 已提交
2336 2337 2338 2339 2340 2341
		int ret = intel_hdcp_init(intel_connector,
					  &intel_hdmi_hdcp_shim);
		if (ret)
			DRM_DEBUG_KMS("HDCP init failed, skipping.\n");
	}

2342
	intel_connector_attach_encoder(intel_connector, intel_encoder);
2343
	intel_hdmi->attached_connector = intel_connector;
2344 2345 2346 2347 2348

	/* 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.
	 */
2349
	if (IS_G4X(dev_priv) && !IS_GM45(dev_priv)) {
2350 2351 2352 2353 2354
		u32 temp = I915_READ(PEG_BAND_GAP_DATA);
		I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
	}
}

2355
void intel_hdmi_init(struct drm_i915_private *dev_priv,
2356
		     i915_reg_t hdmi_reg, enum port port)
2357 2358 2359 2360 2361
{
	struct intel_digital_port *intel_dig_port;
	struct intel_encoder *intel_encoder;
	struct intel_connector *intel_connector;

2362
	intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
2363 2364 2365
	if (!intel_dig_port)
		return;

2366
	intel_connector = intel_connector_alloc();
2367 2368 2369 2370 2371 2372 2373
	if (!intel_connector) {
		kfree(intel_dig_port);
		return;
	}

	intel_encoder = &intel_dig_port->base;

2374 2375 2376
	drm_encoder_init(&dev_priv->drm, &intel_encoder->base,
			 &intel_hdmi_enc_funcs, DRM_MODE_ENCODER_TMDS,
			 "HDMI %c", port_name(port));
P
Paulo Zanoni 已提交
2377

2378
	intel_encoder->hotplug = intel_encoder_hotplug;
2379
	intel_encoder->compute_config = intel_hdmi_compute_config;
2380
	if (HAS_PCH_SPLIT(dev_priv)) {
2381 2382 2383 2384 2385
		intel_encoder->disable = pch_disable_hdmi;
		intel_encoder->post_disable = pch_post_disable_hdmi;
	} else {
		intel_encoder->disable = g4x_disable_hdmi;
	}
P
Paulo Zanoni 已提交
2386
	intel_encoder->get_hw_state = intel_hdmi_get_hw_state;
2387
	intel_encoder->get_config = intel_hdmi_get_config;
2388
	if (IS_CHERRYVIEW(dev_priv)) {
2389
		intel_encoder->pre_pll_enable = chv_hdmi_pre_pll_enable;
2390 2391
		intel_encoder->pre_enable = chv_hdmi_pre_enable;
		intel_encoder->enable = vlv_enable_hdmi;
2392
		intel_encoder->post_disable = chv_hdmi_post_disable;
2393
		intel_encoder->post_pll_disable = chv_hdmi_post_pll_disable;
2394
	} else if (IS_VALLEYVIEW(dev_priv)) {
2395 2396
		intel_encoder->pre_pll_enable = vlv_hdmi_pre_pll_enable;
		intel_encoder->pre_enable = vlv_hdmi_pre_enable;
2397
		intel_encoder->enable = vlv_enable_hdmi;
2398
		intel_encoder->post_disable = vlv_hdmi_post_disable;
2399
	} else {
2400
		intel_encoder->pre_enable = intel_hdmi_pre_enable;
2401
		if (HAS_PCH_CPT(dev_priv))
2402
			intel_encoder->enable = cpt_enable_hdmi;
2403
		else if (HAS_PCH_IBX(dev_priv))
2404
			intel_encoder->enable = ibx_enable_hdmi;
2405
		else
2406
			intel_encoder->enable = g4x_enable_hdmi;
2407
	}
2408

2409
	intel_encoder->type = INTEL_OUTPUT_HDMI;
2410
	intel_encoder->power_domain = intel_port_to_power_domain(port);
2411
	intel_encoder->port = port;
2412
	if (IS_CHERRYVIEW(dev_priv)) {
2413 2414 2415 2416 2417 2418 2419
		if (port == PORT_D)
			intel_encoder->crtc_mask = 1 << 2;
		else
			intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
	} else {
		intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
	}
2420
	intel_encoder->cloneable = 1 << INTEL_OUTPUT_ANALOG;
2421 2422 2423 2424 2425
	/*
	 * BSpec is unclear about HDMI+HDMI cloning on g4x, but it seems
	 * to work on real hardware. And since g4x can send infoframes to
	 * only one port anyway, nothing is lost by allowing it.
	 */
2426
	if (IS_G4X(dev_priv))
2427
		intel_encoder->cloneable |= 1 << INTEL_OUTPUT_HDMI;
2428

2429
	intel_dig_port->hdmi.hdmi_reg = hdmi_reg;
2430
	intel_dig_port->dp.output_reg = INVALID_MMIO_REG;
2431
	intel_dig_port->max_lanes = 4;
2432

2433 2434
	intel_infoframe_init(intel_dig_port);

2435
	intel_hdmi_init_connector(intel_dig_port, intel_connector);
2436
}