vc4_hdmi.c 57.8 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 35
#include <drm/drm_atomic_helper.h>
#include <drm/drm_edid.h>
36
#include <drm/drm_probe_helper.h>
T
Thomas Zimmermann 已提交
37
#include <drm/drm_simple_kms_helper.h>
38 39 40 41 42 43 44 45
#include <linux/clk.h>
#include <linux/component.h>
#include <linux/i2c.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <linux/rational.h>
46
#include <linux/reset.h>
47 48 49 50
#include <sound/dmaengine_pcm.h>
#include <sound/pcm_drm_eld.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
H
Hans Verkuil 已提交
51
#include "media/cec.h"
52
#include "vc4_drv.h"
53
#include "vc4_hdmi.h"
54
#include "vc4_hdmi_regs.h"
55 56
#include "vc4_regs.h"

57 58 59 60 61 62
#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 已提交
63

64 65 66 67
#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)
68

69 70 71 72 73 74
#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 已提交
75

76 77
#define VC5_HDMI_VERTB_VSPO_SHIFT		16
#define VC5_HDMI_VERTB_VSPO_MASK		VC4_MASK(29, 16)
78

79 80
# define VC4_HD_M_SW_RST			BIT(2)
# define VC4_HD_M_ENABLE			BIT(0)
H
Hans Verkuil 已提交
81 82

#define CEC_CLOCK_FREQ 40000
83
#define VC4_HSM_MID_CLOCK 149985000
84

85 86
#define HDMI_14_MAX_TMDS_CLK   (340 * 1000 * 1000)

87
static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
88 89
{
	struct drm_info_node *node = (struct drm_info_node *)m->private;
90
	struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
91
	struct drm_printer p = drm_seq_file_printer(m);
92

93 94
	drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
	drm_print_regset32(&p, &vc4_hdmi->hd_regset);
95 96 97 98

	return 0;
}

99
static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
100
{
101 102 103
	HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
	udelay(1);
	HDMI_WRITE(HDMI_M_CTL, 0);
104

105
	HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
106

107 108 109
	HDMI_WRITE(HDMI_SW_RESET_CONTROL,
		   VC4_HDMI_SW_RESET_HDMI |
		   VC4_HDMI_SW_RESET_FORMAT_DETECT);
110

111
	HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
112 113
}

114
static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
115
{
116
	reset_control_reset(vc4_hdmi->reset);
117

118
	HDMI_WRITE(HDMI_DVP_CTL, 0);
119

120 121
	HDMI_WRITE(HDMI_CLOCK_STOP,
		   HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
122 123 124 125 126
}

static enum drm_connector_status
vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
{
127
	struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
128

129 130 131
	if (vc4_hdmi->hpd_gpio) {
		if (gpio_get_value_cansleep(vc4_hdmi->hpd_gpio) ^
		    vc4_hdmi->hpd_active_low)
132
			return connector_status_connected;
133
		cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
H
Hans Verkuil 已提交
134
		return connector_status_disconnected;
135 136
	}

137
	if (drm_probe_ddc(vc4_hdmi->ddc))
138 139
		return connector_status_connected;

140
	if (HDMI_READ(HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
141
		return connector_status_connected;
142
	cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
H
Hans Verkuil 已提交
143
	return connector_status_disconnected;
144 145 146 147 148 149 150 151 152 153
}

static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
{
	drm_connector_unregister(connector);
	drm_connector_cleanup(connector);
}

static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
{
154 155
	struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
	struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
156 157 158
	int ret = 0;
	struct edid *edid;

159 160
	edid = drm_get_edid(connector, vc4_hdmi->ddc);
	cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
161 162 163 164
	if (!edid)
		return -ENODEV;

	vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
165

166
	drm_connector_update_edid_property(connector, edid);
167
	ret = drm_add_edid_modes(connector, edid);
E
Eric Anholt 已提交
168
	kfree(edid);
169 170 171 172

	return ret;
}

173 174
static void vc4_hdmi_connector_reset(struct drm_connector *connector)
{
175 176 177 178
	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);
179 180

	if (connector->state)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
		__drm_atomic_helper_connector_destroy_state(connector->state);

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

	if (!new_state)
		return;

	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;

203
	new_state->pixel_rate = vc4_state->pixel_rate;
204 205 206
	__drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);

	return &new_state->base;
207 208
}

209 210
static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
	.detect = vc4_hdmi_connector_detect,
211
	.fill_modes = drm_helper_probe_single_connector_modes,
212
	.destroy = vc4_hdmi_connector_destroy,
213
	.reset = vc4_hdmi_connector_reset,
214
	.atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
215 216 217 218 219 220 221
	.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,
};

222
static int vc4_hdmi_connector_init(struct drm_device *dev,
223
				   struct vc4_hdmi *vc4_hdmi)
224
{
225
	struct drm_connector *connector = &vc4_hdmi->connector;
226
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
227
	int ret;
228

229 230 231
	drm_connector_init_with_ddc(dev, connector,
				    &vc4_hdmi_connector_funcs,
				    DRM_MODE_CONNECTOR_HDMIA,
232
				    vc4_hdmi->ddc);
233 234
	drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);

235 236 237
	/* Create and attach TV margin props to this connector. */
	ret = drm_mode_create_tv_margin_properties(dev);
	if (ret)
238
		return ret;
239 240 241

	drm_connector_attach_tv_margin_properties(connector);

242 243 244
	connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
			     DRM_CONNECTOR_POLL_DISCONNECT);

245
	connector->interlace_allowed = 1;
246 247
	connector->doublescan_allowed = 0;

248
	drm_connector_attach_encoder(connector, encoder);
249

250
	return 0;
251 252
}

253
static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
254 255
				enum hdmi_infoframe_type type,
				bool poll)
256
{
257
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
258 259
	u32 packet_id = type - 0x80;

260 261
	HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
		   HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
262

263 264 265
	if (!poll)
		return 0;

266
	return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
267 268 269 270 271 272
			  BIT(packet_id)), 100);
}

