vc4_hdmi.c 97.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2015 Broadcom
 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
 * Copyright (C) 2013 Red Hat
 * Author: Rob Clark <robdclark@gmail.com>
 */

/**
 * DOC: VC4 Falcon HDMI module
 *
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * The HDMI core has a state machine and a PHY.  On BCM2835, most of
 * the unit operates off of the HSM clock from CPRMAN.  It also
 * internally uses the PLLH_PIX clock for the PHY.
 *
 * HDMI infoframes are kept within a small packet ram, where each
 * packet can be individually enabled for including in a frame.
 *
 * HDMI audio is implemented entirely within the HDMI IP block.  A
 * register in the HDMI encoder takes SPDIF frames from the DMA engine
 * and transfers them over an internal MAI (multi-channel audio
 * interconnect) bus to the encoder side for insertion into the video
 * blank regions.
 *
 * The driver's HDMI encoder does not yet support power management.
 * The HDMI encoder's power domain and the HSM/pixel clocks are kept
 * continuously running, and only the HDMI logic and packet ram are
 * powered off/on at disable/enable time.
 *
 * The driver does not yet support CEC control, though the HDMI
 * encoder block has CEC support.
32 33
 */

34
#include <drm/display/drm_hdmi_helper.h>
35
#include <drm/display/drm_scdc_helper.h>
36
#include <drm/drm_atomic_helper.h>
37
#include <drm/drm_drv.h>
38
#include <drm/drm_probe_helper.h>
T
Thomas Zimmermann 已提交
39
#include <drm/drm_simple_kms_helper.h>
40 41
#include <linux/clk.h>
#include <linux/component.h>
42
#include <linux/gpio/consumer.h>
43 44 45 46 47
#include <linux/i2c.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <linux/rational.h>
48
#include <linux/reset.h>
49
#include <sound/dmaengine_pcm.h>
50
#include <sound/hdmi-codec.h>
51 52 53
#include <sound/pcm_drm_eld.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
H
Hans Verkuil 已提交
54
#include "media/cec.h"
55
#include "vc4_drv.h"
56
#include "vc4_hdmi.h"
57
#include "vc4_hdmi_regs.h"
58 59
#include "vc4_regs.h"

60 61 62 63 64 65
#define VC5_HDMI_HORZA_HFP_SHIFT		16
#define VC5_HDMI_HORZA_HFP_MASK			VC4_MASK(28, 16)
#define VC5_HDMI_HORZA_VPOS			BIT(15)
#define VC5_HDMI_HORZA_HPOS			BIT(14)
#define VC5_HDMI_HORZA_HAP_SHIFT		0
#define VC5_HDMI_HORZA_HAP_MASK			VC4_MASK(13, 0)
E
Eric Anholt 已提交
66

67 68 69 70
#define VC5_HDMI_HORZB_HBP_SHIFT		16
#define VC5_HDMI_HORZB_HBP_MASK			VC4_MASK(26, 16)
#define VC5_HDMI_HORZB_HSP_SHIFT		0
#define VC5_HDMI_HORZB_HSP_MASK			VC4_MASK(10, 0)
71

72 73 74 75 76 77
#define VC5_HDMI_VERTA_VSP_SHIFT		24
#define VC5_HDMI_VERTA_VSP_MASK			VC4_MASK(28, 24)
#define VC5_HDMI_VERTA_VFP_SHIFT		16
#define VC5_HDMI_VERTA_VFP_MASK			VC4_MASK(22, 16)
#define VC5_HDMI_VERTA_VAL_SHIFT		0
#define VC5_HDMI_VERTA_VAL_MASK			VC4_MASK(12, 0)
E
Eric Anholt 已提交
78

79 80
#define VC5_HDMI_VERTB_VSPO_SHIFT		16
#define VC5_HDMI_VERTB_VSPO_MASK		VC4_MASK(29, 16)
81

82 83
#define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT	0
#define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK	VC4_MASK(3, 0)
84 85 86
#define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT	0
#define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK	VC4_MASK(3, 0)

87 88
#define VC5_HDMI_SCRAMBLER_CTL_ENABLE		BIT(0)

89 90 91 92 93 94 95 96 97 98 99
#define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT	8
#define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK	VC4_MASK(10, 8)

#define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT		0
#define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK		VC4_MASK(3, 0)

#define VC5_HDMI_GCP_CONFIG_GCP_ENABLE		BIT(31)

#define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT	8
#define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK	VC4_MASK(15, 8)

100 101
# define VC4_HD_M_SW_RST			BIT(2)
# define VC4_HD_M_ENABLE			BIT(0)
H
Hans Verkuil 已提交
102

103
#define HSM_MIN_CLOCK_FREQ	120000000
H
Hans Verkuil 已提交
104
#define CEC_CLOCK_FREQ 40000
105

106 107
#define HDMI_14_MAX_TMDS_CLK   (340 * 1000 * 1000)

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
static const char * const output_format_str[] = {
	[VC4_HDMI_OUTPUT_RGB]		= "RGB",
	[VC4_HDMI_OUTPUT_YUV420]	= "YUV 4:2:0",
	[VC4_HDMI_OUTPUT_YUV422]	= "YUV 4:2:2",
	[VC4_HDMI_OUTPUT_YUV444]	= "YUV 4:4:4",
};

static const char *vc4_hdmi_output_fmt_str(enum vc4_hdmi_output_format fmt)
{
	if (fmt >= ARRAY_SIZE(output_format_str))
		return "invalid";

	return output_format_str[fmt];
}

123 124
static unsigned long long
vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
125
				    unsigned int bpc, enum vc4_hdmi_output_format fmt);
126 127

static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
128 129
					   unsigned int bpc,
					   enum vc4_hdmi_output_format fmt)
130
{
131
	unsigned long long clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
132 133

	return clock > HDMI_14_MAX_TMDS_CLK;
134 135
}

136 137 138
static bool vc4_hdmi_is_full_range_rgb(struct vc4_hdmi *vc4_hdmi,
				       const struct drm_display_mode *mode)
{
139
	struct drm_display_info *display = &vc4_hdmi->connector.display_info;
140

141
	return !display->is_hdmi ||
142 143 144
		drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
}

145
static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
146 147
{
	struct drm_info_node *node = (struct drm_info_node *)m->private;
148
	struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
149
	struct drm_device *drm = vc4_hdmi->connector.dev;
150
	struct drm_printer p = drm_seq_file_printer(m);
151 152 153 154
	int idx;

	if (!drm_dev_enter(drm, &idx))
		return -ENODEV;
155

156 157
	drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
	drm_print_regset32(&p, &vc4_hdmi->hd_regset);
158 159 160 161 162 163
	drm_print_regset32(&p, &vc4_hdmi->cec_regset);
	drm_print_regset32(&p, &vc4_hdmi->csc_regset);
	drm_print_regset32(&p, &vc4_hdmi->dvp_regset);
	drm_print_regset32(&p, &vc4_hdmi->phy_regset);
	drm_print_regset32(&p, &vc4_hdmi->ram_regset);
	drm_print_regset32(&p, &vc4_hdmi->rm_regset);
164

165 166
	drm_dev_exit(idx);

167 168 169
	return 0;
}

170
static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
171
{
172
	struct drm_device *drm = vc4_hdmi->connector.dev;
173
	unsigned long flags;
174 175 176 177 178 179 180 181
	int idx;

	/*
	 * We can be called by our bind callback, when the
	 * connector->dev pointer might not be initialised yet.
	 */
	if (drm && !drm_dev_enter(drm, &idx))
		return;
182 183 184

	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

185 186 187
	HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
	udelay(1);
	HDMI_WRITE(HDMI_M_CTL, 0);
188

189
	HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
190

191 192 193
	HDMI_WRITE(HDMI_SW_RESET_CONTROL,
		   VC4_HDMI_SW_RESET_HDMI |
		   VC4_HDMI_SW_RESET_FORMAT_DETECT);
194

195
	HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
196 197

	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
198 199 200

	if (drm)
		drm_dev_exit(idx);
201 202
}

203
static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
204
{
205
	struct drm_device *drm = vc4_hdmi->connector.dev;
206
	unsigned long flags;
207 208 209 210 211 212 213 214
	int idx;

	/*
	 * We can be called by our bind callback, when the
	 * connector->dev pointer might not be initialised yet.
	 */
	if (drm && !drm_dev_enter(drm, &idx))
		return;
215

216
	reset_control_reset(vc4_hdmi->reset);
217

218 219
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

220
	HDMI_WRITE(HDMI_DVP_CTL, 0);
221

222 223
	HDMI_WRITE(HDMI_CLOCK_STOP,
		   HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
224 225

	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
226 227 228

	if (drm)
		drm_dev_exit(idx);
229 230
}

231 232 233
#ifdef CONFIG_DRM_VC4_HDMI_CEC
static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
{
234 235
	struct drm_device *drm = vc4_hdmi->connector.dev;
	unsigned long cec_rate;
236
	unsigned long flags;
237 238
	u16 clk_cnt;
	u32 value;
239 240 241 242 243 244 245 246 247 248 249 250
	int idx;

	/*
	 * This function is called by our runtime_resume implementation
	 * and thus at bind time, when we haven't registered our
	 * connector yet and thus don't have a pointer to the DRM
	 * device.
	 */
	if (drm && !drm_dev_enter(drm, &idx))
		return;

	cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
251

252 253
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

254 255 256 257 258 259 260
	value = HDMI_READ(HDMI_CEC_CNTRL_1);
	value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;

	/*
	 * Set the clock divider: the hsm_clock rate and this divider
	 * setting will give a 40 kHz CEC clock.
	 */
261
	clk_cnt = cec_rate / CEC_CLOCK_FREQ;
262 263
	value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
	HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
264 265

	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
266 267 268

	if (drm)
		drm_dev_exit(idx);
269 270 271 272 273
}
#else
static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
#endif

274 275
static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder);

276 277 278
static enum drm_connector_status
vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
{
279
	struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
280
	bool connected = false;
281

282 283
	mutex_lock(&vc4_hdmi->mutex);

284 285
	WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));

286 287 288
	if (vc4_hdmi->hpd_gpio) {
		if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
			connected = true;
289
	} else {
290 291
		if (vc4_hdmi->variant->hp_detect &&
		    vc4_hdmi->variant->hp_detect(vc4_hdmi))
292
			connected = true;
293 294
	}

295 296 297 298 299 300 301 302 303
	if (connected) {
		if (connector->status != connector_status_connected) {
			struct edid *edid = drm_get_edid(connector, vc4_hdmi->ddc);

			if (edid) {
				cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
				kfree(edid);
			}
		}
304

305
		vc4_hdmi_enable_scrambling(&vc4_hdmi->encoder.base);
306
		pm_runtime_put(&vc4_hdmi->pdev->dev);
307
		mutex_unlock(&vc4_hdmi->mutex);
308
		return connector_status_connected;
309 310
	}

311
	cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
312
	pm_runtime_put(&vc4_hdmi->pdev->dev);
313
	mutex_unlock(&vc4_hdmi->mutex);
H
Hans Verkuil 已提交
314
	return connector_status_disconnected;
315 316 317 318
}

static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
{
319
	struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
320 321 322
	int ret = 0;
	struct edid *edid;

323 324
	mutex_lock(&vc4_hdmi->mutex);

325 326
	edid = drm_get_edid(connector, vc4_hdmi->ddc);
	cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
327 328 329 330
	if (!edid) {
		ret = -ENODEV;
		goto out;
	}
331

332
	drm_connector_update_edid_property(connector, edid);
333
	ret = drm_add_edid_modes(connector, edid);
E
Eric Anholt 已提交
334
	kfree(edid);
335

336 337 338 339 340
	if (vc4_hdmi->disable_4kp60) {
		struct drm_device *drm = connector->dev;
		struct drm_display_mode *mode;

		list_for_each_entry(mode, &connector->probed_modes, head) {
341
			if (vc4_hdmi_mode_needs_scrambling(mode, 8, VC4_HDMI_OUTPUT_RGB)) {
342 343 344 345 346 347
				drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
				drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
			}
		}
	}

348 349 350
out:
	mutex_unlock(&vc4_hdmi->mutex);

351 352 353
	return ret;
}

354 355 356 357 358 359 360 361 362 363 364 365
static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
					   struct drm_atomic_state *state)
{
	struct drm_connector_state *old_state =
		drm_atomic_get_old_connector_state(state, connector);
	struct drm_connector_state *new_state =
		drm_atomic_get_new_connector_state(state, connector);
	struct drm_crtc *crtc = new_state->crtc;

	if (!crtc)
		return 0;

366 367
	if (old_state->colorspace != new_state->colorspace ||
	    !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
368 369 370 371 372 373 374 375 376 377 378 379
		struct drm_crtc_state *crtc_state;

		crtc_state = drm_atomic_get_crtc_state(state, crtc);
		if (IS_ERR(crtc_state))
			return PTR_ERR(crtc_state);

		crtc_state->mode_changed = true;
	}

	return 0;
}

380 381
static void vc4_hdmi_connector_reset(struct drm_connector *connector)
{
382 383 384 385
	struct vc4_hdmi_connector_state *old_state =
		conn_state_to_vc4_hdmi_conn_state(connector->state);
	struct vc4_hdmi_connector_state *new_state =
		kzalloc(sizeof(*new_state), GFP_KERNEL);
386 387

	if (connector->state)
388 389 390 391 392 393 394 395
		__drm_atomic_helper_connector_destroy_state(connector->state);

	kfree(old_state);
	__drm_atomic_helper_connector_reset(connector, &new_state->base);

	if (!new_state)
		return;

396 397
	new_state->base.max_bpc = 8;
	new_state->base.max_requested_bpc = 8;
398
	new_state->output_format = VC4_HDMI_OUTPUT_RGB;
399 400 401 402 403 404 405 406 407 408 409 410 411 412
	drm_atomic_helper_connector_tv_reset(connector);
}

static struct drm_connector_state *
vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
{
	struct drm_connector_state *conn_state = connector->state;
	struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
	struct vc4_hdmi_connector_state *new_state;

	new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
	if (!new_state)
		return NULL;

413
	new_state->tmds_char_rate = vc4_state->tmds_char_rate;
414
	new_state->output_bpc = vc4_state->output_bpc;
415
	new_state->output_format = vc4_state->output_format;
416 417 418
	__drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);

	return &new_state->base;