static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
				     union hdmi_infoframe *frame)
{
273
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
274
	u32 packet_id = frame->any.type - 0x80;
275 276 277 278 279
	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;
	void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
						       ram_packet_start->reg);
280 281 282 283
	uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
	ssize_t len, i;
	int ret;

284
	WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
285 286 287 288 289 290 291
		    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)
		return;

292
	ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
293 294 295 296 297 298
	if (ret) {
		DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
		return;
	}

	for (i = 0; i < len; i += 7) {
299 300 301 302
		writel(buffer[i + 0] << 0 |
		       buffer[i + 1] << 8 |
		       buffer[i + 2] << 16,
		       base + packet_reg);
303 304
		packet_reg += 4;

305 306 307 308 309
		writel(buffer[i + 3] << 0 |
		       buffer[i + 4] << 8 |
		       buffer[i + 5] << 16 |
		       buffer[i + 6] << 24,
		       base + packet_reg);
310 311 312
		packet_reg += 4;
	}

313 314 315
	HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
		   HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
	ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
316 317 318 319 320 321 322
			BIT(packet_id)), 100);
	if (ret)
		DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
}

static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
{
323
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
324
	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
325
	struct drm_connector *connector = &vc4_hdmi->connector;
326
	struct drm_connector_state *cstate = connector->state;
327 328 329 330 331
	struct drm_crtc *crtc = encoder->crtc;
	const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
	union hdmi_infoframe frame;
	int ret;

332
	ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
333
						       connector, mode);
334 335 336 337 338
	if (ret < 0) {
		DRM_ERROR("couldn't fill AVI infoframe\n");
		return;
	}

339
	drm_hdmi_avi_infoframe_quant_range(&frame.avi,
340
					   connector, mode,
341 342
					   vc4_encoder->limited_rgb_range ?
					   HDMI_QUANTIZATION_RANGE_LIMITED :
343
					   HDMI_QUANTIZATION_RANGE_FULL);
344

345
	drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
346

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	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 已提交
366 367
static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
{
368
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
E
Eric Anholt 已提交
369 370
	union hdmi_infoframe frame;

371
	hdmi_audio_infoframe_init(&frame.audio);
E
Eric Anholt 已提交
372 373 374 375

	frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
	frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
	frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
376
	frame.audio.channels = vc4_hdmi->audio.channels;
E
Eric Anholt 已提交
377 378 379 380

	vc4_hdmi_write_infoframe(encoder, &frame);
}

381 382
static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
{
383 384
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);

385 386
	vc4_hdmi_set_avi_infoframe(encoder);
	vc4_hdmi_set_spd_infoframe(encoder);
387 388 389 390 391 392
	/*
	 * 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);
393 394
}

395 396
static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
					       struct drm_atomic_state *state)
397
{
398
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
399

400
	HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
401 402 403 404 405 406

	HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) |
		   VC4_HD_VID_CTL_CLRRGB | VC4_HD_VID_CTL_CLRSYNC);

	HDMI_WRITE(HDMI_VID_CTL,
		   HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
407 408
}

409 410
static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
						 struct drm_atomic_state *state)
411 412
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
413 414
	int ret;

415 416
	if (vc4_hdmi->variant->phy_disable)
		vc4_hdmi->variant->phy_disable(vc4_hdmi);
417

418 419
	HDMI_WRITE(HDMI_VID_CTL,
		   HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
420

421
	clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
422
	clk_disable_unprepare(vc4_hdmi->hsm_clock);
423
	clk_disable_unprepare(vc4_hdmi->pixel_clock);
424

425
	ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
426 427 428 429
	if (ret < 0)
		DRM_ERROR("Failed to release power domain: %d\n", ret);
}

430 431 432 433
static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
{
}

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
{
	u32 csc_ctl;

	csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
				VC4_HD_CSC_CTL_ORDER);

	if (enable) {
		/* 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);
}

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
{
	u32 csc_ctl;

	csc_ctl = 0x07;	/* RGB_CONVERT_MODE = custom matrix, || USE_RGB_TO_YCBCR */

	if (enable) {
		/* 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.8594 0      0      16]
		 * [ 0      0.8594 0      16]
		 * [ 0      0      0.8594 16]
		 * [ 0      0      0       1]
		 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
		 */
		HDMI_WRITE(HDMI_CSC_12_11, (0x0000 << 16) | 0x1b80);
		HDMI_WRITE(HDMI_CSC_14_13, (0x0400 << 16) | 0x0000);
		HDMI_WRITE(HDMI_CSC_22_21, (0x1b80 << 16) | 0x0000);
		HDMI_WRITE(HDMI_CSC_24_23, (0x0400 << 16) | 0x0000);
		HDMI_WRITE(HDMI_CSC_32_31, (0x0000 << 16) | 0x0000);
		HDMI_WRITE(HDMI_CSC_34_33, (0x0400 << 16) | 0x1b80);
	} else {
		/* Still use the matrix for full range, but make it unity.
		 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
		 */
		HDMI_WRITE(HDMI_CSC_12_11, (0x0000 << 16) | 0x2000);
		HDMI_WRITE(HDMI_CSC_14_13, (0x0000 << 16) | 0x0000);
		HDMI_WRITE(HDMI_CSC_22_21, (0x2000 << 16) | 0x0000);
		HDMI_WRITE(HDMI_CSC_24_23, (0x0000 << 16) | 0x0000);
		HDMI_WRITE(HDMI_CSC_32_31, (0x0000 << 16) | 0x0000);
		HDMI_WRITE(HDMI_CSC_34_33, (0x0000 << 16) | 0x2000);
	}

	HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
}

508 509
static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
				 struct drm_display_mode *mode)
510 511 512
{
	bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
	bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
513
	bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
514
	u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
515
	u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
516
				   VC4_HDMI_VERTA_VSP) |
517
		     VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
518
				   VC4_HDMI_VERTA_VFP) |
519
		     VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
520
	u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
521
		     VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
522
				   VC4_HDMI_VERTB_VBP));