419 420
}

421 422
static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
	.detect = vc4_hdmi_connector_detect,
423
	.fill_modes = drm_helper_probe_single_connector_modes,
424
	.reset = vc4_hdmi_connector_reset,
425
	.atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
426 427 428 429 430
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
};

static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
	.get_modes = vc4_hdmi_connector_get_modes,
431
	.atomic_check = vc4_hdmi_connector_atomic_check,
432 433
};

434
static int vc4_hdmi_connector_init(struct drm_device *dev,
435
				   struct vc4_hdmi *vc4_hdmi)
436
{
437
	struct drm_connector *connector = &vc4_hdmi->connector;
438
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
439
	int ret;
440

441 442 443 444 445 446 447
	ret = drmm_connector_init(dev, connector,
				  &vc4_hdmi_connector_funcs,
				  DRM_MODE_CONNECTOR_HDMIA,
				  vc4_hdmi->ddc);
	if (ret)
		return ret;

448 449
	drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);

450 451 452 453 454 455 456
	/*
	 * Some of the properties below require access to state, like bpc.
	 * Allocate some default initial connector state with our reset helper.
	 */
	if (connector->funcs->reset)
		connector->funcs->reset(connector);

457 458 459
	/* Create and attach TV margin props to this connector. */
	ret = drm_mode_create_tv_margin_properties(dev);
	if (ret)
460
		return ret;
461

462 463 464 465 466
	ret = drm_mode_create_hdmi_colorspace_property(connector);
	if (ret)
		return ret;

	drm_connector_attach_colorspace_property(connector);
467
	drm_connector_attach_tv_margin_properties(connector);
468
	drm_connector_attach_max_bpc_property(connector, 8, 12);
469

470 471 472
	connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
			     DRM_CONNECTOR_POLL_DISCONNECT);

473
	connector->interlace_allowed = 1;
474
	connector->doublescan_allowed = 0;
475
	connector->stereo_allowed = 1;
476

477 478 479
	if (vc4_hdmi->variant->supports_hdr)
		drm_connector_attach_hdr_output_metadata_property(connector);

480
	drm_connector_attach_encoder(connector, encoder);
481

482
	return 0;
483 484
}

485
static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
486 487
				enum hdmi_infoframe_type type,
				bool poll)
488
{
489
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
490
	struct drm_device *drm = vc4_hdmi->connector.dev;
491
	u32 packet_id = type - 0x80;
492
	unsigned long flags;
493 494 495 496 497
	int ret = 0;
	int idx;

	if (!drm_dev_enter(drm, &idx))
		return -ENODEV;
498

499
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
500 501
	HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
		   HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
502
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
503

504 505 506 507
	if (poll) {
		ret = wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
				 BIT(packet_id)), 100);
	}
508

509 510
	drm_dev_exit(idx);
	return ret;
511 512 513 514 515
}

static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
				     union hdmi_infoframe *frame)
{
516
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
517
	struct drm_device *drm = vc4_hdmi->connector.dev;
518
	u32 packet_id = frame->any.type - 0x80;
519 520 521
	const struct vc4_hdmi_register *ram_packet_start =
		&vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
	u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
522 523
	u32 packet_reg_next = ram_packet_start->offset +
		VC4_HDMI_PACKET_STRIDE * (packet_id + 1);
524 525
	void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
						       ram_packet_start->reg);
526
	uint8_t buffer[VC4_HDMI_PACKET_STRIDE] = {};
527
	unsigned long flags;
528 529
	ssize_t len, i;
	int ret;
530 531 532 533
	int idx;

	if (!drm_dev_enter(drm, &idx))
		return;
534

535
	WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
536 537 538 539 540
		    VC4_HDMI_RAM_PACKET_ENABLE),
		  "Packet RAM has to be on to store the packet.");

	len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
	if (len < 0)
541
		goto out;
542

543
	ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
544 545
	if (ret) {
		DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
546
		goto out;
547 548
	}

549 550
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

551
	for (i = 0; i < len; i += 7) {
552 553 554 555
		writel(buffer[i + 0] << 0 |
		       buffer[i + 1] << 8 |
		       buffer[i + 2] << 16,
		       base + packet_reg);
556 557
		packet_reg += 4;

558 559 560 561 562
		writel(buffer[i + 3] << 0 |
		       buffer[i + 4] << 8 |
		       buffer[i + 5] << 16 |
		       buffer[i + 6] << 24,
		       base + packet_reg);
563 564 565
		packet_reg += 4;
	}

566 567 568 569 570 571 572
	/*
	 * clear remainder of packet ram as it's included in the
	 * infoframe and triggers a checksum error on hdmi analyser
	 */
	for (; packet_reg < packet_reg_next; packet_reg += 4)
		writel(0, base + packet_reg);

573 574
	HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
		   HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
575 576 577

	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

578
	ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
579 580 581
			BIT(packet_id)), 100);
	if (ret)
		DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
582 583 584

out:
	drm_dev_exit(idx);
585 586
}

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
static void vc4_hdmi_avi_infoframe_colorspace(struct hdmi_avi_infoframe *frame,
					      enum vc4_hdmi_output_format fmt)
{
	switch (fmt) {
	case VC4_HDMI_OUTPUT_RGB:
		frame->colorspace = HDMI_COLORSPACE_RGB;
		break;

	case VC4_HDMI_OUTPUT_YUV420:
		frame->colorspace = HDMI_COLORSPACE_YUV420;
		break;

	case VC4_HDMI_OUTPUT_YUV422:
		frame->colorspace = HDMI_COLORSPACE_YUV422;
		break;

	case VC4_HDMI_OUTPUT_YUV444:
		frame->colorspace = HDMI_COLORSPACE_YUV444;
		break;

	default:
		break;
	}
}

612 613
static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
{
614
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
615
	struct drm_connector *connector = &vc4_hdmi->connector;
616
	struct drm_connector_state *cstate = connector->state;
617 618
	struct vc4_hdmi_connector_state *vc4_state =
		conn_state_to_vc4_hdmi_conn_state(cstate);
619
	const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
620 621 622
	union hdmi_infoframe frame;
	int ret;

623 624
	lockdep_assert_held(&vc4_hdmi->mutex);

625
	ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
626
						       connector, mode);
627 628 629 630 631
	if (ret < 0) {
		DRM_ERROR("couldn't fill AVI infoframe\n");
		return;
	}

632
	drm_hdmi_avi_infoframe_quant_range(&frame.avi,
633
					   connector, mode,
634 635 636
					   vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode) ?
					   HDMI_QUANTIZATION_RANGE_FULL :
					   HDMI_QUANTIZATION_RANGE_LIMITED);
637
	drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
638
	vc4_hdmi_avi_infoframe_colorspace(&frame.avi, vc4_state->output_format);
639
	drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
640

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
	vc4_hdmi_write_infoframe(encoder, &frame);
}

static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
{
	union hdmi_infoframe frame;
	int ret;

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

	frame.spd.sdi = HDMI_SPD_SDI_PC;

	vc4_hdmi_write_infoframe(encoder, &frame);
}

E
Eric Anholt 已提交
660 661
static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
{
662
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
663
	struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
E
Eric Anholt 已提交
664 665
	union hdmi_infoframe frame;

666
	memcpy(&frame.audio, audio, sizeof(*audio));
667 668 669

	if (vc4_hdmi->packet_ram_enabled)
		vc4_hdmi_write_infoframe(encoder, &frame);
E
Eric Anholt 已提交
670 671
}

672 673 674 675 676 677 678
static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
	struct drm_connector *connector = &vc4_hdmi->connector;
	struct drm_connector_state *conn_state = connector->state;
	union hdmi_infoframe frame;

679 680
	lockdep_assert_held(&vc4_hdmi->mutex);

681 682 683 684 685 686 687 688 689 690 691 692
	if (!vc4_hdmi->variant->supports_hdr)
		return;

	if (!conn_state->hdr_output_metadata)
		return;

	if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
		return;

	vc4_hdmi_write_infoframe(encoder, &frame);
}

693 694
static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
{
695 696
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);

697 698
	lockdep_assert_held(&vc4_hdmi->mutex);

699 700
	vc4_hdmi_set_avi_infoframe(encoder);
	vc4_hdmi_set_spd_infoframe(encoder);
701 702 703 704 705 706
	/*
	 * If audio was streaming, then we need to reenabled the audio
	 * infoframe here during encoder_enable.
	 */
	if (vc4_hdmi->audio.streaming)
		vc4_hdmi_set_audio_infoframe(encoder);
707 708

	vc4_hdmi_set_hdr_infoframe(encoder);
709 710
}

711 712 713 714 715 716
static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder,
					 struct drm_display_mode *mode)
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
	struct drm_display_info *display = &vc4_hdmi->connector.display_info;

717 718
	lockdep_assert_held(&vc4_hdmi->mutex);

719
	if (!display->is_hdmi)
720 721 722 723 724 725 726 727 728
		return false;

	if (!display->hdmi.scdc.supported ||
	    !display->hdmi.scdc.scrambling.supported)
		return false;

	return true;
}

729 730
#define SCRAMBLING_POLLING_DELAY_MS	1000

731 732 733
static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
734
	struct drm_device *drm = vc4_hdmi->connector.dev;
735
	struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
736
	unsigned long flags;
737
	int idx;
738

739 740
	lockdep_assert_held(&vc4_hdmi->mutex);

741 742 743
	if (!vc4_hdmi_supports_scrambling(encoder, mode))
		return;

744 745 746
	if (!vc4_hdmi_mode_needs_scrambling(mode,
					    vc4_hdmi->output_bpc,
					    vc4_hdmi->output_format))
747 748
		return;

749 750 751
	if (!drm_dev_enter(drm, &idx))
		return;

752 753 754
	drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
	drm_scdc_set_scrambling(vc4_hdmi->ddc, true);

755
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
756 757
	HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
		   VC5_HDMI_SCRAMBLER_CTL_ENABLE);
758
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
759

760 761
	drm_dev_exit(idx);

762 763
	vc4_hdmi->scdc_enabled = true;

764 765
	queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
			   msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
766 767 768 769 770
}

static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
771
	struct drm_device *drm = vc4_hdmi->connector.dev;
772
	unsigned long flags;
773
	int idx;
774

775 776
	lockdep_assert_held(&vc4_hdmi->mutex);

777
	if (!vc4_hdmi->scdc_enabled)
778 779
		return;

780
	vc4_hdmi->scdc_enabled = false;
781

782 783 784
	if (delayed_work_pending(&vc4_hdmi->scrambling_work))
		cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);

785 786 787
	if (!drm_dev_enter(drm, &idx))
		return;

788
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
789 790
	HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
		   ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
791
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
792 793 794

	drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
	drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
795 796

	drm_dev_exit(idx);
797 798
}

799 800 801 802 803 804 805 806 807 808 809 810 811 812
static void vc4_hdmi_scrambling_wq(struct work_struct *work)
{
	struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
						 struct vc4_hdmi,
						 scrambling_work);

	if (drm_scdc_get_scrambling_status(vc4_hdmi->ddc))
		return;

	drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
	drm_scdc_set_scrambling(vc4_hdmi->ddc, true);

	queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
			   msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
813 814
}

815 816
static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
					       struct drm_atomic_state *state)
817
{
818
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
819
	struct drm_device *drm = vc4_hdmi->connector.dev;
820
	unsigned long flags;
821
	int idx;
822

823 824
	mutex_lock(&vc4_hdmi->mutex);

825 826
	vc4_hdmi->packet_ram_enabled = false;

827 828 829
	if (!drm_dev_enter(drm, &idx))
		goto out;

830
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
831

832
	HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
833

834
	HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
835

836 837
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

838
	mdelay(1);
839

840
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
841 842
	HDMI_WRITE(HDMI_VID_CTL,
		   HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
843 844
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

845
	vc4_hdmi_disable_scrambling(encoder);
846

847 848 849
	drm_dev_exit(idx);

out:
850
	mutex_unlock(&vc4_hdmi->mutex);
851 852
}

853 854
static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
						 struct drm_atomic_state *state)
855 856
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
857
	struct drm_device *drm = vc4_hdmi->connector.dev;
858
	unsigned long flags;
859
	int ret;
860
	int idx;
861

862 863
	mutex_lock(&vc4_hdmi->mutex);

864 865 866
	if (!drm_dev_enter(drm, &idx))
		goto out;

867
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
868 869
	HDMI_WRITE(HDMI_VID_CTL,
		   HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
870
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
871

872 873
	if (vc4_hdmi->variant->phy_disable)
		vc4_hdmi->variant->phy_disable(vc4_hdmi);
874

875
	clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
876
	clk_disable_unprepare(vc4_hdmi->pixel_clock);
877

878
	ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
879 880
	if (ret < 0)
		DRM_ERROR("Failed to release power domain: %d\n", ret);
881

882 883 884
	drm_dev_exit(idx);

out:
885
	mutex_unlock(&vc4_hdmi->mutex);
886 887
}

888
static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
889
			       struct drm_connector_state *state,
890
			       const struct drm_display_mode *mode)
891
{
892
	struct drm_device *drm = vc4_hdmi->connector.dev;
893
	unsigned long flags;
894
	u32 csc_ctl;
895 896 897 898
	int idx;

	if (!drm_dev_enter(drm, &idx))
		return;
899

900 901
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

902 903 904
	csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
				VC4_HD_CSC_CTL_ORDER);

905
	if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode)) {
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
		/* CEA VICs other than #1 requre limited range RGB
		 * output unless overridden by an AVI infoframe.
		 * Apply a colorspace conversion to squash 0-255 down
		 * to 16-235.  The matrix here is:
		 *
		 * [ 0      0      0.8594 16]
		 * [ 0      0.8594 0      16]
		 * [ 0.8594 0      0      16]
		 * [ 0      0      0       1]
		 */
		csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
		csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
		csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
					 VC4_HD_CSC_CTL_MODE);

		HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
		HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
		HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
		HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
		HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
		HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
	}

	/* The RGB order applies even when CSC is disabled. */
	HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
931 932

	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
933 934

	drm_dev_exit(idx);
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
/*
 * If we need to output Full Range RGB, then use the unity matrix
 *
 * [ 1      0      0      0]
 * [ 0      1      0      0]
 * [ 0      0      1      0]
 *
 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
 */
static const u16 vc5_hdmi_csc_full_rgb_unity[3][4] = {
	{ 0x2000, 0x0000, 0x0000, 0x0000 },
	{ 0x0000, 0x2000, 0x0000, 0x0000 },
	{ 0x0000, 0x0000, 0x2000, 0x0000 },
};

/*
 * CEA VICs other than #1 require limited range RGB output unless
 * overridden by an AVI infoframe. Apply a colorspace conversion to
 * squash 0-255 down to 16-235. The matrix here is:
 *
 * [ 0.8594 0      0      16]
 * [ 0      0.8594 0      16]
 * [ 0      0      0.8594 16]
 *
 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
 */
static const u16 vc5_hdmi_csc_full_rgb_to_limited_rgb[3][4] = {
	{ 0x1b80, 0x0000, 0x0000, 0x0400 },
	{ 0x0000, 0x1b80, 0x0000, 0x0400 },
	{ 0x0000, 0x0000, 0x1b80, 0x0400 },
};

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
/*
 * Conversion between Full Range RGB and Full Range YUV422 using the
 * BT.709 Colorspace
 *
 *
 * [  0.181906  0.611804  0.061758  16  ]
 * [ -0.100268 -0.337232  0.437500  128 ]
 * [  0.437500 -0.397386 -0.040114  128 ]
 *
 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
 */
static const u16 vc5_hdmi_csc_full_rgb_to_limited_yuv422_bt709[3][4] = {
	{ 0x05d2, 0x1394, 0x01fa, 0x0400 },
	{ 0xfccc, 0xf536, 0x0e00, 0x2000 },
	{ 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
};

/*
 * Conversion between Full Range RGB and Full Range YUV444 using the
 * BT.709 Colorspace
 *
 * [ -0.100268 -0.337232  0.437500  128 ]
 * [  0.437500 -0.397386 -0.040114  128 ]
 * [  0.181906  0.611804  0.061758  16  ]
 *
 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
 */
static const u16 vc5_hdmi_csc_full_rgb_to_limited_yuv444_bt709[3][4] = {
	{ 0xfccc, 0xf536, 0x0e00, 0x2000 },
	{ 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
	{ 0x05d2, 0x1394, 0x01fa, 0x0400 },
};

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
				    const u16 coeffs[3][4])
{
	lockdep_assert_held(&vc4_hdmi->hw_lock);

	HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
	HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
	HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
	HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
	HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
	HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
}

1015
static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1016
			       struct drm_connector_state *state,
1017
			       const struct drm_display_mode *mode)
1018
{
1019
	struct drm_device *drm = vc4_hdmi->connector.dev;
1020 1021
	struct vc4_hdmi_connector_state *vc4_state =
		conn_state_to_vc4_hdmi_conn_state(state);
1022
	unsigned long flags;
1023 1024 1025
	u32 if_cfg = 0;
	u32 if_xbar = 0x543210;
	u32 csc_chan_ctl = 0;
1026 1027
	u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
							       VC5_MT_CP_CSC_CTL_MODE);
1028 1029 1030 1031
	int idx;

	if (!drm_dev_enter(drm, &idx))
		return;
1032

1033 1034
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

1035 1036 1037 1038
	switch (vc4_state->output_format) {
	case VC4_HDMI_OUTPUT_YUV444:
		vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_yuv444_bt709);
		break;
1039

1040 1041 1042 1043 1044
	case VC4_HDMI_OUTPUT_YUV422:
		csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
					 VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
			VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
			VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
1045

1046 1047 1048 1049 1050 1051 1052 1053
		csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
					      VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);

		if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
					VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);

		vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_yuv422_bt709);
		break;
1054

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	case VC4_HDMI_OUTPUT_RGB:
		if_xbar = 0x354021;

		if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode))
			vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_rgb);
		else
			vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_unity);
		break;

	default:
		break;
	}

	HDMI_WRITE(HDMI_VEC_INTERFACE_CFG, if_cfg);
	HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, if_xbar);
	HDMI_WRITE(HDMI_CSC_CHANNEL_CTL, csc_chan_ctl);
1071
	HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1072 1073

	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1074 1075

	drm_dev_exit(idx);
1076 1077
}

1078
static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1079
				 struct drm_connector_state *state,
1080
				 struct drm_display_mode *mode)
1081
{
1082
	struct drm_device *drm = vc4_hdmi->connector.dev;
1083 1084
	bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
	bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1085
	bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1086
	u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1087
	u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1088
				   VC4_HDMI_VERTA_VSP) |
1089
		     VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1090
				   VC4_HDMI_VERTA_VFP) |
1091
		     VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
1092
	u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1093 1094
		     VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
				   interlaced,
1095
				   VC4_HDMI_VERTB_VBP));
1096 1097
	u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
			  VC4_SET_FIELD(mode->crtc_vtotal -
1098
					mode->crtc_vsync_end,
1099
					VC4_HDMI_VERTB_VBP));
1100
	unsigned long flags;
1101
	u32 reg;
1102 1103 1104 1105
	int idx;

	if (!drm_dev_enter(drm, &idx))
		return;
1106 1107

	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130

	HDMI_WRITE(HDMI_HORZA,
		   (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
		   (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
		   VC4_SET_FIELD(mode->hdisplay * pixel_rep,
				 VC4_HDMI_HORZA_HAP));

	HDMI_WRITE(HDMI_HORZB,
		   VC4_SET_FIELD((mode->htotal -
				  mode->hsync_end) * pixel_rep,
				 VC4_HDMI_HORZB_HBP) |
		   VC4_SET_FIELD((mode->hsync_end -
				  mode->hsync_start) * pixel_rep,
				 VC4_HDMI_HORZB_HSP) |
		   VC4_SET_FIELD((mode->hsync_start -
				  mode->hdisplay) * pixel_rep,
				 VC4_HDMI_HORZB_HFP));

	HDMI_WRITE(HDMI_VERTA0, verta);
	HDMI_WRITE(HDMI_VERTA1, verta);

	HDMI_WRITE(HDMI_VERTB0, vertb_even);
	HDMI_WRITE(HDMI_VERTB1, vertb);
1131

1132 1133 1134 1135 1136
	reg = HDMI_READ(HDMI_MISC_CONTROL);
	reg &= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
	reg |= VC4_SET_FIELD(pixel_rep - 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP);
	HDMI_WRITE(HDMI_MISC_CONTROL, reg);

1137
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1138 1139

	drm_dev_exit(idx);
1140
}
1141

1142
static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1143
				 struct drm_connector_state *state,
1144 1145
				 struct drm_display_mode *mode)
{
1146
	struct drm_device *drm = vc4_hdmi->connector.dev;
1147 1148
	const struct vc4_hdmi_connector_state *vc4_state =
		conn_state_to_vc4_hdmi_conn_state(state);
1149 1150 1151 1152 1153 1154 1155 1156 1157
	bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
	bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
	bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
	u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
	u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
				   VC5_HDMI_VERTA_VSP) |
		     VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
				   VC5_HDMI_VERTA_VFP) |
		     VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
1158 1159 1160
	u32 vertb = (VC4_SET_FIELD(mode->htotal >> (2 - pixel_rep),
				   VC5_HDMI_VERTB_VSPO) |
		     VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
1161 1162 1163
				   VC4_HDMI_VERTB_VBP));
	u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
			  VC4_SET_FIELD(mode->crtc_vtotal -
1164
					mode->crtc_vsync_end - interlaced,
1165
					VC4_HDMI_VERTB_VBP));
1166
	unsigned long flags;
1167 1168 1169
	unsigned char gcp;
	bool gcp_en;
	u32 reg;
1170 1171 1172 1173
	int idx;

	if (!drm_dev_enter(drm, &idx))
		return;
1174

1175 1176
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	HDMI_WRITE(HDMI_HORZA,
		   (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
		   (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
		   VC4_SET_FIELD(mode->hdisplay * pixel_rep,
				 VC5_HDMI_HORZA_HAP) |
		   VC4_SET_FIELD((mode->hsync_start -
				  mode->hdisplay) * pixel_rep,
				 VC5_HDMI_HORZA_HFP));

	HDMI_WRITE(HDMI_HORZB,
		   VC4_SET_FIELD((mode->htotal -
				  mode->hsync_end) * pixel_rep,
				 VC5_HDMI_HORZB_HBP) |
		   VC4_SET_FIELD((mode->hsync_end -
				  mode->hsync_start) * pixel_rep,
				 VC5_HDMI_HORZB_HSP));

	HDMI_WRITE(HDMI_VERTA0, verta);
	HDMI_WRITE(HDMI_VERTA1, verta);

	HDMI_WRITE(HDMI_VERTB0, vertb_even);
	HDMI_WRITE(HDMI_VERTB1, vertb);

1200
	switch (vc4_state->output_bpc) {
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
	case 12:
		gcp = 6;
		gcp_en = true;
		break;
	case 10:
		gcp = 5;
		gcp_en = true;
		break;
	case 8:
	default:
		gcp = 4;
		gcp_en = false;
		break;
	}

1216 1217 1218 1219 1220 1221 1222 1223 1224
	/*
	 * YCC422 is always 36-bit and not considered deep colour so
	 * doesn't signal in GCP.
	 */
	if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
		gcp = 4;
		gcp_en = false;
	}

1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
	reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
		 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
	reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
	       VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
	HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);

	reg = HDMI_READ(HDMI_GCP_WORD_1);
	reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
	reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
	HDMI_WRITE(HDMI_GCP_WORD_1, reg);

	reg = HDMI_READ(HDMI_GCP_CONFIG);
	reg &= ~VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
	reg |= gcp_en ? VC5_HDMI_GCP_CONFIG_GCP_ENABLE : 0;
	HDMI_WRITE(HDMI_GCP_CONFIG, reg);

1242 1243
	reg = HDMI_READ(HDMI_MISC_CONTROL);
	reg &= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1244
	reg |= VC4_SET_FIELD(pixel_rep - 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP);
1245 1246
	HDMI_WRITE(HDMI_MISC_CONTROL, reg);

1247
	HDMI_WRITE(HDMI_CLOCK_STOP, 0);
1248 1249

	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1250 1251

	drm_dev_exit(idx);
1252
}
1253

1254 1255
static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
{
1256
	struct drm_device *drm = vc4_hdmi->connector.dev;
1257
	unsigned long flags;
1258 1259
	u32 drift;
	int ret;
1260 1261 1262 1263
	int idx;

	if (!drm_dev_enter(drm, &idx))
		return;
1264

1265 1266
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

1267 1268 1269 1270 1271 1272 1273
	drift = HDMI_READ(HDMI_FIFO_CTL);
	drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;

	HDMI_WRITE(HDMI_FIFO_CTL,
		   drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
	HDMI_WRITE(HDMI_FIFO_CTL,
		   drift | VC4_HDMI_FIFO_CTL_RECENTER);
1274 1275 1276

	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

1277
	usleep_range(1000, 1100);
1278 1279 1280

	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

1281 1282 1283 1284 1285
	HDMI_WRITE(HDMI_FIFO_CTL,
		   drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
	HDMI_WRITE(HDMI_FIFO_CTL,
		   drift | VC4_HDMI_FIFO_CTL_RECENTER);

1286 1287
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

1288 1289 1290 1291
	ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
		       VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
	WARN_ONCE(ret, "Timeout waiting for "
		  "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
1292 1293

	drm_dev_exit(idx);
1294 1295
}

1296 1297
static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
						struct drm_atomic_state *state)
1298
{
1299
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1300
	struct drm_device *drm = vc4_hdmi->connector.dev;
1301
	struct drm_connector *connector = &vc4_hdmi->connector;
1302
	struct drm_connector_state *conn_state =
1303
		drm_atomic_get_new_connector_state(state, connector);
1304 1305
	struct vc4_hdmi_connector_state *vc4_conn_state =
		conn_state_to_vc4_hdmi_conn_state(conn_state);
1306
	struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1307
	unsigned long tmds_char_rate = vc4_conn_state->tmds_char_rate;
1308
	unsigned long bvb_rate, hsm_rate;
1309
	unsigned long flags;
1310
	int ret;
1311
	int idx;
1312

1313 1314
	mutex_lock(&vc4_hdmi->mutex);

1315 1316 1317
	if (!drm_dev_enter(drm, &idx))
		goto out;

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
	/*
	 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
	 * be faster than pixel clock, infinitesimally faster, tested in
	 * simulation. Otherwise, exact value is unimportant for HDMI
	 * operation." This conflicts with bcm2835's vc4 documentation, which
	 * states HSM's clock has to be at least 108% of the pixel clock.
	 *
	 * Real life tests reveal that vc4's firmware statement holds up, and
	 * users are able to use pixel clocks closer to HSM's, namely for
	 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
	 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
	 * 162MHz.
	 *
	 * Additionally, the AXI clock needs to be at least 25% of
	 * pixel clock, but HSM ends up being the limiting factor.
1333
	 */
1334
	hsm_rate = max_t(unsigned long, 120000000, (tmds_char_rate / 100) * 101);
1335
	ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
1336 1337
	if (ret) {
		DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1338
		goto err_dev_exit;
1339
	}
1340

1341 1342 1343
	ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
	if (ret < 0) {
		DRM_ERROR("Failed to retain power domain: %d\n", ret);
1344
		goto err_dev_exit;
1345 1346
	}

1347
	ret = clk_set_rate(vc4_hdmi->pixel_clock, tmds_char_rate);
1348
	if (ret) {
1349
		DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
1350
		goto err_put_runtime_pm;
1351 1352 1353 1354 1355
	}

	ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
	if (ret) {
		DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
1356
		goto err_put_runtime_pm;
1357 1358
	}

1359

1360 1361
	vc4_hdmi_cec_update_clk_div(vc4_hdmi);

1362
	if (tmds_char_rate > 297000000)
1363
		bvb_rate = 300000000;
1364
	else if (tmds_char_rate > 148500000)
1365 1366 1367 1368 1369
		bvb_rate = 150000000;
	else
		bvb_rate = 75000000;

	ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
1370 1371
	if (ret) {
		DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
1372
		goto err_disable_pixel_clock;
1373
	}
1374

1375 1376 1377
	ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
	if (ret) {
		DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
1378
		goto err_disable_pixel_clock;
1379 1380
	}

1381
	if (vc4_hdmi->variant->phy_init)
1382
		vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
1383

1384 1385
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

1386 1387
	HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
		   HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1388 1389 1390
		   VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
		   VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);

1391 1392
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

1393
	if (vc4_hdmi->variant->set_timings)
1394
		vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
1395

1396 1397
	drm_dev_exit(idx);

1398 1399
	mutex_unlock(&vc4_hdmi->mutex);

1400 1401 1402 1403 1404 1405
	return;

err_disable_pixel_clock:
	clk_disable_unprepare(vc4_hdmi->pixel_clock);
err_put_runtime_pm:
	pm_runtime_put(&vc4_hdmi->pdev->dev);
1406 1407
err_dev_exit:
	drm_dev_exit(idx);
1408 1409
out:
	mutex_unlock(&vc4_hdmi->mutex);
1410
	return;
1411
}
1412

1413 1414
static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
					     struct drm_atomic_state *state)