523 524 525 526 527
	u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
			  VC4_SET_FIELD(mode->crtc_vtotal -
					mode->crtc_vsync_end -
					interlaced,
					VC4_HDMI_VERTB_VBP));
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

	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);
}
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
				 struct drm_display_mode *mode)
{
	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));
	u32 vertb = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
		     VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
				   VC4_HDMI_VERTB_VBP));
	u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
			  VC4_SET_FIELD(mode->crtc_vtotal -
					mode->crtc_vsync_end -
					interlaced,
					VC4_HDMI_VERTB_VBP));

	HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, 0x354021);
	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);

	HDMI_WRITE(HDMI_CLOCK_STOP, 0);
}
599

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
{
	u32 drift;
	int ret;

	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);
	usleep_range(1000, 1100);
	HDMI_WRITE(HDMI_FIFO_CTL,
		   drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
	HDMI_WRITE(HDMI_FIFO_CTL,
		   drift | VC4_HDMI_FIFO_CTL_RECENTER);

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

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
static struct drm_connector_state *
vc4_hdmi_encoder_get_connector_state(struct drm_encoder *encoder,
				     struct drm_atomic_state *state)
{
	struct drm_connector_state *conn_state;
	struct drm_connector *connector;
	unsigned int i;

	for_each_new_connector_in_state(state, connector, conn_state, i) {
		if (conn_state->best_encoder == encoder)
			return conn_state;
	}

	return NULL;
}

640 641
static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
						struct drm_atomic_state *state)
642
{
643 644 645 646
	struct drm_connector_state *conn_state =
		vc4_hdmi_encoder_get_connector_state(encoder, state);
	struct vc4_hdmi_connector_state *vc4_conn_state =
		conn_state_to_vc4_hdmi_conn_state(conn_state);
647 648
	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
649
	unsigned long pixel_rate, hsm_rate;
650 651
	int ret;

652
	ret = pm_runtime_get_sync(&vc4_hdmi->pdev->dev);
653 654 655 656 657
	if (ret < 0) {
		DRM_ERROR("Failed to retain power domain: %d\n", ret);
		return;
	}

658
	pixel_rate = vc4_conn_state->pixel_rate;
659
	ret = clk_set_rate(vc4_hdmi->pixel_clock, pixel_rate);
660 661 662 663 664
	if (ret) {
		DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
		return;
	}

665
	ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
666 667 668 669 670
	if (ret) {
		DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
		return;
	}

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
	/*
	 * 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.
686
	 */
687
	hsm_rate = max_t(unsigned long, 120000000, (pixel_rate / 100) * 101);
688
	ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
689 690 691 692
	if (ret) {
		DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
		return;
	}
693

694 695 696 697 698 699
	ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
	if (ret) {
		DRM_ERROR("Failed to turn on HSM clock: %d\n", ret);
		clk_disable_unprepare(vc4_hdmi->pixel_clock);
		return;
	}
700

701 702 703 704 705 706 707 708 709 710 711 712
	/*
	 * FIXME: When the pixel freq is 594MHz (4k60), this needs to be setup
	 * at 300MHz.
	 */
	ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock,
			       (hsm_rate > VC4_HSM_MID_CLOCK ? 150000000 : 75000000));
	if (ret) {
		DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
		clk_disable_unprepare(vc4_hdmi->hsm_clock);
		clk_disable_unprepare(vc4_hdmi->pixel_clock);
		return;
	}
713

714 715 716 717 718 719
	ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
	if (ret) {
		DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
		clk_disable_unprepare(vc4_hdmi->hsm_clock);
		clk_disable_unprepare(vc4_hdmi->pixel_clock);
		return;
720 721
	}

722 723
	if (vc4_hdmi->variant->reset)
		vc4_hdmi->variant->reset(vc4_hdmi);
724

725
	if (vc4_hdmi->variant->phy_init)
726
		vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
727

728 729
	HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
		   HDMI_READ(HDMI_SCHEDULER_CONTROL) |
730 731 732
		   VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
		   VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);

733 734
	if (vc4_hdmi->variant->set_timings)
		vc4_hdmi->variant->set_timings(vc4_hdmi, mode);
735
}
736

737 738
static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
					     struct drm_atomic_state *state)
739 740 741 742
{
	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
743

744
	if (vc4_encoder->hdmi_monitor &&
745 746 747
	    drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED) {
		if (vc4_hdmi->variant->csc_setup)
			vc4_hdmi->variant->csc_setup(vc4_hdmi, true);
748

749 750
		vc4_encoder->limited_rgb_range = true;
	} else {
751 752 753
		if (vc4_hdmi->variant->csc_setup)
			vc4_hdmi->variant->csc_setup(vc4_hdmi, false);

754
		vc4_encoder->limited_rgb_range = false;
755 756
	}

757
	HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
758
}
759

760 761
static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
					      struct drm_atomic_state *state)
762
{
763
	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
764 765
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
766 767
	bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
	bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
768
	int ret;
769

770 771 772
	HDMI_WRITE(HDMI_VID_CTL,
		   VC4_HD_VID_CTL_ENABLE |
		   VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
773 774 775
		   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));
776

777 778
	HDMI_WRITE(HDMI_VID_CTL,
		   HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
779 780

	if (vc4_encoder->hdmi_monitor) {
781 782
		HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(HDMI_SCHEDULER_CONTROL) |
783 784
			   VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);

785
		ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
786
			       VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
787 788 789
		WARN_ONCE(ret, "Timeout waiting for "
			  "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
	} else {
790 791
		HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
			   HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
792
			   ~(VC4_HDMI_RAM_PACKET_ENABLE));
793 794
		HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(HDMI_SCHEDULER_CONTROL) &
795 796
			   ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);

797
		ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
798
				 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
799 800 801 802 803
		WARN_ONCE(ret, "Timeout waiting for "
			  "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
	}

	if (vc4_encoder->hdmi_monitor) {
804
		WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
805
			  VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
806 807
		HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(HDMI_SCHEDULER_CONTROL) |
808 809
			   VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);

810
		HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
811 812 813
			   VC4_HDMI_RAM_PACKET_ENABLE);

		vc4_hdmi_set_infoframes(encoder);