1415 1416
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1417
	struct drm_device *drm = vc4_hdmi->connector.dev;
1418
	struct drm_connector *connector = &vc4_hdmi->connector;
1419
	struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1420 1421
	struct drm_connector_state *conn_state =
		drm_atomic_get_new_connector_state(state, connector);
1422
	unsigned long flags;
1423
	int idx;
1424

1425 1426
	mutex_lock(&vc4_hdmi->mutex);

1427 1428 1429
	if (!drm_dev_enter(drm, &idx))
		return;

1430
	if (vc4_hdmi->variant->csc_setup)
1431
		vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
1432

1433
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1434
	HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
1435
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1436

1437 1438
	drm_dev_exit(idx);

1439
	mutex_unlock(&vc4_hdmi->mutex);
1440
}
1441

1442 1443
static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
					      struct drm_atomic_state *state)
1444 1445
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1446
	struct drm_device *drm = vc4_hdmi->connector.dev;
1447
	struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1448
	struct drm_display_info *display = &vc4_hdmi->connector.display_info;
1449 1450
	bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
	bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1451
	unsigned long flags;
1452
	int ret;
1453
	int idx;
1454

1455 1456
	mutex_lock(&vc4_hdmi->mutex);

1457 1458 1459
	if (!drm_dev_enter(drm, &idx))
		return;

1460 1461
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

1462 1463
	HDMI_WRITE(HDMI_VID_CTL,
		   VC4_HD_VID_CTL_ENABLE |
1464
		   VC4_HD_VID_CTL_CLRRGB |
1465
		   VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
1466 1467 1468
		   VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
		   (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
		   (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
1469

1470 1471
	HDMI_WRITE(HDMI_VID_CTL,
		   HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1472

1473
	if (display->is_hdmi) {
1474 1475
		HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1476 1477
			   VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);

1478 1479
		spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

1480
		ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1481
			       VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1482 1483 1484
		WARN_ONCE(ret, "Timeout waiting for "
			  "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
	} else {
1485 1486
		HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
			   HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
1487
			   ~(VC4_HDMI_RAM_PACKET_ENABLE));
1488 1489
		HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1490 1491
			   ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);

1492 1493
		spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

1494
		ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1495
				 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1496 1497 1498 1499
		WARN_ONCE(ret, "Timeout waiting for "
			  "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
	}

1500
	if (display->is_hdmi) {
1501 1502
		spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

1503
		WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1504 1505
			  VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));

1506
		HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1507 1508
			   VC4_HDMI_RAM_PACKET_ENABLE);

1509
		spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1510
		vc4_hdmi->packet_ram_enabled = true;
1511

1512
		vc4_hdmi_set_infoframes(encoder);
1513
	}
1514 1515

	vc4_hdmi_recenter_fifo(vc4_hdmi);
1516
	vc4_hdmi_enable_scrambling(encoder);
1517

1518
	drm_dev_exit(idx);
1519
	mutex_unlock(&vc4_hdmi->mutex);
1520 1521
}

1522 1523 1524 1525 1526
static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
					     struct drm_crtc_state *crtc_state,
					     struct drm_connector_state *conn_state)
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1527 1528
	struct vc4_hdmi_connector_state *vc4_state =
		conn_state_to_vc4_hdmi_conn_state(conn_state);
1529 1530

	mutex_lock(&vc4_hdmi->mutex);
V
Ville Syrjälä 已提交
1531 1532
	drm_mode_copy(&vc4_hdmi->saved_adjusted_mode,
		      &crtc_state->adjusted_mode);
1533
	vc4_hdmi->output_bpc = vc4_state->output_bpc;
1534
	vc4_hdmi->output_format = vc4_state->output_format;
1535 1536 1537
	mutex_unlock(&vc4_hdmi->mutex);
}

1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
static bool
vc4_hdmi_sink_supports_format_bpc(const struct vc4_hdmi *vc4_hdmi,
				  const struct drm_display_info *info,
				  const struct drm_display_mode *mode,
				  unsigned int format, unsigned int bpc)
{
	struct drm_device *dev = vc4_hdmi->connector.dev;
	u8 vic = drm_match_cea_mode(mode);

	if (vic == 1 && bpc != 8) {
		drm_dbg(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
		return false;
	}

	if (!info->is_hdmi &&
	    (format != VC4_HDMI_OUTPUT_RGB || bpc != 8)) {
		drm_dbg(dev, "DVI Monitors require an RGB output at 8 bpc\n");
		return false;
	}

	switch (format) {
	case VC4_HDMI_OUTPUT_RGB:
		drm_dbg(dev, "RGB Format, checking the constraints.\n");

		if (!(info->color_formats & DRM_COLOR_FORMAT_RGB444))
			return false;

		if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
			drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
			return false;
		}

		if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
			drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
			return false;
		}

		drm_dbg(dev, "RGB format supported in that configuration.\n");

		return true;

	case VC4_HDMI_OUTPUT_YUV422:
		drm_dbg(dev, "YUV422 format, checking the constraints.\n");

		if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR422)) {
			drm_dbg(dev, "Sink doesn't support YUV422.\n");
			return false;
		}

		if (bpc != 12) {
			drm_dbg(dev, "YUV422 only supports 12 bpc.\n");
			return false;
		}

		drm_dbg(dev, "YUV422 format supported in that configuration.\n");

		return true;

	case VC4_HDMI_OUTPUT_YUV444:
		drm_dbg(dev, "YUV444 format, checking the constraints.\n");

		if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR444)) {
			drm_dbg(dev, "Sink doesn't support YUV444.\n");
			return false;
		}

		if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) {
			drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
			return false;
		}

		if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) {
			drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
			return false;
		}

		drm_dbg(dev, "YUV444 format supported in that configuration.\n");

		return true;
	}

	return false;
}

1622 1623 1624 1625
static enum drm_mode_status
vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi *vc4_hdmi,
			     unsigned long long clock)
{
1626 1627 1628
	const struct drm_connector *connector = &vc4_hdmi->connector;
	const struct drm_display_info *info = &connector->display_info;

1629 1630 1631 1632 1633 1634
	if (clock > vc4_hdmi->variant->max_pixel_clock)
		return MODE_CLOCK_HIGH;

	if (vc4_hdmi->disable_4kp60 && clock > HDMI_14_MAX_TMDS_CLK)
		return MODE_CLOCK_HIGH;

1635 1636 1637
	if (info->max_tmds_clock && clock > (info->max_tmds_clock * 1000))
		return MODE_CLOCK_HIGH;

1638 1639 1640
	return MODE_OK;
}

1641 1642
static unsigned long long
vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
1643 1644
				    unsigned int bpc,
				    enum vc4_hdmi_output_format fmt)
1645
{
1646
	unsigned long long clock = mode->clock * 1000ULL;
1647 1648 1649 1650

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

1651 1652 1653
	if (fmt == VC4_HDMI_OUTPUT_YUV422)
		bpc = 8;

1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
	clock = clock * bpc;
	do_div(clock, 8);

	return clock;
}

static int
vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi *vc4_hdmi,
			       struct vc4_hdmi_connector_state *vc4_state,
			       const struct drm_display_mode *mode,
1664
			       unsigned int bpc, unsigned int fmt)
1665 1666 1667
{
	unsigned long long clock;

1668
	clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
1669 1670 1671 1672 1673 1674 1675 1676
	if (vc4_hdmi_encoder_clock_valid(vc4_hdmi, clock) != MODE_OK)
		return -EINVAL;

	vc4_state->tmds_char_rate = clock;

	return 0;
}

1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
static int
vc4_hdmi_encoder_compute_format(const struct vc4_hdmi *vc4_hdmi,
				struct vc4_hdmi_connector_state *vc4_state,
				const struct drm_display_mode *mode,
				unsigned int bpc)
{
	struct drm_device *dev = vc4_hdmi->connector.dev;
	const struct drm_connector *connector = &vc4_hdmi->connector;
	const struct drm_display_info *info = &connector->display_info;
	unsigned int format;

	drm_dbg(dev, "Trying with an RGB output\n");

	format = VC4_HDMI_OUTPUT_RGB;
	if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
		int ret;

		ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
						     mode, bpc, format);
		if (!ret) {
			vc4_state->output_format = format;
			return 0;
		}
	}

	drm_dbg(dev, "Failed, Trying with an YUV422 output\n");

	format = VC4_HDMI_OUTPUT_YUV422;
	if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
		int ret;

		ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
						     mode, bpc, format);
		if (!ret) {
			vc4_state->output_format = format;
			return 0;
		}
	}

	drm_dbg(dev, "Failed. No Format Supported for that bpc count.\n");

	return -EINVAL;
}

1721 1722 1723 1724 1725
static int
vc4_hdmi_encoder_compute_config(const struct vc4_hdmi *vc4_hdmi,
				struct vc4_hdmi_connector_state *vc4_state,
				const struct drm_display_mode *mode)
{
1726
	struct drm_device *dev = vc4_hdmi->connector.dev;
1727 1728 1729 1730 1731 1732 1733 1734
	struct drm_connector_state *conn_state = &vc4_state->base;
	unsigned int max_bpc = clamp_t(unsigned int, conn_state->max_bpc, 8, 12);
	unsigned int bpc;
	int ret;

	for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
		drm_dbg(dev, "Trying with a %d bpc output\n", bpc);

1735 1736
		ret = vc4_hdmi_encoder_compute_format(vc4_hdmi, vc4_state,
						      mode, bpc);
1737 1738 1739 1740 1741 1742
		if (ret)
			continue;

		vc4_state->output_bpc = bpc;

		drm_dbg(dev,
1743
			"Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
1744 1745
			mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
			vc4_state->output_bpc,
1746
			vc4_hdmi_output_fmt_str(vc4_state->output_format),
1747 1748 1749 1750 1751 1752 1753 1754
			vc4_state->tmds_char_rate);

		break;
	}

	return ret;
}

1755 1756 1757
#define WIFI_2_4GHz_CH1_MIN_FREQ	2400000000ULL
#define WIFI_2_4GHz_CH1_MAX_FREQ	2422000000ULL

1758 1759 1760 1761
static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
					 struct drm_crtc_state *crtc_state,
					 struct drm_connector_state *conn_state)
{
1762 1763 1764 1765 1766 1767
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
	struct drm_connector *connector = &vc4_hdmi->connector;
	struct drm_connector_state *old_conn_state =
		drm_atomic_get_old_connector_state(conn_state->state, connector);
	struct vc4_hdmi_connector_state *old_vc4_state =
		conn_state_to_vc4_hdmi_conn_state(old_conn_state);
1768
	struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
1769
	struct drm_display_mode *mode = &crtc_state->adjusted_mode;
1770 1771
	unsigned long long tmds_char_rate = mode->clock * 1000;
	unsigned long long tmds_bit_rate;
1772
	int ret;
1773

1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
	if (vc4_hdmi->variant->unsupported_odd_h_timings) {
		if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
			/* Only try to fixup DBLCLK modes to get 480i and 576i
			 * working.
			 * A generic solution for all modes with odd horizontal
			 * timing values seems impossible based on trying to
			 * solve it for 1366x768 monitors.
			 */
			if ((mode->hsync_start - mode->hdisplay) & 1)
				mode->hsync_start--;
			if ((mode->hsync_end - mode->hsync_start) & 1)
				mode->hsync_end--;
		}

		/* Now check whether we still have odd values remaining */
		if ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
		    (mode->hsync_end % 2) || (mode->htotal % 2))
			return -EINVAL;
	}
1793

1794 1795 1796 1797 1798 1799
	/*
	 * The 1440p@60 pixel rate is in the same range than the first
	 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
	 * bandwidth). Slightly lower the frequency to bring it out of
	 * the WiFi range.
	 */
1800
	tmds_bit_rate = tmds_char_rate * 10;
1801
	if (vc4_hdmi->disable_wifi_frequencies &&
1802 1803
	    (tmds_bit_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
	     tmds_bit_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
1804
		mode->clock = 238560;
1805
		tmds_char_rate = mode->clock * 1000;
1806 1807
	}

1808
	ret = vc4_hdmi_encoder_compute_config(vc4_hdmi, vc4_state, mode);
1809 1810
	if (ret)
		return ret;
1811

1812 1813 1814 1815 1816
	/* vc4_hdmi_encoder_compute_config may have changed output_bpc and/or output_format */
	if (vc4_state->output_bpc != old_vc4_state->output_bpc ||
	    vc4_state->output_format != old_vc4_state->output_format)
		crtc_state->mode_changed = true;

1817 1818 1819
	return 0;
}

1820
static enum drm_mode_status
1821
vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
1822 1823
			    const struct drm_display_mode *mode)
{
1824 1825
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);

1826
	if (vc4_hdmi->variant->unsupported_odd_h_timings &&
1827
	    !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
1828 1829 1830 1831
	    ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
	     (mode->hsync_end % 2) || (mode->htotal % 2)))
		return MODE_H_ILLEGAL;