814
	}
815 816

	vc4_hdmi_recenter_fifo(vc4_hdmi);
817 818
}

819 820
static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
{
821 822
}

823 824 825
#define WIFI_2_4GHz_CH1_MIN_FREQ	2400000000ULL
#define WIFI_2_4GHz_CH1_MAX_FREQ	2422000000ULL

826 827 828 829
static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
					 struct drm_crtc_state *crtc_state,
					 struct drm_connector_state *conn_state)
{
830
	struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
831 832 833
	struct drm_display_mode *mode = &crtc_state->adjusted_mode;
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
	unsigned long long pixel_rate = mode->clock * 1000;
834
	unsigned long long tmds_rate;
835

836 837 838 839 840
	if (vc4_hdmi->variant->unsupported_odd_h_timings &&
	    ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
	     (mode->hsync_end % 2) || (mode->htotal % 2)))
		return -EINVAL;

841 842 843 844 845 846 847 848 849 850 851 852 853 854
	/*
	 * 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.
	 */
	tmds_rate = pixel_rate * 10;
	if (vc4_hdmi->disable_wifi_frequencies &&
	    (tmds_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
	     tmds_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
		mode->clock = 238560;
		pixel_rate = mode->clock * 1000;
	}

855 856 857
	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
		pixel_rate = pixel_rate * 2;

858 859 860
	if (pixel_rate > vc4_hdmi->variant->max_pixel_clock)
		return -EINVAL;

861 862
	vc4_state->pixel_rate = pixel_rate;

863 864 865
	return 0;
}

866
static enum drm_mode_status
867
vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
868 869
			    const struct drm_display_mode *mode)
{
870 871
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);

872 873 874 875 876
	if (vc4_hdmi->variant->unsupported_odd_h_timings &&
	    ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
	     (mode->hsync_end % 2) || (mode->htotal % 2)))
		return MODE_H_ILLEGAL;

877
	if ((mode->clock * 1000) > vc4_hdmi->variant->max_pixel_clock)
878 879 880 881 882
		return MODE_CLOCK_HIGH;

	return MODE_OK;
}

883
static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
884
	.atomic_check = vc4_hdmi_encoder_atomic_check,
885
	.mode_valid = vc4_hdmi_encoder_mode_valid,
886 887 888 889
	.disable = vc4_hdmi_encoder_disable,
	.enable = vc4_hdmi_encoder_enable,
};

890 891 892 893 894 895 896 897 898 899 900 901
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;
}

902 903 904 905 906 907 908 909 910 911 912 913
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;
}

E
Eric Anholt 已提交
914
/* HDMI audio codec callbacks */
915
static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
916
{
917
	u32 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
E
Eric Anholt 已提交
918 919
	unsigned long n, m;

920
	rational_best_approximation(hsm_clock, vc4_hdmi->audio.samplerate,
E
Eric Anholt 已提交
921 922 923 924 925 926
				    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);

927 928 929
	HDMI_WRITE(HDMI_MAI_SMP,
		   VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
		   VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
E
Eric Anholt 已提交
930 931
}

932
static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
933
{
934
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
E
Eric Anholt 已提交
935 936
	struct drm_crtc *crtc = encoder->crtc;
	const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
937
	u32 samplerate = vc4_hdmi->audio.samplerate;
E
Eric Anholt 已提交
938 939 940 941 942 943 944 945
	u32 n, cts;
	u64 tmp;

	n = 128 * samplerate / 1000;
	tmp = (u64)(mode->clock * 1000) * n;
	do_div(tmp, 128 * samplerate);
	cts = tmp;

946
	HDMI_WRITE(HDMI_CRP_CFG,
E
Eric Anholt 已提交
947 948 949 950 951 952 953 954
		   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
	 */
955 956
	HDMI_WRITE(HDMI_CTS_0, cts);
	HDMI_WRITE(HDMI_CTS_1, cts);
E
Eric Anholt 已提交
957 958 959 960 961 962 963 964 965 966 967 968
}

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

static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
				  struct snd_soc_dai *dai)
{
969 970
	struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
971
	struct drm_connector *connector = &vc4_hdmi->connector;
E
Eric Anholt 已提交
972 973
	int ret;

974
	if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream)
E
Eric Anholt 已提交
975 976
		return -EINVAL;

977
	vc4_hdmi->audio.substream = substream;
E
Eric Anholt 已提交
978 979 980 981 982

	/*
	 * If the HDMI encoder hasn't probed, or the encoder is
	 * currently in DVI mode, treat the codec dai as missing.
	 */
983
	if (!encoder->crtc || !(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
E
Eric Anholt 已提交
984 985 986
				VC4_HDMI_RAM_PACKET_ENABLE))
		return -ENODEV;

987
	ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld);
E
Eric Anholt 已提交
988 989 990 991 992 993 994 995 996 997 998
	if (ret)
		return ret;

	return 0;
}

static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
	return 0;
}

999
static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
1000
{
1001 1002
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
	struct device *dev = &vc4_hdmi->pdev->dev;
E
Eric Anholt 已提交
1003 1004
	int ret;

1005
	vc4_hdmi->audio.streaming = false;
1006
	ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
E
Eric Anholt 已提交
1007 1008 1009
	if (ret)
		dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);

1010 1011 1012
	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);
E
Eric Anholt 已提交
1013 1014 1015 1016 1017
}

static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
				    struct snd_soc_dai *dai)
{
1018
	struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
E
Eric Anholt 已提交
1019

1020
	if (substream != vc4_hdmi->audio.substream)
E
Eric Anholt 已提交
1021 1022
		return;

1023
	vc4_hdmi_audio_reset(vc4_hdmi);
E
Eric Anholt 已提交
1024

1025
	vc4_hdmi->audio.substream = NULL;
E
Eric Anholt 已提交
1026 1027 1028 1029 1030 1031 1032
}

/* HDMI audio codec callbacks */
static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream,
				    struct snd_pcm_hw_params *params,
				    struct snd_soc_dai *dai)
{
1033
	struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
1034
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
1035
	struct device *dev = &vc4_hdmi->pdev->dev;
E
Eric Anholt 已提交
1036
	u32 audio_packet_config, channel_mask;
1037
	u32 channel_map;
E
Eric Anholt 已提交
1038

1039
	if (substream != vc4_hdmi->audio.substream)
E
Eric Anholt 已提交
1040 1041 1042 1043 1044 1045
		return -EINVAL;

	dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
		params_rate(params), params_width(params),
		params_channels(params));

1046 1047
	vc4_hdmi->audio.channels = params_channels(params);
	vc4_hdmi->audio.samplerate = params_rate(params);
E
Eric Anholt 已提交
1048

1049 1050 1051 1052 1053 1054
	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);
E
Eric Anholt 已提交
1055

1056
	vc4_hdmi_audio_set_mai_clock(vc4_hdmi);
E
Eric Anholt 已提交
1057

1058
	/* The B frame identifier should match the value used by alsa-lib (8) */
E
Eric Anholt 已提交
1059 1060 1061
	audio_packet_config =
		VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
		VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
1062
		VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
E
Eric Anholt 已提交
1063

1064
	channel_mask = GENMASK(vc4_hdmi->audio.channels - 1, 0);
E
Eric Anholt 已提交
1065 1066 1067 1068
	audio_packet_config |= VC4_SET_FIELD(channel_mask,
					     VC4_HDMI_AUDIO_PACKET_CEA_MASK);

	/* Set the MAI threshold.  This logic mimics the firmware's. */
1069
	if (vc4_hdmi->audio.samplerate > 96000) {
1070 1071 1072
		HDMI_WRITE(HDMI_MAI_THR,
			   VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) |
			   VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
1073
	} else if (vc4_hdmi->audio.samplerate > 48000) {
1074 1075 1076
		HDMI_WRITE(HDMI_MAI_THR,
			   VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) |
			   VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
E
Eric Anholt 已提交
1077
	} else {
1078 1079 1080 1081 1082
		HDMI_WRITE(HDMI_MAI_THR,
			   VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
			   VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
			   VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
			   VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
E
Eric Anholt 已提交
1083 1084
	}

1085
	HDMI_WRITE(HDMI_MAI_CONFIG,
E
Eric Anholt 已提交
1086 1087 1088
		   VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
		   VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));

1089
	channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
1090 1091
	HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
	HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
1092
	vc4_hdmi_set_n_cts(vc4_hdmi);
E
Eric Anholt 已提交
1093

1094 1095
	vc4_hdmi_set_audio_infoframe(encoder);

E
Eric Anholt 已提交
1096 1097 1098 1099 1100 1101
	return 0;
}

static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
				  struct snd_soc_dai *dai)
{
1102
	struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
E
Eric Anholt 已提交
1103 1104 1105

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
1106
		vc4_hdmi->audio.streaming = true;
1107 1108 1109

		if (vc4_hdmi->variant->phy_rng_enable)
			vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
1110 1111 1112 1113 1114

		HDMI_WRITE(HDMI_MAI_CTL,
			   VC4_SET_FIELD(vc4_hdmi->audio.channels,
					 VC4_HD_MAI_CTL_CHNUM) |
			   VC4_HD_MAI_CTL_ENABLE);
E
Eric Anholt 已提交
1115 1116
		break;
	case SNDRV_PCM_TRIGGER_STOP:
1117 1118 1119 1120
		HDMI_WRITE(HDMI_MAI_CTL,
			   VC4_HD_MAI_CTL_DLATE |
			   VC4_HD_MAI_CTL_ERRORE |
			   VC4_HD_MAI_CTL_ERRORF);
1121 1122 1123 1124

		if (vc4_hdmi->variant->phy_rng_disable)
			vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);

1125 1126
		vc4_hdmi->audio.streaming = false;

E
Eric Anholt 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
		break;
	default:
		break;
	}

	return 0;
}

static inline struct vc4_hdmi *
snd_component_to_hdmi(struct snd_soc_component *component)
{
	struct snd_soc_card *card = snd_soc_component_get_drvdata(component);

	return snd_soc_card_get_drvdata(card);
}

static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
{
	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1147
	struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
1148
	struct drm_connector *connector = &vc4_hdmi->connector;
E
Eric Anholt 已提交
1149 1150

	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1151
	uinfo->count = sizeof(connector->eld);
E
Eric Anholt 已提交
1152 1153 1154 1155 1156 1157 1158 1159

	return 0;
}

static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1160
	struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
1161
	struct drm_connector *connector = &vc4_hdmi->connector;
E
Eric Anholt 已提交
1162

1163 1164
	memcpy(ucontrol->value.bytes.data, connector->eld,
	       sizeof(connector->eld));
E
Eric Anholt 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187

	return 0;
}

static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
	{
		.access = SNDRV_CTL_ELEM_ACCESS_READ |
			  SNDRV_CTL_ELEM_ACCESS_VOLATILE,
		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
		.name = "ELD",
		.info = vc4_hdmi_audio_eld_ctl_info,
		.get = vc4_hdmi_audio_eld_ctl_get,
	},
};

static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
	SND_SOC_DAPM_OUTPUT("TX"),
};

static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
	{ "TX", NULL, "Playback" },
};

1188
static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = {
1189
	.name			= "vc4-hdmi-codec-dai-component",
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	.controls		= vc4_hdmi_audio_controls,
	.num_controls		= ARRAY_SIZE(vc4_hdmi_audio_controls),
	.dapm_widgets		= vc4_hdmi_audio_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(vc4_hdmi_audio_widgets),
	.dapm_routes		= vc4_hdmi_audio_routes,
	.num_dapm_routes	= ARRAY_SIZE(vc4_hdmi_audio_routes),
	.idle_bias_on		= 1,
	.use_pmdown_time	= 1,
	.endianness		= 1,
	.non_legacy_dai_naming	= 1,
E
Eric Anholt 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
};