1832
	return vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode->clock * 1000);
1833 1834
}

1835
static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
1836
	.atomic_check = vc4_hdmi_encoder_atomic_check,
1837
	.atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
1838
	.mode_valid = vc4_hdmi_encoder_mode_valid,
1839 1840
};

1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
static int vc4_hdmi_late_register(struct drm_encoder *encoder)
{
	struct drm_device *drm = encoder->dev;
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
	const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
	int ret;

	ret = vc4_debugfs_add_file(drm->primary, variant->debugfs_name,
				   vc4_hdmi_debugfs_regs,
				   vc4_hdmi);
	if (ret)
		return ret;

	return 0;
}

static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
	.late_register = vc4_hdmi_late_register,
};

1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
{
	int i;
	u32 channel_map = 0;

	for (i = 0; i < 8; i++) {
		if (channel_mask & BIT(i))
			channel_map |= i << (3 * i);
	}
	return channel_map;
}

1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
{
	int i;
	u32 channel_map = 0;

	for (i = 0; i < 8; i++) {
		if (channel_mask & BIT(i))
			channel_map |= i << (4 * i);
	}
	return channel_map;
}

1885 1886
static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi)
{
1887
	struct drm_device *drm = vc4_hdmi->connector.dev;
1888 1889
	unsigned long flags;
	u32 hotplug;
1890 1891 1892 1893
	int idx;

	if (!drm_dev_enter(drm, &idx))
		return false;
1894 1895 1896 1897 1898

	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
	hotplug = HDMI_READ(HDMI_HOTPLUG);
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

1899 1900
	drm_dev_exit(idx);

1901 1902 1903
	return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED);
}

E
Eric Anholt 已提交
1904
/* HDMI audio codec callbacks */
1905 1906
static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
					 unsigned int samplerate)
E
Eric Anholt 已提交
1907
{
1908 1909
	struct drm_device *drm = vc4_hdmi->connector.dev;
	u32 hsm_clock;
1910
	unsigned long flags;
E
Eric Anholt 已提交
1911
	unsigned long n, m;
1912 1913 1914 1915
	int idx;

	if (!drm_dev_enter(drm, &idx))
		return;
E
Eric Anholt 已提交
1916

1917
	hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
1918
	rational_best_approximation(hsm_clock, samplerate,
E
Eric Anholt 已提交
1919 1920 1921 1922 1923 1924
				    VC4_HD_MAI_SMP_N_MASK >>
				    VC4_HD_MAI_SMP_N_SHIFT,
				    (VC4_HD_MAI_SMP_M_MASK >>
				     VC4_HD_MAI_SMP_M_SHIFT) + 1,
				    &n, &m);

1925
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1926 1927 1928
	HDMI_WRITE(HDMI_MAI_SMP,
		   VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
		   VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
1929
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1930 1931

	drm_dev_exit(idx);
E
Eric Anholt 已提交
1932 1933
}

1934
static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
E
Eric Anholt 已提交
1935
{
1936
	const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
E
Eric Anholt 已提交
1937 1938 1939
	u32 n, cts;
	u64 tmp;

1940
	lockdep_assert_held(&vc4_hdmi->mutex);
1941 1942
	lockdep_assert_held(&vc4_hdmi->hw_lock);

E
Eric Anholt 已提交
1943 1944 1945 1946 1947
	n = 128 * samplerate / 1000;
	tmp = (u64)(mode->clock * 1000) * n;
	do_div(tmp, 128 * samplerate);
	cts = tmp;

1948
	HDMI_WRITE(HDMI_CRP_CFG,
E
Eric Anholt 已提交
1949 1950 1951 1952 1953 1954 1955 1956
		   VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
		   VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));

	/*
	 * We could get slightly more accurate clocks in some cases by
	 * providing a CTS_1 value.  The two CTS values are alternated
	 * between based on the period fields
	 */
1957 1958
	HDMI_WRITE(HDMI_CTS_0, cts);
	HDMI_WRITE(HDMI_CTS_1, cts);
E
Eric Anholt 已提交
1959 1960 1961 1962 1963 1964 1965 1966 1967
}

static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
{
	struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);

	return snd_soc_card_get_drvdata(card);
}

1968 1969
static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
{
1970
	struct drm_display_info *display = &vc4_hdmi->connector.display_info;
1971

1972
	lockdep_assert_held(&vc4_hdmi->mutex);
1973 1974 1975 1976 1977

	/*
	 * If the encoder is currently in DVI mode, treat the codec DAI
	 * as missing.
	 */
1978
	if (!display->is_hdmi)
1979 1980 1981 1982 1983
		return false;

	return true;
}

1984
static int vc4_hdmi_audio_startup(struct device *dev, void *data)
E
Eric Anholt 已提交
1985
{
1986
	struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1987
	struct drm_device *drm = vc4_hdmi->connector.dev;
1988
	unsigned long flags;
1989 1990
	int ret = 0;
	int idx;
E
Eric Anholt 已提交
1991

1992 1993
	mutex_lock(&vc4_hdmi->mutex);

1994 1995 1996 1997 1998
	if (!drm_dev_enter(drm, &idx)) {
		ret = -ENODEV;
		goto out;
	}

1999
	if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2000 2001
		ret = -ENODEV;
		goto out_dev_exit;
2002
	}
E
Eric Anholt 已提交
2003

2004
	vc4_hdmi->audio.streaming = true;
E
Eric Anholt 已提交
2005

2006
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2007 2008 2009 2010 2011 2012
	HDMI_WRITE(HDMI_MAI_CTL,
		   VC4_HD_MAI_CTL_RESET |
		   VC4_HD_MAI_CTL_FLUSH |
		   VC4_HD_MAI_CTL_DLATE |
		   VC4_HD_MAI_CTL_ERRORE |
		   VC4_HD_MAI_CTL_ERRORF);
2013
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2014 2015 2016

	if (vc4_hdmi->variant->phy_rng_enable)
		vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
E
Eric Anholt 已提交
2017

2018 2019 2020
out_dev_exit:
	drm_dev_exit(idx);
out:
2021 2022
	mutex_unlock(&vc4_hdmi->mutex);

2023
	return ret;
E
Eric Anholt 已提交
2024 2025
}

2026
static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
2027
{
2028
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2029
	struct device *dev = &vc4_hdmi->pdev->dev;
2030
	unsigned long flags;
E
Eric Anholt 已提交
2031 2032
	int ret;

2033 2034
	lockdep_assert_held(&vc4_hdmi->mutex);

2035
	vc4_hdmi->audio.streaming = false;
2036
	ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
E
Eric Anholt 已提交
2037 2038 2039
	if (ret)
		dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);

2040 2041
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

2042 2043 2044
	HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
	HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
	HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
2045 2046

	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
E
Eric Anholt 已提交
2047 2048
}

2049
static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
E
Eric Anholt 已提交
2050
{
2051
	struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2052
	struct drm_device *drm = vc4_hdmi->connector.dev;
2053
	unsigned long flags;
2054
	int idx;
2055

2056 2057
	mutex_lock(&vc4_hdmi->mutex);

2058 2059 2060
	if (!drm_dev_enter(drm, &idx))
		goto out;

2061
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
E
Eric Anholt 已提交
2062

2063 2064 2065 2066
	HDMI_WRITE(HDMI_MAI_CTL,
		   VC4_HD_MAI_CTL_DLATE |
		   VC4_HD_MAI_CTL_ERRORE |
		   VC4_HD_MAI_CTL_ERRORF);
E
Eric Anholt 已提交
2067

2068 2069
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

2070 2071
	if (vc4_hdmi->variant->phy_rng_disable)
		vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
E
Eric Anholt 已提交
2072

2073
	vc4_hdmi->audio.streaming = false;
2074
	vc4_hdmi_audio_reset(vc4_hdmi);
2075

2076 2077 2078
	drm_dev_exit(idx);

out:
2079
	mutex_unlock(&vc4_hdmi->mutex);
E
Eric Anholt 已提交
2080 2081
}

D
Dom Cobley 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
static int sample_rate_to_mai_fmt(int samplerate)
{
	switch (samplerate) {
	case 8000:
		return VC4_HDMI_MAI_SAMPLE_RATE_8000;
	case 11025:
		return VC4_HDMI_MAI_SAMPLE_RATE_11025;
	case 12000:
		return VC4_HDMI_MAI_SAMPLE_RATE_12000;
	case 16000:
		return VC4_HDMI_MAI_SAMPLE_RATE_16000;
	case 22050:
		return VC4_HDMI_MAI_SAMPLE_RATE_22050;
	case 24000:
		return VC4_HDMI_MAI_SAMPLE_RATE_24000;
	case 32000:
		return VC4_HDMI_MAI_SAMPLE_RATE_32000;
	case 44100:
		return VC4_HDMI_MAI_SAMPLE_RATE_44100;
	case 48000:
		return VC4_HDMI_MAI_SAMPLE_RATE_48000;
	case 64000:
		return VC4_HDMI_MAI_SAMPLE_RATE_64000;
	case 88200:
		return VC4_HDMI_MAI_SAMPLE_RATE_88200;
	case 96000:
		return VC4_HDMI_MAI_SAMPLE_RATE_96000;
	case 128000:
		return VC4_HDMI_MAI_SAMPLE_RATE_128000;
	case 176400:
		return VC4_HDMI_MAI_SAMPLE_RATE_176400;
	case 192000:
		return VC4_HDMI_MAI_SAMPLE_RATE_192000;
	default:
		return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
	}
E
Eric Anholt 已提交
2118 2119 2120
}

/* HDMI audio codec callbacks */
2121 2122 2123
static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
				  struct hdmi_codec_daifmt *daifmt,
				  struct hdmi_codec_params *params)
E
Eric Anholt 已提交
2124
{
2125
	struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2126
	struct drm_device *drm = vc4_hdmi->connector.dev;
2127
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2128 2129
	unsigned int sample_rate = params->sample_rate;
	unsigned int channels = params->channels;
2130
	unsigned long flags;
E
Eric Anholt 已提交
2131
	u32 audio_packet_config, channel_mask;
2132
	u32 channel_map;
D
Dom Cobley 已提交
2133 2134
	u32 mai_audio_format;
	u32 mai_sample_rate;
2135 2136
	int ret = 0;
	int idx;
E
Eric Anholt 已提交
2137 2138

	dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
2139
		sample_rate, params->sample_width, channels);
E
Eric Anholt 已提交
2140

2141 2142
	mutex_lock(&vc4_hdmi->mutex);

2143 2144 2145 2146 2147
	if (!drm_dev_enter(drm, &idx)) {
		ret = -ENODEV;
		goto out;
	}

2148
	if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2149 2150
		ret = -EINVAL;
		goto out_dev_exit;
2151 2152
	}

2153 2154 2155
	vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);

	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2156
	HDMI_WRITE(HDMI_MAI_CTL,
2157
		   VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
2158 2159 2160
		   VC4_HD_MAI_CTL_WHOLSMP |
		   VC4_HD_MAI_CTL_CHALIGN |
		   VC4_HD_MAI_CTL_ENABLE);
E
Eric Anholt 已提交
2161

2162
	mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
2163 2164 2165 2166 2167
	if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
	    params->channels == 8)
		mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
	else
		mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
D
Dom Cobley 已提交
2168 2169 2170 2171 2172 2173
	HDMI_WRITE(HDMI_MAI_FMT,
		   VC4_SET_FIELD(mai_sample_rate,
				 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
		   VC4_SET_FIELD(mai_audio_format,
				 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));

2174
	/* The B frame identifier should match the value used by alsa-lib (8) */
E
Eric Anholt 已提交
2175 2176 2177
	audio_packet_config =
		VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
		VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
2178
		VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
E
Eric Anholt 已提交
2179

2180
	channel_mask = GENMASK(channels - 1, 0);
E
Eric Anholt 已提交
2181 2182 2183
	audio_packet_config |= VC4_SET_FIELD(channel_mask,
					     VC4_HDMI_AUDIO_PACKET_CEA_MASK);

2184 2185
	/* Set the MAI threshold */
	HDMI_WRITE(HDMI_MAI_THR,
2186 2187 2188 2189
		   VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICHIGH) |
		   VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICLOW) |
		   VC4_SET_FIELD(0x06, VC4_HD_MAI_THR_DREQHIGH) |
		   VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_DREQLOW));
E
Eric Anholt 已提交
2190

2191
	HDMI_WRITE(HDMI_MAI_CONFIG,
E
Eric Anholt 已提交
2192
		   VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
2193
		   VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
E
Eric Anholt 已提交
2194 2195
		   VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));

2196
	channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
2197 2198
	HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
	HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
2199

2200
	vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
E
Eric Anholt 已提交
2201

2202 2203
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

2204
	memcpy(&vc4_hdmi->audio.infoframe, &params->cea, sizeof(params->cea));
2205 2206
	vc4_hdmi_set_audio_infoframe(encoder);

2207 2208 2209
out_dev_exit:
	drm_dev_exit(idx);
out:
2210 2211
	mutex_unlock(&vc4_hdmi->mutex);

2212
	return ret;
E
Eric Anholt 已提交
2213 2214 2215 2216 2217 2218 2219 2220
}

static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
	.name = "vc4-hdmi-cpu-dai-component",
};

static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
{
2221
	struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
E
Eric Anholt 已提交
2222

2223
	snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
E
Eric Anholt 已提交
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247

	return 0;
}

static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
	.name = "vc4-hdmi-cpu-dai",
	.probe  = vc4_hdmi_audio_cpu_dai_probe,
	.playback = {
		.stream_name = "Playback",
		.channels_min = 1,
		.channels_max = 8,
		.rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
			 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
			 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
			 SNDRV_PCM_RATE_192000,
		.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
	},
};

static const struct snd_dmaengine_pcm_config pcm_conf = {
	.chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
	.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
};

2248 2249 2250 2251 2252 2253
static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
				  uint8_t *buf, size_t len)
{
	struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
	struct drm_connector *connector = &vc4_hdmi->connector;

2254
	mutex_lock(&vc4_hdmi->mutex);
2255
	memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
2256
	mutex_unlock(&vc4_hdmi->mutex);
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267

	return 0;
}