static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
	.startup = vc4_hdmi_audio_startup,
	.shutdown = vc4_hdmi_audio_shutdown,
	.hw_params = vc4_hdmi_audio_hw_params,
	.set_fmt = vc4_hdmi_audio_set_fmt,
	.trigger = vc4_hdmi_audio_trigger,
};

static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
	.name = "vc4-hdmi-hifi",
	.playback = {
		.stream_name = "Playback",
		.channels_min = 2,
		.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_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)
{
1230
	struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
E
Eric Anholt 已提交
1231

1232
	snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
E
Eric Anholt 已提交
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

	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,
	},
	.ops = &vc4_hdmi_audio_dai_ops,
};

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

1258
static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
1259
{
1260 1261
	const struct vc4_hdmi_register *mai_data =
		&vc4_hdmi->variant->registers[HDMI_MAI_DATA];
1262 1263 1264
	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;
E
Eric Anholt 已提交
1265
	const __be32 *addr;
1266
	int index;
E
Eric Anholt 已提交
1267 1268 1269 1270 1271 1272 1273 1274
	int ret;

	if (!of_find_property(dev->of_node, "dmas", NULL)) {
		dev_warn(dev,
			 "'dmas' DT property is missing, no HDMI audio\n");
		return 0;
	}

1275 1276 1277 1278 1279
	if (mai_data->reg != VC4_HD) {
		WARN_ONCE(true, "MAI isn't in the HD block\n");
		return -EINVAL;
	}

E
Eric Anholt 已提交
1280 1281 1282 1283 1284 1285 1286
	/*
	 * 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.
	 */
1287 1288 1289 1290 1291 1292 1293
	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);

1294
	vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
1295 1296
	vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
	vc4_hdmi->audio.dma_data.maxburst = 2;
E
Eric Anholt 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

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

1311 1312
	/* register component and codec dai */
	ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv,
E
Eric Anholt 已提交
1313 1314
				     &vc4_hdmi_audio_codec_dai_drv, 1);
	if (ret) {
1315
		dev_err(dev, "Could not register component: %d\n", ret);
E
Eric Anholt 已提交
1316 1317 1318
		return ret;
	}

1319 1320 1321
	dai_link->cpus		= &vc4_hdmi->audio.cpu;
	dai_link->codecs	= &vc4_hdmi->audio.codec;
	dai_link->platforms	= &vc4_hdmi->audio.platform;
1322 1323 1324

	dai_link->num_cpus	= 1;
	dai_link->num_codecs	= 1;
1325
	dai_link->num_platforms	= 1;
1326

E
Eric Anholt 已提交
1327 1328
	dai_link->name = "MAI";
	dai_link->stream_name = "MAI PCM";
1329 1330 1331
	dai_link->codecs->dai_name = vc4_hdmi_audio_codec_dai_drv.name;
	dai_link->cpus->dai_name = dev_name(dev);
	dai_link->codecs->name = dev_name(dev);
1332
	dai_link->platforms->name = dev_name(dev);
E
Eric Anholt 已提交
1333 1334 1335

	card->dai_link = dai_link;
	card->num_links = 1;
1336
	card->name = vc4_hdmi->variant->card_name;
E
Eric Anholt 已提交
1337
	card->dev = dev;
1338
	card->owner = THIS_MODULE;
E
Eric Anholt 已提交
1339 1340 1341 1342 1343 1344 1345 1346

	/*
	 * 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.
	 */
1347
	snd_soc_card_set_drvdata(card, vc4_hdmi);
E
Eric Anholt 已提交
1348
	ret = devm_snd_soc_register_card(dev, card);
1349
	if (ret)
E
Eric Anholt 已提交
1350 1351 1352 1353 1354 1355
		dev_err(dev, "Could not register sound card: %d\n", ret);

	return ret;

}

H
Hans Verkuil 已提交
1356 1357 1358
#ifdef CONFIG_DRM_VC4_HDMI_CEC
static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
{
1359
	struct vc4_hdmi *vc4_hdmi = priv;
1360 1361 1362 1363 1364 1365 1366

	if (vc4_hdmi->cec_irq_was_rx) {
		if (vc4_hdmi->cec_rx_msg.len)
			cec_received_msg(vc4_hdmi->cec_adap,
					 &vc4_hdmi->cec_rx_msg);
	} else if (vc4_hdmi->cec_tx_ok) {
		cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
H
Hans Verkuil 已提交
1367 1368 1369 1370 1371 1372
				  0, 0, 0, 0);
	} else {
		/*
		 * This CEC implementation makes 1 retry, so if we
		 * get a NACK, then that means it made 2 attempts.
		 */
1373
		cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
H
Hans Verkuil 已提交
1374 1375 1376 1377 1378
				  0, 2, 0, 0);
	}
	return IRQ_HANDLED;
}

1379
static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
H
Hans Verkuil 已提交
1380
{
1381
	struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
H
Hans Verkuil 已提交
1382 1383 1384 1385 1386
	unsigned int i;

	msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
					VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
	for (i = 0; i < msg->len; i += 4) {
1387
		u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + i);
H
Hans Verkuil 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397

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

static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
{
1398
	struct vc4_hdmi *vc4_hdmi = priv;
1399
	u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
H
Hans Verkuil 已提交
1400 1401 1402 1403
	u32 cntrl1, cntrl5;

	if (!(stat & VC4_HDMI_CPU_CEC))
		return IRQ_NONE;
1404
	vc4_hdmi->cec_rx_msg.len = 0;
1405 1406
	cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
	cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
1407 1408
	vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
	if (vc4_hdmi->cec_irq_was_rx) {
1409
		vc4_cec_read_msg(vc4_hdmi, cntrl1);
H
Hans Verkuil 已提交
1410
		cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1411
		HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
H
Hans Verkuil 已提交
1412 1413
		cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
	} else {
1414
		vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
H
Hans Verkuil 已提交
1415 1416
		cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
	}
1417 1418
	HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
	HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
H
Hans Verkuil 已提交
1419 1420 1421 1422 1423 1424

	return IRQ_WAKE_THREAD;
}

static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
{
1425
	struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
H
Hans Verkuil 已提交
1426 1427
	/* clock period in microseconds */
	const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
1428
	u32 val = HDMI_READ(HDMI_CEC_CNTRL_5);
H
Hans Verkuil 已提交
1429 1430 1431 1432 1433 1434 1435 1436

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

	if (enable) {
1437
		HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
H
Hans Verkuil 已提交
1438
			   VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
		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));

		HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
H
Hans Verkuil 已提交
1458
	} else {
1459 1460
		HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
		HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
H
Hans Verkuil 已提交
1461 1462 1463 1464 1465 1466 1467
			   VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
	}
	return 0;
}

static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
{
1468
	struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
H
Hans Verkuil 已提交
1469

1470 1471
	HDMI_WRITE(HDMI_CEC_CNTRL_1,
		   (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
H
Hans Verkuil 已提交
1472 1473 1474 1475 1476 1477 1478
		   (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
	return 0;
}

static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
				      u32 signal_free_time, struct cec_msg *msg)
{
1479
	struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
H
Hans Verkuil 已提交
1480 1481 1482 1483
	u32 val;
	unsigned int i;

	for (i = 0; i < msg->len; i += 4)
1484
		HDMI_WRITE(HDMI_CEC_TX_DATA_1 + i,
H
Hans Verkuil 已提交
1485 1486 1487 1488 1489
			   (msg->msg[i]) |
			   (msg->msg[i + 1] << 8) |
			   (msg->msg[i + 2] << 16) |
			   (msg->msg[i + 3] << 24));

1490
	val = HDMI_READ(HDMI_CEC_CNTRL_1);
H
Hans Verkuil 已提交
1491
	val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1492
	HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
H
Hans Verkuil 已提交
1493 1494 1495 1496
	val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
	val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
	val |= VC4_HDMI_CEC_START_XMIT_BEGIN;

1497
	HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
H
Hans Verkuil 已提交
1498 1499 1500 1501 1502 1503 1504 1505 1506
	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,
};

1507
static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
1508
{
1509
	struct cec_connector_info conn_info;
1510
	struct platform_device *pdev = vc4_hdmi->pdev;
1511 1512 1513
	u32 value;
	int ret;

1514 1515 1516
	if (!vc4_hdmi->variant->cec_available)
		return 0;

1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 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
	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);

	HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
	value = HDMI_READ(HDMI_CEC_CNTRL_1);
	value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
	/*
	 * Set the logical address to Unregistered and set the clock
	 * divider: the hsm_clock rate and this divider setting will
	 * give a 40 kHz CEC clock.
	 */
	value |= VC4_HDMI_CEC_ADDR_MASK |
		 (4091 << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT);
	HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
	ret = devm_request_threaded_irq(&pdev->dev, platform_get_irq(pdev, 0),
					vc4_cec_irq_handler,
					vc4_cec_irq_handler_thread, 0,
					"vc4 hdmi cec", vc4_hdmi);
	if (ret)
		goto err_delete_cec_adap;

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

	return 0;

err_delete_cec_adap:
	cec_delete_adapter(vc4_hdmi->cec_adap);

	return ret;
}

static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi)
{
	cec_unregister_adapter(vc4_hdmi->cec_adap);
}
#else
static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
{
	return 0;
}

static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {};

H
Hans Verkuil 已提交
1570 1571
#endif

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi,
				 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;

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

1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
	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)
1599
		return -ENOMEM;
1600 1601 1602 1603 1604 1605 1606 1607

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

	return 0;
}

1608
static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
1609
{
1610 1611
	struct platform_device *pdev = vc4_hdmi->pdev;
	struct device *dev = &pdev->dev;
1612 1613
	int ret;

1614 1615 1616
	vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
	if (IS_ERR(vc4_hdmi->hdmicore_regs))
		return PTR_ERR(vc4_hdmi->hdmicore_regs);
1617

1618 1619 1620
	vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
	if (IS_ERR(vc4_hdmi->hd_regs))
		return PTR_ERR(vc4_hdmi->hd_regs);
1621

1622 1623 1624
	ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
	if (ret)
		return ret;
1625

1626 1627 1628
	ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
	if (ret)
		return ret;
1629

1630 1631 1632
	vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
	if (IS_ERR(vc4_hdmi->pixel_clock)) {
		ret = PTR_ERR(vc4_hdmi->pixel_clock);
1633 1634 1635
		if (ret != -EPROBE_DEFER)
			DRM_ERROR("Failed to get pixel clock\n");
		return ret;
1636
	}
1637

1638 1639
	vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
	if (IS_ERR(vc4_hdmi->hsm_clock)) {
1640
		DRM_ERROR("Failed to get HDMI state machine clock\n");
1641
		return PTR_ERR(vc4_hdmi->hsm_clock);
1642
	}
1643
	vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
1644

1645 1646 1647
	return 0;
}

1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
static int vc5_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
{
	struct platform_device *pdev = vc4_hdmi->pdev;
	struct device *dev = &pdev->dev;
	struct resource *res;

	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));
1660 1661
	if (!vc4_hdmi->hdmicore_regs)
		return -ENOMEM;
1662 1663 1664 1665 1666 1667

	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));
1668 1669
	if (!vc4_hdmi->hd_regs)
		return -ENOMEM;
1670 1671 1672 1673 1674 1675

	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));
1676 1677
	if (!vc4_hdmi->cec_regs)
		return -ENOMEM;
1678 1679 1680 1681 1682 1683

	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));
1684 1685
	if (!vc4_hdmi->csc_regs)
		return -ENOMEM;
1686 1687 1688 1689 1690 1691

	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));
1692 1693
	if (!vc4_hdmi->dvp_regs)
		return -ENOMEM;
1694 1695 1696 1697 1698 1699

	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));
1700 1701
	if (!vc4_hdmi->phy_regs)
		return -ENOMEM;
1702 1703 1704 1705 1706 1707

	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));
1708 1709
	if (!vc4_hdmi->ram_regs)
		return -ENOMEM;
1710 1711 1712 1713 1714 1715

	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));
1716 1717
	if (!vc4_hdmi->rm_regs)
		return -ENOMEM;
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745

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

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

	return 0;
}