static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
	.get_eld = vc4_hdmi_audio_get_eld,
	.prepare = vc4_hdmi_audio_prepare,
	.audio_shutdown = vc4_hdmi_audio_shutdown,
	.audio_startup = vc4_hdmi_audio_startup,
};

2268
static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
2269 2270 2271 2272 2273
	.ops = &vc4_hdmi_codec_ops,
	.max_i2s_channels = 8,
	.i2s = 1,
};

2274 2275 2276 2277 2278 2279 2280 2281
static void vc4_hdmi_audio_codec_release(void *ptr)
{
	struct vc4_hdmi *vc4_hdmi = ptr;

	platform_device_unregister(vc4_hdmi->audio.codec_pdev);
	vc4_hdmi->audio.codec_pdev = NULL;
}

2282
static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
2283
{
2284 2285
	const struct vc4_hdmi_register *mai_data =
		&vc4_hdmi->variant->registers[HDMI_MAI_DATA];
2286 2287 2288
	struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
	struct snd_soc_card *card = &vc4_hdmi->audio.card;
	struct device *dev = &vc4_hdmi->pdev->dev;
2289
	struct platform_device *codec_pdev;
E
Eric Anholt 已提交
2290
	const __be32 *addr;
2291
	int index, len;
E
Eric Anholt 已提交
2292 2293
	int ret;

2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
	/*
	 * ASoC makes it a bit hard to retrieve a pointer to the
	 * vc4_hdmi structure. Registering the card will overwrite our
	 * device drvdata with a pointer to the snd_soc_card structure,
	 * which can then be used to retrieve whatever drvdata we want
	 * to associate.
	 *
	 * However, that doesn't fly in the case where we wouldn't
	 * register an ASoC card (because of an old DT that is missing
	 * the dmas properties for example), then the card isn't
	 * registered and the device drvdata wouldn't be set.
	 *
	 * We can deal with both cases by making sure a snd_soc_card
	 * pointer and a vc4_hdmi structure are pointing to the same
	 * memory address, so we can treat them indistinctly without any
	 * issue.
	 */
	BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
	BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);

2314
	if (!of_find_property(dev->of_node, "dmas", &len) || !len) {
E
Eric Anholt 已提交
2315
		dev_warn(dev,
2316
			 "'dmas' DT property is missing or empty, no HDMI audio\n");
E
Eric Anholt 已提交
2317 2318 2319
		return 0;
	}

2320 2321 2322 2323 2324
	if (mai_data->reg != VC4_HD) {
		WARN_ONCE(true, "MAI isn't in the HD block\n");
		return -EINVAL;
	}

E
Eric Anholt 已提交
2325 2326 2327 2328 2329 2330 2331
	/*
	 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
	 * the bus address specified in the DT, because the physical address
	 * (the one returned by platform_get_resource()) is not appropriate
	 * for DMA transfers.
	 * This VC/MMU should probably be exposed to avoid this kind of hacks.
	 */
2332 2333 2334 2335 2336 2337 2338
	index = of_property_match_string(dev->of_node, "reg-names", "hd");
	/* Before BCM2711, we don't have a named register range */
	if (index < 0)
		index = 1;

	addr = of_get_address(dev->of_node, index, NULL, NULL);

2339
	vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
2340 2341
	vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
	vc4_hdmi->audio.dma_data.maxburst = 2;
E
Eric Anholt 已提交
2342

2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
	/*
	 * NOTE: Strictly speaking, we should probably use a DRM-managed
	 * registration there to avoid removing all the audio components
	 * by the time the driver doesn't have any user anymore.
	 *
	 * However, the ASoC core uses a number of devm_kzalloc calls
	 * when registering, even when using non-device-managed
	 * functions (such as in snd_soc_register_component()).
	 *
	 * If we call snd_soc_unregister_component() in a DRM-managed
	 * action, the device-managed actions have already been executed
	 * and thus we would access memory that has been freed.
	 *
	 * Using device-managed hooks here probably leaves us open to a
	 * bunch of issues if userspace still has a handle on the ALSA
	 * device when the device is removed. However, this is mitigated
	 * by the use of drm_dev_enter()/drm_dev_exit() in the audio
	 * path to prevent the access to the device resources if it
	 * isn't there anymore.
	 *
	 * Then, the vc4_hdmi structure is DRM-managed and thus only
	 * freed whenever the last user has closed the DRM device file.
	 * It should thus outlive ALSA in most situations.
	 */
E
Eric Anholt 已提交
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
	ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
	if (ret) {
		dev_err(dev, "Could not register PCM component: %d\n", ret);
		return ret;
	}

	ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
					      &vc4_hdmi_audio_cpu_dai_drv, 1);
	if (ret) {
		dev_err(dev, "Could not register CPU DAI: %d\n", ret);
		return ret;
	}

2380 2381 2382 2383 2384 2385 2386
	codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
						   PLATFORM_DEVID_AUTO,
						   &vc4_hdmi_codec_pdata,
						   sizeof(vc4_hdmi_codec_pdata));
	if (IS_ERR(codec_pdev)) {
		dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
		return PTR_ERR(codec_pdev);
E
Eric Anholt 已提交
2387
	}
2388
	vc4_hdmi->audio.codec_pdev = codec_pdev;
E
Eric Anholt 已提交
2389

2390 2391 2392 2393
	ret = devm_add_action_or_reset(dev, vc4_hdmi_audio_codec_release, vc4_hdmi);
	if (ret)
		return ret;

2394 2395 2396
	dai_link->cpus		= &vc4_hdmi->audio.cpu;
	dai_link->codecs	= &vc4_hdmi->audio.codec;
	dai_link->platforms	= &vc4_hdmi->audio.platform;
2397 2398 2399

	dai_link->num_cpus	= 1;
	dai_link->num_codecs	= 1;
2400
	dai_link->num_platforms	= 1;
2401

E
Eric Anholt 已提交
2402 2403
	dai_link->name = "MAI";
	dai_link->stream_name = "MAI PCM";
2404
	dai_link->codecs->dai_name = "i2s-hifi";
2405
	dai_link->cpus->dai_name = dev_name(dev);
2406
	dai_link->codecs->name = dev_name(&codec_pdev->dev);
2407
	dai_link->platforms->name = dev_name(dev);
E
Eric Anholt 已提交
2408 2409 2410

	card->dai_link = dai_link;
	card->num_links = 1;
2411
	card->name = vc4_hdmi->variant->card_name;
2412
	card->driver_name = "vc4-hdmi";
E
Eric Anholt 已提交
2413
	card->dev = dev;
2414
	card->owner = THIS_MODULE;
E
Eric Anholt 已提交
2415 2416 2417 2418 2419 2420 2421 2422

	/*
	 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
	 * stores a pointer to the snd card object in dev->driver_data. This
	 * means we cannot use it for something else. The hdmi back-pointer is
	 * now stored in card->drvdata and should be retrieved with
	 * snd_soc_card_get_drvdata() if needed.
	 */
2423
	snd_soc_card_set_drvdata(card, vc4_hdmi);
E
Eric Anholt 已提交
2424
	ret = devm_snd_soc_register_card(dev, card);
2425
	if (ret)
2426
		dev_err_probe(dev, ret, "Could not register sound card\n");
E
Eric Anholt 已提交
2427 2428 2429 2430 2431

	return ret;

}

2432 2433 2434
static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
{
	struct vc4_hdmi *vc4_hdmi = priv;
2435 2436
	struct drm_connector *connector = &vc4_hdmi->connector;
	struct drm_device *dev = connector->dev;
2437

2438
	if (dev && dev->registered)
2439
		drm_connector_helper_hpd_irq_event(connector);
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450

	return IRQ_HANDLED;
}

static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
{
	struct drm_connector *connector = &vc4_hdmi->connector;
	struct platform_device *pdev = vc4_hdmi->pdev;
	int ret;

	if (vc4_hdmi->variant->external_irq_controller) {
2451 2452 2453
		unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
		unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");

2454 2455 2456 2457
		ret = devm_request_threaded_irq(&pdev->dev, hpd_con,
						NULL,
						vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
						"vc4 hdmi hpd connected", vc4_hdmi);
2458 2459 2460
		if (ret)
			return ret;

2461 2462 2463 2464 2465
		ret = devm_request_threaded_irq(&pdev->dev, hpd_rm,
						NULL,
						vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
						"vc4 hdmi hpd disconnected", vc4_hdmi);
		if (ret)
2466 2467 2468 2469 2470 2471 2472 2473
			return ret;

		connector->polled = DRM_CONNECTOR_POLL_HPD;
	}

	return 0;
}

H
Hans Verkuil 已提交
2474
#ifdef CONFIG_DRM_VC4_HDMI_CEC
2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
{
	struct vc4_hdmi *vc4_hdmi = priv;

	if (vc4_hdmi->cec_rx_msg.len)
		cec_received_msg(vc4_hdmi->cec_adap,
				 &vc4_hdmi->cec_rx_msg);

	return IRQ_HANDLED;
}

static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
H
Hans Verkuil 已提交
2487
{
2488
	struct vc4_hdmi *vc4_hdmi = priv;
2489

2490
	if (vc4_hdmi->cec_tx_ok) {
2491
		cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
H
Hans Verkuil 已提交
2492 2493 2494 2495 2496 2497
				  0, 0, 0, 0);
	} else {
		/*
		 * This CEC implementation makes 1 retry, so if we
		 * get a NACK, then that means it made 2 attempts.
		 */
2498
		cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
H
Hans Verkuil 已提交
2499 2500 2501 2502 2503
				  0, 2, 0, 0);
	}
	return IRQ_HANDLED;
}

2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
{
	struct vc4_hdmi *vc4_hdmi = priv;
	irqreturn_t ret;

	if (vc4_hdmi->cec_irq_was_rx)
		ret = vc4_cec_irq_handler_rx_thread(irq, priv);
	else
		ret = vc4_cec_irq_handler_tx_thread(irq, priv);

	return ret;
}

2517
static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
H
Hans Verkuil 已提交
2518
{
2519
	struct drm_device *dev = vc4_hdmi->connector.dev;
2520
	struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
H
Hans Verkuil 已提交
2521 2522
	unsigned int i;

2523 2524
	lockdep_assert_held(&vc4_hdmi->hw_lock);

H
Hans Verkuil 已提交
2525 2526
	msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
					VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
2527 2528 2529 2530 2531 2532

	if (msg->len > 16) {
		drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
		return;
	}

H
Hans Verkuil 已提交
2533
	for (i = 0; i < msg->len; i += 4) {
2534
		u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
H
Hans Verkuil 已提交
2535 2536 2537 2538 2539 2540 2541 2542

		msg->msg[i] = val & 0xff;
		msg->msg[i + 1] = (val >> 8) & 0xff;
		msg->msg[i + 2] = (val >> 16) & 0xff;
		msg->msg[i + 3] = (val >> 24) & 0xff;
	}
}

2543
static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2544 2545 2546
{
	u32 cntrl1;

2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
	/*
	 * We don't need to protect the register access using
	 * drm_dev_enter() there because the interrupt handler lifetime
	 * is tied to the device itself, and not to the DRM device.
	 *
	 * So when the device will be gone, one of the first thing we
	 * will be doing will be to unregister the interrupt handler,
	 * and then unregister the DRM device. drm_dev_enter() would
	 * thus always succeed if we are here.
	 */

2558 2559
	lockdep_assert_held(&vc4_hdmi->hw_lock);

2560 2561 2562 2563 2564 2565 2566 2567
	cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
	vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
	cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
	HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);

	return IRQ_WAKE_THREAD;
}

2568
static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
2569 2570
{
	struct vc4_hdmi *vc4_hdmi = priv;
2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
	irqreturn_t ret;

	spin_lock(&vc4_hdmi->hw_lock);
	ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
	spin_unlock(&vc4_hdmi->hw_lock);

	return ret;
}

static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
{
2582 2583
	u32 cntrl1;

2584 2585
	lockdep_assert_held(&vc4_hdmi->hw_lock);

2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
	/*
	 * We don't need to protect the register access using
	 * drm_dev_enter() there because the interrupt handler lifetime
	 * is tied to the device itself, and not to the DRM device.
	 *
	 * So when the device will be gone, one of the first thing we
	 * will be doing will be to unregister the interrupt handler,
	 * and then unregister the DRM device. drm_dev_enter() would
	 * thus always succeed if we are here.
	 */

2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
	vc4_hdmi->cec_rx_msg.len = 0;
	cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
	vc4_cec_read_msg(vc4_hdmi, cntrl1);
	cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
	HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
	cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;

	HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);

	return IRQ_WAKE_THREAD;
}

2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
{
	struct vc4_hdmi *vc4_hdmi = priv;
	irqreturn_t ret;

	spin_lock(&vc4_hdmi->hw_lock);
	ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
	spin_unlock(&vc4_hdmi->hw_lock);

	return ret;
}

H
Hans Verkuil 已提交
2621 2622
static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
{
2623
	struct vc4_hdmi *vc4_hdmi = priv;
2624
	u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
2625 2626
	irqreturn_t ret;
	u32 cntrl5;
H
Hans Verkuil 已提交
2627

2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
	/*
	 * We don't need to protect the register access using
	 * drm_dev_enter() there because the interrupt handler lifetime
	 * is tied to the device itself, and not to the DRM device.
	 *
	 * So when the device will be gone, one of the first thing we
	 * will be doing will be to unregister the interrupt handler,
	 * and then unregister the DRM device. drm_dev_enter() would
	 * thus always succeed if we are here.
	 */

H
Hans Verkuil 已提交
2639 2640
	if (!(stat & VC4_HDMI_CPU_CEC))
		return IRQ_NONE;
2641

2642
	spin_lock(&vc4_hdmi->hw_lock);
2643
	cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
2644
	vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
2645
	if (vc4_hdmi->cec_irq_was_rx)
2646
		ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
2647
	else
2648
		ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
H
Hans Verkuil 已提交
2649

2650
	HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
2651 2652
	spin_unlock(&vc4_hdmi->hw_lock);

2653
	return ret;
H
Hans Verkuil 已提交
2654 2655
}

2656
static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
H
Hans Verkuil 已提交
2657
{
2658
	struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2659
	struct drm_device *drm = vc4_hdmi->connector.dev;
H
Hans Verkuil 已提交
2660 2661
	/* clock period in microseconds */
	const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
2662
	unsigned long flags;
2663 2664
	u32 val;
	int ret;
2665
	int idx;
2666

2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
	/*
	 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
	 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
	 * .detect or .get_modes might call .adap_enable, which leads to this
	 * function being called with that mutex held.
	 *
	 * Concurrency is not an issue for the moment since we don't share any
	 * state with KMS, so we can ignore the lock for now, but we need to
	 * keep it in mind if we were to change that assumption.
	 */

2678 2679 2680 2681 2682 2683 2684 2685
	if (!drm_dev_enter(drm, &idx))
		/*
		 * We can't return an error code, because the CEC
		 * framework will emit WARN_ON messages at unbind
		 * otherwise.
		 */
		return 0;

2686
	ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
2687 2688
	if (ret) {
		drm_dev_exit(idx);
2689
		return ret;
2690
	}
H
Hans Verkuil 已提交
2691

2692 2693
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

2694
	val = HDMI_READ(HDMI_CEC_CNTRL_5);
H
Hans Verkuil 已提交
2695 2696 2697 2698 2699 2700
	val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
		 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
		 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
	val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
	       ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);

2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
	HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
		   VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
	HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
	HDMI_WRITE(HDMI_CEC_CNTRL_2,
		   ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
		   ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
		   ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
		   ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
		   ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
	HDMI_WRITE(HDMI_CEC_CNTRL_3,
		   ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
		   ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
		   ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
		   ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
	HDMI_WRITE(HDMI_CEC_CNTRL_4,
		   ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
		   ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
		   ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
		   ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));

	if (!vc4_hdmi->variant->external_irq_controller)
		HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);

2724 2725
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

2726 2727
	drm_dev_exit(idx);

2728 2729 2730 2731 2732 2733
	return 0;
}

static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
{
	struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2734
	struct drm_device *drm = vc4_hdmi->connector.dev;
2735
	unsigned long flags;
2736 2737 2738 2739 2740 2741 2742 2743 2744
	int idx;

	if (!drm_dev_enter(drm, &idx))
		/*
		 * We can't return an error code, because the CEC
		 * framework will emit WARN_ON messages at unbind
		 * otherwise.
		 */
		return 0;
2745

2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
	/*
	 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
	 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
	 * .detect or .get_modes might call .adap_enable, which leads to this
	 * function being called with that mutex held.
	 *
	 * Concurrency is not an issue for the moment since we don't share any
	 * state with KMS, so we can ignore the lock for now, but we need to
	 * keep it in mind if we were to change that assumption.
	 */

2757
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2758 2759 2760 2761 2762 2763 2764

	if (!vc4_hdmi->variant->external_irq_controller)
		HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);

	HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
		   VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);

2765 2766
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

2767 2768
	pm_runtime_put(&vc4_hdmi->pdev->dev);

2769 2770
	drm_dev_exit(idx);

H
Hans Verkuil 已提交
2771 2772 2773
	return 0;
}

2774 2775 2776 2777 2778 2779 2780 2781
static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
{
	if (enable)
		return vc4_hdmi_cec_enable(adap);
	else
		return vc4_hdmi_cec_disable(adap);
}

H
Hans Verkuil 已提交
2782 2783
static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
{
2784
	struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2785
	struct drm_device *drm = vc4_hdmi->connector.dev;
2786
	unsigned long flags;
2787
	int idx;
H
Hans Verkuil 已提交
2788

2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799
	/*
	 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
	 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
	 * .detect or .get_modes might call .adap_enable, which leads to this
	 * function being called with that mutex held.
	 *
	 * Concurrency is not an issue for the moment since we don't share any
	 * state with KMS, so we can ignore the lock for now, but we need to
	 * keep it in mind if we were to change that assumption.
	 */

2800 2801 2802 2803 2804 2805 2806 2807
	if (!drm_dev_enter(drm, &idx))
		/*
		 * We can't return an error code, because the CEC
		 * framework will emit WARN_ON messages at unbind
		 * otherwise.
		 */
		return 0;

2808
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2809 2810
	HDMI_WRITE(HDMI_CEC_CNTRL_1,
		   (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
H
Hans Verkuil 已提交
2811
		   (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
2812 2813
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

2814 2815
	drm_dev_exit(idx);

H
Hans Verkuil 已提交
2816 2817 2818 2819 2820 2821
	return 0;
}

static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
				      u32 signal_free_time, struct cec_msg *msg)
{
2822
	struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2823
	struct drm_device *dev = vc4_hdmi->connector.dev;
2824
	unsigned long flags;
H
Hans Verkuil 已提交
2825 2826
	u32 val;
	unsigned int i;
2827
	int idx;
H
Hans Verkuil 已提交
2828

2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839
	/*
	 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
	 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
	 * .detect or .get_modes might call .adap_enable, which leads to this
	 * function being called with that mutex held.
	 *
	 * Concurrency is not an issue for the moment since we don't share any
	 * state with KMS, so we can ignore the lock for now, but we need to
	 * keep it in mind if we were to change that assumption.
	 */

2840 2841 2842
	if (!drm_dev_enter(dev, &idx))
		return -ENODEV;

2843 2844
	if (msg->len > 16) {
		drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
2845
		drm_dev_exit(idx);
2846 2847 2848
		return -ENOMEM;
	}

2849 2850
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);

H
Hans Verkuil 已提交
2851
	for (i = 0; i < msg->len; i += 4)
2852
		HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
H
Hans Verkuil 已提交
2853 2854 2855 2856 2857
			   (msg->msg[i]) |
			   (msg->msg[i + 1] << 8) |
			   (msg->msg[i + 2] << 16) |
			   (msg->msg[i + 3] << 24));

2858
	val = HDMI_READ(HDMI_CEC_CNTRL_1);
H
Hans Verkuil 已提交
2859
	val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
2860
	HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
H
Hans Verkuil 已提交
2861 2862 2863 2864
	val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
	val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
	val |= VC4_HDMI_CEC_START_XMIT_BEGIN;

2865
	HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
2866 2867 2868

	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

2869 2870
	drm_dev_exit(idx);

H
Hans Verkuil 已提交
2871 2872 2873 2874 2875 2876 2877 2878 2879
	return 0;
}

static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
	.adap_enable = vc4_hdmi_cec_adap_enable,
	.adap_log_addr = vc4_hdmi_cec_adap_log_addr,
	.adap_transmit = vc4_hdmi_cec_adap_transmit,
};

2880 2881 2882 2883 2884 2885 2886 2887
static void vc4_hdmi_cec_release(void *ptr)
{
	struct vc4_hdmi *vc4_hdmi = ptr;

	cec_unregister_adapter(vc4_hdmi->cec_adap);
	vc4_hdmi->cec_adap = NULL;
}

2888
static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
2889
{
2890
	struct cec_connector_info conn_info;
2891
	struct platform_device *pdev = vc4_hdmi->pdev;
2892
	struct device *dev = &pdev->dev;
2893 2894
	int ret;

2895 2896 2897 2898 2899
	if (!of_find_property(dev->of_node, "interrupts", NULL)) {
		dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
		return 0;
	}

2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
	vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
						  vc4_hdmi, "vc4",
						  CEC_CAP_DEFAULTS |
						  CEC_CAP_CONNECTOR_INFO, 1);
	ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
	if (ret < 0)
		return ret;

	cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
	cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);

2911
	if (vc4_hdmi->variant->external_irq_controller) {
2912 2913 2914 2915
		ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-rx"),
						vc4_cec_irq_handler_rx_bare,
						vc4_cec_irq_handler_rx_thread, 0,
						"vc4 hdmi cec rx", vc4_hdmi);
2916 2917 2918
		if (ret)
			goto err_delete_cec_adap;

2919 2920 2921 2922
		ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-tx"),
						vc4_cec_irq_handler_tx_bare,
						vc4_cec_irq_handler_tx_thread, 0,
						"vc4 hdmi cec tx", vc4_hdmi);
2923
		if (ret)
2924
			goto err_delete_cec_adap;
2925
	} else {
2926 2927 2928 2929
		ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
						vc4_cec_irq_handler,
						vc4_cec_irq_handler_thread, 0,
						"vc4 hdmi cec", vc4_hdmi);
2930 2931 2932
		if (ret)
			goto err_delete_cec_adap;
	}
2933 2934 2935

	ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
	if (ret < 0)
2936
		goto err_delete_cec_adap;
2937

2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
	/*
	 * NOTE: Strictly speaking, we should probably use a DRM-managed
	 * registration there to avoid removing the CEC adapter by the
	 * time the DRM driver doesn't have any user anymore.
	 *
	 * However, the CEC framework already cleans up the CEC adapter
	 * only when the last user has closed its file descriptor, so we
	 * don't need to handle it in DRM.
	 *
	 * By the time the device-managed hook is executed, we will give
	 * up our reference to the CEC adapter and therefore don't
	 * really care when it's actually freed.
	 *
	 * There's still a problematic sequence: if we unregister our
	 * CEC adapter, but the userspace keeps a handle on the CEC
	 * adapter but not the DRM device for some reason. In such a
	 * case, our vc4_hdmi structure will be freed, but the
	 * cec_adapter structure will have a dangling pointer to what
	 * used to be our HDMI controller. If we get a CEC call at that
	 * moment, we could end up with a use-after-free. Fortunately,
	 * the CEC framework already handles this too, by calling
	 * cec_is_registered() in cec_ioctl() and cec_poll().
	 */
	ret = devm_add_action_or_reset(dev, vc4_hdmi_cec_release, vc4_hdmi);
	if (ret)
		return ret;
2964

2965
	return 0;
2966

2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
err_delete_cec_adap:
	cec_delete_adapter(vc4_hdmi->cec_adap);

	return ret;
}
#else
static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
{
	return 0;
}
H
Hans Verkuil 已提交
2977 2978
#endif

2979 2980 2981 2982 2983 2984 2985 2986 2987
static void vc4_hdmi_free_regset(struct drm_device *drm, void *ptr)
{
	struct debugfs_reg32 *regs = ptr;

	kfree(regs);
}

static int vc4_hdmi_build_regset(struct drm_device *drm,
				 struct vc4_hdmi *vc4_hdmi,
2988 2989 2990 2991 2992 2993 2994
				 struct debugfs_regset32 *regset,
				 enum vc4_hdmi_regs reg)
{
	const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
	struct debugfs_reg32 *regs, *new_regs;
	unsigned int count = 0;
	unsigned int i;
2995
	int ret;
2996 2997 2998 2999

	regs = kcalloc(variant->num_registers, sizeof(*regs),
		       GFP_KERNEL);
	if (!regs)
3000 3001
		return -ENOMEM;

3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
	for (i = 0; i < variant->num_registers; i++) {
		const struct vc4_hdmi_register *field =	&variant->registers[i];

		if (field->reg != reg)
			continue;

		regs[count].name = field->name;
		regs[count].offset = field->offset;
		count++;
	}

	new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
	if (!new_regs)
3015
		return -ENOMEM;
3016 3017 3018 3019 3020

	regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
	regset->regs = new_regs;
	regset->nregs = count;

3021 3022 3023 3024
	ret = drmm_add_action_or_reset(drm, vc4_hdmi_free_regset, new_regs);
	if (ret)
		return ret;

3025 3026 3027
	return 0;
}

3028 3029
static int vc4_hdmi_init_resources(struct drm_device *drm,
				   struct vc4_hdmi *vc4_hdmi)
3030
{
3031 3032
	struct platform_device *pdev = vc4_hdmi->pdev;
	struct device *dev = &pdev->dev;
3033 3034
	int ret;

3035 3036 3037
	vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
	if (IS_ERR(vc4_hdmi->hdmicore_regs))
		return PTR_ERR(vc4_hdmi->hdmicore_regs);
3038

3039 3040 3041
	vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
	if (IS_ERR(vc4_hdmi->hd_regs))
		return PTR_ERR(vc4_hdmi->hd_regs);
3042

3043
	ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3044 3045
	if (ret)
		return ret;
3046

3047
	ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3048 3049
	if (ret)
		return ret;
3050

3051 3052 3053
	vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
	if (IS_ERR(vc4_hdmi->pixel_clock)) {
		ret = PTR_ERR(vc4_hdmi->pixel_clock);
3054 3055 3056
		if (ret != -EPROBE_DEFER)
			DRM_ERROR("Failed to get pixel clock\n");
		return ret;
3057
	}
3058

3059 3060
	vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
	if (IS_ERR(vc4_hdmi->hsm_clock)) {
3061
		DRM_ERROR("Failed to get HDMI state machine clock\n");
3062
		return PTR_ERR(vc4_hdmi->hsm_clock);
3063
	}
3064
	vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
3065
	vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
3066

3067 3068 3069
	return 0;
}

3070 3071
static int vc5_hdmi_init_resources(struct drm_device *drm,
				   struct vc4_hdmi *vc4_hdmi)
3072 3073 3074 3075
{
	struct platform_device *pdev = vc4_hdmi->pdev;
	struct device *dev = &pdev->dev;
	struct resource *res;
3076
	int ret;
3077 3078 3079 3080 3081 3082 3083

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
	if (!res)
		return -ENODEV;

	vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
					       resource_size(res));
3084 3085
	if (!vc4_hdmi->hdmicore_regs)
		return -ENOMEM;
3086 3087 3088 3089 3090 3091

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
	if (!res)
		return -ENODEV;

	vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
3092 3093
	if (!vc4_hdmi->hd_regs)
		return -ENOMEM;
3094 3095 3096 3097 3098 3099

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
	if (!res)
		return -ENODEV;

	vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
3100 3101
	if (!vc4_hdmi->cec_regs)
		return -ENOMEM;
3102 3103 3104 3105 3106 3107

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
	if (!res)
		return -ENODEV;

	vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
3108 3109
	if (!vc4_hdmi->csc_regs)
		return -ENOMEM;
3110 3111 3112 3113 3114 3115

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
	if (!res)
		return -ENODEV;

	vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