1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
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;
	u32 value;
	int ret;

	vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
	if (!vc4_hdmi)
		return -ENOMEM;

	dev_set_drvdata(dev, vc4_hdmi);
	encoder = &vc4_hdmi->encoder.base.base;
1763
	vc4_hdmi->encoder.base.type = variant->encoder_type;
1764 1765 1766 1767 1768
	vc4_hdmi->encoder.base.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
	vc4_hdmi->encoder.base.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
	vc4_hdmi->encoder.base.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
	vc4_hdmi->encoder.base.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
	vc4_hdmi->encoder.base.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
1769 1770 1771 1772 1773 1774
	vc4_hdmi->pdev = pdev;
	vc4_hdmi->variant = variant;

	ret = variant->init_resources(vc4_hdmi);
	if (ret)
		return ret;
1775

1776 1777 1778 1779 1780 1781
	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;
	}

1782
	vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
1783
	of_node_put(ddc_node);
1784
	if (!vc4_hdmi->ddc) {
1785 1786 1787 1788 1789 1790 1791 1792
		DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
		return -EPROBE_DEFER;
	}

	/* Only use the GPIO HPD pin if present in the DT, otherwise
	 * we'll use the HDMI core's register.
	 */
	if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
1793 1794
		enum of_gpio_flags hpd_gpio_flags;

1795 1796 1797 1798 1799
		vc4_hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
							     "hpd-gpios", 0,
							     &hpd_gpio_flags);
		if (vc4_hdmi->hpd_gpio < 0) {
			ret = vc4_hdmi->hpd_gpio;
H
Hans Verkuil 已提交
1800
			goto err_unprepare_hsm;
1801
		}
1802

1803
		vc4_hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
1804 1805
	}

1806 1807 1808
	vc4_hdmi->disable_wifi_frequencies =
		of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");

1809
	pm_runtime_enable(dev);
1810

1811 1812
	drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
	drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
1813

1814
	ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
1815
	if (ret)
1816
		goto err_destroy_encoder;
1817

1818
	ret = vc4_hdmi_cec_init(vc4_hdmi);
H
Hans Verkuil 已提交
1819
	if (ret)
1820
		goto err_destroy_conn;
1821

1822
	ret = vc4_hdmi_audio_init(vc4_hdmi);
E
Eric Anholt 已提交
1823
	if (ret)
1824
		goto err_free_cec;
E
Eric Anholt 已提交
1825

1826 1827 1828
	vc4_debugfs_add_file(drm, variant->debugfs_name,
			     vc4_hdmi_debugfs_regs,
			     vc4_hdmi);
1829

1830 1831
	return 0;

1832 1833
err_free_cec:
	vc4_hdmi_cec_exit(vc4_hdmi);
H
Hans Verkuil 已提交
1834
err_destroy_conn:
1835
	vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
1836
err_destroy_encoder:
1837
	drm_encoder_cleanup(encoder);
H
Hans Verkuil 已提交
1838
err_unprepare_hsm:
1839
	pm_runtime_disable(dev);
1840
	put_device(&vc4_hdmi->ddc->dev);
1841 1842 1843 1844 1845 1846 1847

	return ret;
}

static void vc4_hdmi_unbind(struct device *dev, struct device *master,
			    void *data)
{
1848
	struct vc4_hdmi *vc4_hdmi;
1849

1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
	/*
	 * 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);
	vc4_hdmi = dev_get_drvdata(dev);
1870

1871 1872
	kfree(vc4_hdmi->hdmi_regset.regs);
	kfree(vc4_hdmi->hd_regset.regs);
1873

1874
	vc4_hdmi_cec_exit(vc4_hdmi);
1875
	vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
1876
	drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
1877

1878
	pm_runtime_disable(dev);
1879

1880
	put_device(&vc4_hdmi->ddc->dev);
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
}

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

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

1899
static const struct vc4_hdmi_variant bcm2835_variant = {
1900
	.encoder_type		= VC4_ENCODER_TYPE_HDMI0,
1901
	.debugfs_name		= "hdmi_regs",
1902
	.card_name		= "vc4-hdmi",
1903
	.max_pixel_clock	= 162000000,
1904
	.cec_available		= true,
1905 1906 1907
	.registers		= vc4_hdmi_fields,
	.num_registers		= ARRAY_SIZE(vc4_hdmi_fields),

1908
	.init_resources		= vc4_hdmi_init_resources,
1909
	.csc_setup		= vc4_hdmi_csc_setup,
1910
	.reset			= vc4_hdmi_reset,
1911
	.set_timings		= vc4_hdmi_set_timings,
1912 1913
	.phy_init		= vc4_hdmi_phy_init,
	.phy_disable		= vc4_hdmi_phy_disable,
1914 1915
	.phy_rng_enable		= vc4_hdmi_phy_rng_enable,
	.phy_rng_disable	= vc4_hdmi_phy_rng_disable,
1916
	.channel_map		= vc4_hdmi_channel_map,
1917 1918
};

1919 1920 1921 1922
static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
	.encoder_type		= VC4_ENCODER_TYPE_HDMI0,
	.debugfs_name		= "hdmi0_regs",
	.card_name		= "vc4-hdmi-0",
1923
	.max_pixel_clock	= HDMI_14_MAX_TMDS_CLK,
1924 1925 1926 1927 1928 1929 1930 1931
	.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,
	},
1932
	.unsupported_odd_h_timings	= true,
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948

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

static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
	.encoder_type		= VC4_ENCODER_TYPE_HDMI1,
	.debugfs_name		= "hdmi1_regs",
	.card_name		= "vc4-hdmi-1",
1949
	.max_pixel_clock	= HDMI_14_MAX_TMDS_CLK,
1950 1951 1952 1953 1954 1955 1956 1957
	.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,
	},
1958
	.unsupported_odd_h_timings	= true,
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970

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

1971
static const struct of_device_id vc4_hdmi_dt_match[] = {
1972
	{ .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
1973 1974
	{ .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
	{ .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
	{}
};

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