3116 3117
	if (!vc4_hdmi->dvp_regs)
		return -ENOMEM;
3118 3119 3120 3121 3122 3123

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
	if (!res)
		return -ENODEV;

	vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
3124 3125
	if (!vc4_hdmi->phy_regs)
		return -ENOMEM;
3126 3127 3128 3129 3130 3131

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
	if (!res)
		return -ENODEV;

	vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
3132 3133
	if (!vc4_hdmi->ram_regs)
		return -ENOMEM;
3134 3135 3136 3137 3138 3139

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
	if (!res)
		return -ENODEV;

	vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
3140 3141
	if (!vc4_hdmi->rm_regs)
		return -ENOMEM;
3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160

	vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
	if (IS_ERR(vc4_hdmi->hsm_clock)) {
		DRM_ERROR("Failed to get HDMI state machine clock\n");
		return PTR_ERR(vc4_hdmi->hsm_clock);
	}

	vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
	if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
		DRM_ERROR("Failed to get pixel bvb clock\n");
		return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
	}

	vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
	if (IS_ERR(vc4_hdmi->audio_clock)) {
		DRM_ERROR("Failed to get audio clock\n");
		return PTR_ERR(vc4_hdmi->audio_clock);
	}

3161 3162 3163 3164 3165 3166
	vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
	if (IS_ERR(vc4_hdmi->cec_clock)) {
		DRM_ERROR("Failed to get CEC clock\n");
		return PTR_ERR(vc4_hdmi->cec_clock);
	}

3167 3168 3169 3170 3171 3172
	vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
	if (IS_ERR(vc4_hdmi->reset)) {
		DRM_ERROR("Failed to get HDMI reset line\n");
		return PTR_ERR(vc4_hdmi->reset);
	}

3173
	ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3174 3175 3176
	if (ret)
		return ret;

3177
	ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3178 3179 3180
	if (ret)
		return ret;

3181
	ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->cec_regset, VC5_CEC);
3182 3183 3184
	if (ret)
		return ret;

3185
	ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->csc_regset, VC5_CSC);
3186 3187 3188
	if (ret)
		return ret;

3189
	ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->dvp_regset, VC5_DVP);
3190 3191 3192
	if (ret)
		return ret;

3193
	ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->phy_regset, VC5_PHY);
3194 3195 3196
	if (ret)
		return ret;

3197
	ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->ram_regset, VC5_RAM);
3198 3199 3200
	if (ret)
		return ret;

3201
	ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->rm_regset, VC5_RM);
3202 3203 3204
	if (ret)
		return ret;

3205 3206 3207
	return 0;
}

3208
static int vc4_hdmi_runtime_suspend(struct device *dev)
3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
{
	struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);

	clk_disable_unprepare(vc4_hdmi->hsm_clock);

	return 0;
}

static int vc4_hdmi_runtime_resume(struct device *dev)
{
	struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3220 3221
	unsigned long __maybe_unused flags;
	u32 __maybe_unused value;
3222 3223 3224 3225 3226 3227
	int ret;

	ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
	if (ret)
		return ret;

3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247
	if (vc4_hdmi->variant->reset)
		vc4_hdmi->variant->reset(vc4_hdmi);

#ifdef CONFIG_DRM_VC4_HDMI_CEC
	spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
	value = HDMI_READ(HDMI_CEC_CNTRL_1);
	/* Set the logical address to Unregistered */
	value |= VC4_HDMI_CEC_ADDR_MASK;
	HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
	spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);

	vc4_hdmi_cec_update_clk_div(vc4_hdmi);

	if (!vc4_hdmi->variant->external_irq_controller) {
		spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
		HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
		spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
	}
#endif

3248 3249 3250
	return 0;
}

3251 3252 3253 3254 3255 3256 3257
static void vc4_hdmi_put_ddc_device(void *ptr)
{
	struct vc4_hdmi *vc4_hdmi = ptr;

	put_device(&vc4_hdmi->ddc->dev);
}

3258 3259 3260 3261 3262 3263 3264 3265 3266 3267
static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
{
	const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
	struct platform_device *pdev = to_platform_device(dev);
	struct drm_device *drm = dev_get_drvdata(master);
	struct vc4_hdmi *vc4_hdmi;
	struct drm_encoder *encoder;
	struct device_node *ddc_node;
	int ret;

3268
	vc4_hdmi = drmm_kzalloc(drm, sizeof(*vc4_hdmi), GFP_KERNEL);
3269 3270
	if (!vc4_hdmi)
		return -ENOMEM;
3271

3272 3273 3274 3275
	ret = drmm_mutex_init(drm, &vc4_hdmi->mutex);
	if (ret)
		return ret;

3276
	spin_lock_init(&vc4_hdmi->hw_lock);
3277
	INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
3278 3279

	dev_set_drvdata(dev, vc4_hdmi);
3280 3281 3282 3283 3284 3285 3286
	encoder = &vc4_hdmi->encoder.base;
	vc4_hdmi->encoder.type = variant->encoder_type;
	vc4_hdmi->encoder.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
	vc4_hdmi->encoder.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
	vc4_hdmi->encoder.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
	vc4_hdmi->encoder.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
	vc4_hdmi->encoder.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
3287 3288 3289
	vc4_hdmi->pdev = pdev;
	vc4_hdmi->variant = variant;

3290 3291 3292 3293 3294 3295
	/*
	 * Since we don't know the state of the controller and its
	 * display (if any), let's assume it's always enabled.
	 * vc4_hdmi_disable_scrambling() will thus run at boot, make
	 * sure it's disabled, and avoid any inconsistency.
	 */
3296 3297
	if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK)
		vc4_hdmi->scdc_enabled = true;
3298

3299
	ret = variant->init_resources(drm, vc4_hdmi);
3300 3301
	if (ret)
		return ret;
3302

3303 3304 3305 3306 3307 3308
	ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
	if (!ddc_node) {
		DRM_ERROR("Failed to find ddc node in device tree\n");
		return -ENODEV;
	}

3309
	vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
3310
	of_node_put(ddc_node);
3311
	if (!vc4_hdmi->ddc) {
3312 3313 3314 3315
		DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
		return -EPROBE_DEFER;
	}

3316 3317 3318 3319
	ret = devm_add_action_or_reset(dev, vc4_hdmi_put_ddc_device, vc4_hdmi);
	if (ret)
		return ret;

3320 3321 3322
	/* Only use the GPIO HPD pin if present in the DT, otherwise
	 * we'll use the HDMI core's register.
	 */
M
Maxime Ripard 已提交
3323 3324
	vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
	if (IS_ERR(vc4_hdmi->hpd_gpio)) {
3325
		return PTR_ERR(vc4_hdmi->hpd_gpio);
3326 3327
	}

3328 3329 3330
	vc4_hdmi->disable_wifi_frequencies =
		of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");

3331 3332 3333 3334 3335 3336 3337 3338
	if (variant->max_pixel_clock == 600000000) {
		struct vc4_dev *vc4 = to_vc4_dev(drm);
		long max_rate = clk_round_rate(vc4->hvs->core_clk, 550000000);

		if (max_rate < 550000000)
			vc4_hdmi->disable_4kp60 = true;
	}

3339 3340 3341
	ret = devm_pm_runtime_enable(dev);
	if (ret)
		return ret;
M
Maxime Ripard 已提交
3342

3343
	/*
M
Maxime Ripard 已提交
3344 3345
	 *  We need to have the device powered up at this point to call
	 *  our reset hook and for the CEC init.
3346
	 */
M
Maxime Ripard 已提交
3347
	ret = pm_runtime_resume_and_get(dev);
3348
	if (ret)
3349
		return ret;
3350

3351 3352 3353 3354 3355 3356 3357 3358
	if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
	     of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
	    HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
		clk_prepare_enable(vc4_hdmi->pixel_clock);
		clk_prepare_enable(vc4_hdmi->hsm_clock);
		clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
	}

3359
	ret = drmm_encoder_init(drm, encoder,
3360
				&vc4_hdmi_encoder_funcs,
3361 3362 3363 3364 3365
				DRM_MODE_ENCODER_TMDS,
				NULL);
	if (ret)
		goto err_put_runtime_pm;

3366
	drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
3367

3368
	ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
3369
	if (ret)
3370
		goto err_put_runtime_pm;
3371

3372
	ret = vc4_hdmi_hotplug_init(vc4_hdmi);
H
Hans Verkuil 已提交
3373
	if (ret)
3374
		goto err_put_runtime_pm;
3375

3376
	ret = vc4_hdmi_cec_init(vc4_hdmi);
H
Hans Verkuil 已提交
3377
	if (ret)
3378
		goto err_put_runtime_pm;
3379

3380
	ret = vc4_hdmi_audio_init(vc4_hdmi);
E
Eric Anholt 已提交
3381
	if (ret)
3382
		goto err_put_runtime_pm;
E
Eric Anholt 已提交
3383

3384 3385
	pm_runtime_put_sync(dev);

3386 3387
	return 0;

3388
err_put_runtime_pm:
3389
	pm_runtime_put_sync(dev);
3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408

	return ret;
}

static const struct component_ops vc4_hdmi_ops = {
	.bind   = vc4_hdmi_bind,
};

static int vc4_hdmi_dev_probe(struct platform_device *pdev)
{
	return component_add(&pdev->dev, &vc4_hdmi_ops);
}

static int vc4_hdmi_dev_remove(struct platform_device *pdev)
{
	component_del(&pdev->dev, &vc4_hdmi_ops);
	return 0;
}

3409
static const struct vc4_hdmi_variant bcm2835_variant = {
3410
	.encoder_type		= VC4_ENCODER_TYPE_HDMI0,
3411
	.debugfs_name		= "hdmi_regs",
3412
	.card_name		= "vc4-hdmi",
3413
	.max_pixel_clock	= 162000000,
3414 3415 3416
	.registers		= vc4_hdmi_fields,
	.num_registers		= ARRAY_SIZE(vc4_hdmi_fields),

3417
	.init_resources		= vc4_hdmi_init_resources,
3418
	.csc_setup		= vc4_hdmi_csc_setup,
3419
	.reset			= vc4_hdmi_reset,
3420
	.set_timings		= vc4_hdmi_set_timings,
3421 3422
	.phy_init		= vc4_hdmi_phy_init,
	.phy_disable		= vc4_hdmi_phy_disable,
3423 3424
	.phy_rng_enable		= vc4_hdmi_phy_rng_enable,
	.phy_rng_disable	= vc4_hdmi_phy_rng_disable,
3425
	.channel_map		= vc4_hdmi_channel_map,
3426
	.supports_hdr		= false,
3427 3428
};

3429 3430 3431 3432
static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
	.encoder_type		= VC4_ENCODER_TYPE_HDMI0,
	.debugfs_name		= "hdmi0_regs",
	.card_name		= "vc4-hdmi-0",
3433
	.max_pixel_clock	= 600000000,
3434 3435 3436 3437 3438 3439 3440 3441
	.registers		= vc5_hdmi_hdmi0_fields,
	.num_registers		= ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
	.phy_lane_mapping	= {
		PHY_LANE_0,
		PHY_LANE_1,
		PHY_LANE_2,
		PHY_LANE_CK,
	},
3442
	.unsupported_odd_h_timings	= true,
3443
	.external_irq_controller	= true,
3444 3445 3446 3447 3448 3449 3450 3451 3452 3453

	.init_resources		= vc5_hdmi_init_resources,
	.csc_setup		= vc5_hdmi_csc_setup,
	.reset			= vc5_hdmi_reset,
	.set_timings		= vc5_hdmi_set_timings,
	.phy_init		= vc5_hdmi_phy_init,
	.phy_disable		= vc5_hdmi_phy_disable,
	.phy_rng_enable		= vc5_hdmi_phy_rng_enable,
	.phy_rng_disable	= vc5_hdmi_phy_rng_disable,
	.channel_map		= vc5_hdmi_channel_map,
3454
	.supports_hdr		= true,
3455
	.hp_detect		= vc5_hdmi_hp_detect,
3456 3457 3458 3459 3460 3461
};

static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
	.encoder_type		= VC4_ENCODER_TYPE_HDMI1,
	.debugfs_name		= "hdmi1_regs",
	.card_name		= "vc4-hdmi-1",
3462
	.max_pixel_clock	= HDMI_14_MAX_TMDS_CLK,
3463 3464 3465 3466 3467 3468 3469 3470
	.registers		= vc5_hdmi_hdmi1_fields,
	.num_registers		= ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
	.phy_lane_mapping	= {
		PHY_LANE_1,
		PHY_LANE_0,
		PHY_LANE_CK,
		PHY_LANE_2,
	},
3471
	.unsupported_odd_h_timings	= true,
3472
	.external_irq_controller	= true,
3473 3474 3475 3476 3477 3478 3479 3480 3481 3482

	.init_resources		= vc5_hdmi_init_resources,
	.csc_setup		= vc5_hdmi_csc_setup,
	.reset			= vc5_hdmi_reset,
	.set_timings		= vc5_hdmi_set_timings,
	.phy_init		= vc5_hdmi_phy_init,
	.phy_disable		= vc5_hdmi_phy_disable,
	.phy_rng_enable		= vc5_hdmi_phy_rng_enable,
	.phy_rng_disable	= vc5_hdmi_phy_rng_disable,
	.channel_map		= vc5_hdmi_channel_map,
3483
	.supports_hdr		= true,
3484
	.hp_detect		= vc5_hdmi_hp_detect,
3485 3486
};

3487
static const struct of_device_id vc4_hdmi_dt_match[] = {
3488
	{ .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
3489 3490
	{ .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
	{ .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
3491 3492 3493
	{}
};

3494 3495 3496 3497 3498 3499
static const struct dev_pm_ops vc4_hdmi_pm_ops = {
	SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
			   vc4_hdmi_runtime_resume,
			   NULL)
};

3500 3501 3502 3503 3504 3505
struct platform_driver vc4_hdmi_driver = {
	.probe = vc4_hdmi_dev_probe,
	.remove = vc4_hdmi_dev_remove,
	.driver = {
		.name = "vc4_hdmi",
		.of_match_table = vc4_hdmi_dt_match,
3506
		.pm = &vc4_hdmi_pm_ops,
3507 3508
	},
};