vc4_hdmi.c 69.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 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
#include <drm/drm_scdc_helper.h>
39 40 41 42 43 44 45 46
#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>
47
#include <linux/reset.h>
48
#include <sound/dmaengine_pcm.h>
49
#include <sound/hdmi-codec.h>
50 51 52
#include <sound/pcm_drm_eld.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
H
Hans Verkuil 已提交
53
#include "media/cec.h"
54
#include "vc4_drv.h"
55
#include "vc4_hdmi.h"
56
#include "vc4_hdmi_regs.h"
57 58
#include "vc4_regs.h"

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

66 67 68 69
#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)
70

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

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

81 82
#define VC5_HDMI_SCRAMBLER_CTL_ENABLE		BIT(0)

83 84 85 86 87 88 89 90 91 92 93
#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)

94 95
# define VC4_HD_M_SW_RST			BIT(2)
# define VC4_HD_M_ENABLE			BIT(0)
H
Hans Verkuil 已提交
96 97

#define CEC_CLOCK_FREQ 40000
98

99 100
#define HDMI_14_MAX_TMDS_CLK   (340 * 1000 * 1000)

101 102 103 104 105
static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode)
{
	return (mode->clock * 1000) > HDMI_14_MAX_TMDS_CLK;
}

106
static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
107 108
{
	struct drm_info_node *node = (struct drm_info_node *)m->private;
109
	struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
110
	struct drm_printer p = drm_seq_file_printer(m);
111

112 113
	drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
	drm_print_regset32(&p, &vc4_hdmi->hd_regset);
114 115 116 117

	return 0;
}

118
static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
119
{
120 121 122
	HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
	udelay(1);
	HDMI_WRITE(HDMI_M_CTL, 0);
123

124
	HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
125

126 127 128
	HDMI_WRITE(HDMI_SW_RESET_CONTROL,
		   VC4_HDMI_SW_RESET_HDMI |
		   VC4_HDMI_SW_RESET_FORMAT_DETECT);
129

130
	HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
131 132
}

133
static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
134
{
135
	reset_control_reset(vc4_hdmi->reset);
136

137
	HDMI_WRITE(HDMI_DVP_CTL, 0);
138

139 140
	HDMI_WRITE(HDMI_CLOCK_STOP,
		   HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
141 142
}

143 144 145 146 147 148 149 150 151 152 153 154 155
#ifdef CONFIG_DRM_VC4_HDMI_CEC
static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
{
	u16 clk_cnt;
	u32 value;

	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.
	 */
156
	clk_cnt = clk_get_rate(vc4_hdmi->cec_clock) / CEC_CLOCK_FREQ;
157 158 159 160 161 162 163
	value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
	HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
}
#else
static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
#endif

164 165 166
static enum drm_connector_status
vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
{
167
	struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
168
	bool connected = false;
169

170 171
	WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));

M
Maxime Ripard 已提交
172 173 174
	if (vc4_hdmi->hpd_gpio &&
	    gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio)) {
		connected = true;
175 176 177 178
	} else if (drm_probe_ddc(vc4_hdmi->ddc)) {
		connected = true;
	} else if (HDMI_READ(HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED) {
		connected = true;
179 180
	}

181 182 183 184 185 186 187 188 189 190
	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);
				vc4_hdmi->encoder.hdmi_monitor = drm_detect_hdmi_monitor(edid);
				kfree(edid);
			}
		}
191

192
		pm_runtime_put(&vc4_hdmi->pdev->dev);
193
		return connector_status_connected;
194 195
	}

196
	cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
197
	pm_runtime_put(&vc4_hdmi->pdev->dev);
H
Hans Verkuil 已提交
198
	return connector_status_disconnected;
199 200 201 202 203 204 205 206 207 208
}

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)
{
209 210
	struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
	struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
211 212 213
	int ret = 0;
	struct edid *edid;

214 215
	edid = drm_get_edid(connector, vc4_hdmi->ddc);
	cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
216 217 218 219
	if (!edid)
		return -ENODEV;

	vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
220

221
	drm_connector_update_edid_property(connector, edid);
222
	ret = drm_add_edid_modes(connector, edid);
E
Eric Anholt 已提交
223
	kfree(edid);
224

225 226 227 228 229 230 231 232 233 234 235 236
	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) {
			if (vc4_hdmi_mode_needs_scrambling(mode)) {
				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.");
			}
		}
	}

237 238 239
	return ret;
}

240 241 242 243 244 245 246 247 248 249 250 251
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;

252 253
	if (old_state->colorspace != new_state->colorspace ||
	    !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
254 255 256 257 258 259 260 261 262 263 264 265
		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;
}

266 267
static void vc4_hdmi_connector_reset(struct drm_connector *connector)
{
268 269 270 271
	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);
272 273

	if (connector->state)
274 275 276 277 278 279 280 281
		__drm_atomic_helper_connector_destroy_state(connector->state);

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

	if (!new_state)
		return;

282 283
	new_state->base.max_bpc = 8;
	new_state->base.max_requested_bpc = 8;
284 285 286 287 288 289 290 291 292 293 294 295 296 297
	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;

298
	new_state->pixel_rate = vc4_state->pixel_rate;
299 300 301
	__drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);

	return &new_state->base;
302 303
}

304 305
static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
	.detect = vc4_hdmi_connector_detect,
306
	.fill_modes = drm_helper_probe_single_connector_modes,
307
	.destroy = vc4_hdmi_connector_destroy,
308
	.reset = vc4_hdmi_connector_reset,
309
	.atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
310 311 312 313 314
	.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,
315
	.atomic_check = vc4_hdmi_connector_atomic_check,
316 317
};

318
static int vc4_hdmi_connector_init(struct drm_device *dev,
319
				   struct vc4_hdmi *vc4_hdmi)
320
{
321
	struct drm_connector *connector = &vc4_hdmi->connector;
322
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
323
	int ret;
324

325 326 327
	drm_connector_init_with_ddc(dev, connector,
				    &vc4_hdmi_connector_funcs,
				    DRM_MODE_CONNECTOR_HDMIA,
328
				    vc4_hdmi->ddc);
329 330
	drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);

331 332 333 334 335 336 337
	/*
	 * 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);

338 339 340
	/* Create and attach TV margin props to this connector. */
	ret = drm_mode_create_tv_margin_properties(dev);
	if (ret)
341
		return ret;
342

343 344 345 346 347
	ret = drm_mode_create_hdmi_colorspace_property(connector);
	if (ret)
		return ret;

	drm_connector_attach_colorspace_property(connector);
348
	drm_connector_attach_tv_margin_properties(connector);
349
	drm_connector_attach_max_bpc_property(connector, 8, 12);
350

351 352 353
	connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
			     DRM_CONNECTOR_POLL_DISCONNECT);

354
	connector->interlace_allowed = 1;
355 356
	connector->doublescan_allowed = 0;

357 358 359
	if (vc4_hdmi->variant->supports_hdr)
		drm_connector_attach_hdr_output_metadata_property(connector);

360
	drm_connector_attach_encoder(connector, encoder);
361

362
	return 0;
363 364
}

365
static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
366 367
				enum hdmi_infoframe_type type,
				bool poll)
368
{
369
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
370 371
	u32 packet_id = type - 0x80;

372 373
	HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
		   HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
374

375 376 377
	if (!poll)
		return 0;

378
	return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
379 380 381 382 383 384
			  BIT(packet_id)), 100);
}

static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
				     union hdmi_infoframe *frame)
{
385
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
386
	u32 packet_id = frame->any.type - 0x80;
387 388 389 390 391
	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);
392 393 394 395
	uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
	ssize_t len, i;
	int ret;

396
	WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
397 398 399 400 401 402 403
		    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;

404
	ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
405 406 407 408 409 410
	if (ret) {
		DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
		return;
	}

	for (i = 0; i < len; i += 7) {
411 412 413 414
		writel(buffer[i + 0] << 0 |
		       buffer[i + 1] << 8 |
		       buffer[i + 2] << 16,
		       base + packet_reg);
415 416
		packet_reg += 4;

417 418 419 420 421
		writel(buffer[i + 3] << 0 |
		       buffer[i + 4] << 8 |
		       buffer[i + 5] << 16 |
		       buffer[i + 6] << 24,
		       base + packet_reg);
422 423 424
		packet_reg += 4;
	}

425 426 427
	HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
		   HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
	ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
428 429 430 431 432 433 434
			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)
{
435
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
436
	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
437
	struct drm_connector *connector = &vc4_hdmi->connector;
438
	struct drm_connector_state *cstate = connector->state;
439
	struct drm_crtc *crtc = cstate->crtc;
440 441 442 443
	const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
	union hdmi_infoframe frame;
	int ret;

444
	ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
445
						       connector, mode);
446 447 448 449 450
	if (ret < 0) {
		DRM_ERROR("couldn't fill AVI infoframe\n");
		return;
	}

451
	drm_hdmi_avi_infoframe_quant_range(&frame.avi,
452
					   connector, mode,
453 454
					   vc4_encoder->limited_rgb_range ?
					   HDMI_QUANTIZATION_RANGE_LIMITED :
455
					   HDMI_QUANTIZATION_RANGE_FULL);
456
	drm_hdmi_avi_infoframe_colorspace(&frame.avi, cstate);
457
	drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
458

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	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 已提交
478 479
static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
{
480
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
481
	struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
E
Eric Anholt 已提交
482 483
	union hdmi_infoframe frame;

484
	memcpy(&frame.audio, audio, sizeof(*audio));
E
Eric Anholt 已提交
485 486 487
	vc4_hdmi_write_infoframe(encoder, &frame);
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
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;

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

507 508
static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
{
509 510
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);

511 512
	vc4_hdmi_set_avi_infoframe(encoder);
	vc4_hdmi_set_spd_infoframe(encoder);
513 514 515 516 517 518
	/*
	 * 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);
519 520

	vc4_hdmi_set_hdr_infoframe(encoder);
521 522
}

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder,
					 struct drm_display_mode *mode)
{
	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
	struct drm_display_info *display = &vc4_hdmi->connector.display_info;

	if (!vc4_encoder->hdmi_monitor)
		return false;

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

	return true;
}

540 541
#define SCRAMBLING_POLLING_DELAY_MS	1000

542 543 544
static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
545 546 547 548
	struct drm_connector *connector = &vc4_hdmi->connector;
	struct drm_connector_state *cstate = connector->state;
	struct drm_crtc *crtc = cstate->crtc;
	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
549 550 551 552 553 554 555 556 557 558 559 560

	if (!vc4_hdmi_supports_scrambling(encoder, mode))
		return;

	if (!vc4_hdmi_mode_needs_scrambling(mode))
		return;

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

	HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
		   VC5_HDMI_SCRAMBLER_CTL_ENABLE);
561 562 563

	queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
			   msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
564 565 566 567 568
}

static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
569 570
	struct drm_connector *connector = &vc4_hdmi->connector;
	struct drm_connector_state *cstate = connector->state;
571 572

	/*
573
	 * At boot, connector->state will be NULL. Since we don't know the
574 575 576
	 * state of the scrambler and in order to avoid any
	 * inconsistency, let's disable it all the time.
	 */
577
	if (cstate && !vc4_hdmi_supports_scrambling(encoder, &cstate->crtc->mode))
578 579
		return;

580
	if (cstate && !vc4_hdmi_mode_needs_scrambling(&cstate->crtc->mode))
581 582
		return;

583 584 585
	if (delayed_work_pending(&vc4_hdmi->scrambling_work))
		cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);

586 587 588 589 590 591 592
	HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
		   ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);

	drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
	drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
}

593 594 595 596 597 598 599 600 601 602 603 604 605 606
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));
607 608
}

609 610
static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
					       struct drm_atomic_state *state)
611
{
612
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
613

614
	HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
615

616
	HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
617

618
	mdelay(1);
619

620 621
	HDMI_WRITE(HDMI_VID_CTL,
		   HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
622
	vc4_hdmi_disable_scrambling(encoder);
623 624
}

625 626
static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
						 struct drm_atomic_state *state)
627 628
{
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
629 630
	int ret;

631 632 633
	HDMI_WRITE(HDMI_VID_CTL,
		   HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);

634 635
	if (vc4_hdmi->variant->phy_disable)
		vc4_hdmi->variant->phy_disable(vc4_hdmi);
636

637
	clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
638
	clk_disable_unprepare(vc4_hdmi->pixel_clock);
639

640
	ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
641 642 643 644
	if (ret < 0)
		DRM_ERROR("Failed to release power domain: %d\n", ret);
}

645 646 647 648
static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
{
}

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
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);
}

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
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);
}

723
static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
724
				 struct drm_connector_state *state,
725
				 struct drm_display_mode *mode)
726 727 728
{
	bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
	bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
729
	bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
730
	u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
731
	u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
732
				   VC4_HDMI_VERTA_VSP) |
733
		     VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
734
				   VC4_HDMI_VERTA_VFP) |
735
		     VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
736
	u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
737
		     VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
738
				   VC4_HDMI_VERTB_VBP));
739 740 741 742 743
	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));
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767

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

769
static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
770
				 struct drm_connector_state *state,
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
				 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));
790 791 792
	unsigned char gcp;
	bool gcp_en;
	u32 reg;
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

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

818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
	switch (state->max_bpc) {
	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;
	}

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

851 852
	HDMI_WRITE(HDMI_CLOCK_STOP, 0);
}
853

854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
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");
}

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
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;
}

894 895
static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
						struct drm_atomic_state *state)
896
{
897 898 899 900
	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);
901 902 903
	struct drm_crtc_state *crtc_state =
		drm_atomic_get_new_crtc_state(state, conn_state->crtc);
	struct drm_display_mode *mode = &crtc_state->adjusted_mode;
904
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
905
	unsigned long bvb_rate, pixel_rate, hsm_rate;
906 907
	int ret;

908
	ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
909 910 911 912 913
	if (ret < 0) {
		DRM_ERROR("Failed to retain power domain: %d\n", ret);
		return;
	}

914
	pixel_rate = vc4_conn_state->pixel_rate;
915
	ret = clk_set_rate(vc4_hdmi->pixel_clock, pixel_rate);
916 917 918 919 920
	if (ret) {
		DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
		return;
	}

921
	ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
922 923 924 925 926
	if (ret) {
		DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
		return;
	}

927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
	/*
	 * 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.
942
	 */
943
	hsm_rate = max_t(unsigned long, 120000000, (pixel_rate / 100) * 101);
944
	ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
945 946 947 948
	if (ret) {
		DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
		return;
	}
949

950 951
	vc4_hdmi_cec_update_clk_div(vc4_hdmi);

952 953 954 955 956 957 958 959
	if (pixel_rate > 297000000)
		bvb_rate = 300000000;
	else if (pixel_rate > 148500000)
		bvb_rate = 150000000;
	else
		bvb_rate = 75000000;

	ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
960 961 962 963 964
	if (ret) {
		DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
		clk_disable_unprepare(vc4_hdmi->pixel_clock);
		return;
	}
965

966 967 968 969 970
	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->pixel_clock);
		return;
971 972
	}

973
	if (vc4_hdmi->variant->phy_init)
974
		vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
975

976 977
	HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
		   HDMI_READ(HDMI_SCHEDULER_CONTROL) |
978 979 980
		   VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
		   VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);

981
	if (vc4_hdmi->variant->set_timings)
982
		vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
983
}
984

985 986
static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
					     struct drm_atomic_state *state)
987
{
988 989 990 991 992
	struct drm_connector_state *conn_state =
		vc4_hdmi_encoder_get_connector_state(encoder, state);
	struct drm_crtc_state *crtc_state =
		drm_atomic_get_new_crtc_state(state, conn_state->crtc);
	struct drm_display_mode *mode = &crtc_state->adjusted_mode;
993 994
	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
995

996
	if (vc4_encoder->hdmi_monitor &&
997 998 999
	    drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED) {
		if (vc4_hdmi->variant->csc_setup)
			vc4_hdmi->variant->csc_setup(vc4_hdmi, true);
1000

1001 1002
		vc4_encoder->limited_rgb_range = true;
	} else {
1003 1004 1005
		if (vc4_hdmi->variant->csc_setup)
			vc4_hdmi->variant->csc_setup(vc4_hdmi, false);

1006
		vc4_encoder->limited_rgb_range = false;
1007 1008
	}

1009
	HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
1010
}
1011

1012 1013
static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
					      struct drm_atomic_state *state)
1014
{
1015 1016 1017 1018 1019
	struct drm_connector_state *conn_state =
		vc4_hdmi_encoder_get_connector_state(encoder, state);
	struct drm_crtc_state *crtc_state =
		drm_atomic_get_new_crtc_state(state, conn_state->crtc);
	struct drm_display_mode *mode = &crtc_state->adjusted_mode;
1020 1021
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
1022 1023
	bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
	bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1024
	int ret;
1025

1026 1027
	HDMI_WRITE(HDMI_VID_CTL,
		   VC4_HD_VID_CTL_ENABLE |
1028
		   VC4_HD_VID_CTL_CLRRGB |
1029
		   VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
1030 1031 1032
		   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));
1033

1034 1035
	HDMI_WRITE(HDMI_VID_CTL,
		   HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1036 1037

	if (vc4_encoder->hdmi_monitor) {
1038 1039
		HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1040 1041
			   VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);

1042
		ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1043
			       VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1044 1045 1046
		WARN_ONCE(ret, "Timeout waiting for "
			  "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
	} else {
1047 1048
		HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
			   HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
1049
			   ~(VC4_HDMI_RAM_PACKET_ENABLE));
1050 1051
		HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1052 1053
			   ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);

1054
		ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1055
				 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1056 1057 1058 1059 1060
		WARN_ONCE(ret, "Timeout waiting for "
			  "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
	}

	if (vc4_encoder->hdmi_monitor) {
1061
		WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1062
			  VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
1063 1064
		HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1065 1066
			   VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);

1067
		HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1068 1069 1070
			   VC4_HDMI_RAM_PACKET_ENABLE);

		vc4_hdmi_set_infoframes(encoder);
1071
	}
1072 1073

	vc4_hdmi_recenter_fifo(vc4_hdmi);
1074
	vc4_hdmi_enable_scrambling(encoder);
1075 1076
}

1077 1078
static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
{
1079 1080
}

1081 1082 1083
#define WIFI_2_4GHz_CH1_MIN_FREQ	2400000000ULL
#define WIFI_2_4GHz_CH1_MAX_FREQ	2422000000ULL

1084 1085 1086 1087
static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
					 struct drm_crtc_state *crtc_state,
					 struct drm_connector_state *conn_state)
{
1088
	struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
1089 1090 1091
	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;
1092
	unsigned long long tmds_rate;
1093

1094 1095 1096 1097 1098
	if (vc4_hdmi->variant->unsupported_odd_h_timings &&
	    ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
	     (mode->hsync_end % 2) || (mode->htotal % 2)))
		return -EINVAL;

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
	/*
	 * 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;
	}

1113 1114 1115 1116 1117 1118 1119 1120
	if (conn_state->max_bpc == 12) {
		pixel_rate = pixel_rate * 150;
		do_div(pixel_rate, 100);
	} else if (conn_state->max_bpc == 10) {
		pixel_rate = pixel_rate * 125;
		do_div(pixel_rate, 100);
	}

1121 1122 1123
	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
		pixel_rate = pixel_rate * 2;

1124 1125 1126
	if (pixel_rate > vc4_hdmi->variant->max_pixel_clock)
		return -EINVAL;

1127 1128 1129
	if (vc4_hdmi->disable_4kp60 && (pixel_rate > HDMI_14_MAX_TMDS_CLK))
		return -EINVAL;

1130 1131
	vc4_state->pixel_rate = pixel_rate;

1132 1133 1134
	return 0;
}

1135
static enum drm_mode_status
1136
vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
1137 1138
			    const struct drm_display_mode *mode)
{
1139 1140
	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);

1141 1142 1143 1144 1145
	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;

1146
	if ((mode->clock * 1000) > vc4_hdmi->variant->max_pixel_clock)
1147 1148
		return MODE_CLOCK_HIGH;

1149 1150 1151
	if (vc4_hdmi->disable_4kp60 && vc4_hdmi_mode_needs_scrambling(mode))
		return MODE_CLOCK_HIGH;

1152 1153 1154
	return MODE_OK;
}

1155
static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
1156
	.atomic_check = vc4_hdmi_encoder_atomic_check,
1157
	.mode_valid = vc4_hdmi_encoder_mode_valid,
1158 1159 1160 1161
	.disable = vc4_hdmi_encoder_disable,
	.enable = vc4_hdmi_encoder_enable,
};

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
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;
}

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
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 已提交
1186
/* HDMI audio codec callbacks */
1187 1188
static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
					 unsigned int samplerate)
E
Eric Anholt 已提交
1189
{
1190
	u32 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
E
Eric Anholt 已提交
1191 1192
	unsigned long n, m;

1193
	rational_best_approximation(hsm_clock, samplerate,
E
Eric Anholt 已提交
1194 1195 1196 1197 1198 1199
				    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);

1200 1201 1202
	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 已提交
1203 1204
}

1205
static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
E
Eric Anholt 已提交
1206
{
1207 1208
	struct drm_connector *connector = &vc4_hdmi->connector;
	struct drm_crtc *crtc = connector->state->crtc;
E
Eric Anholt 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217
	const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
	u32 n, cts;
	u64 tmp;

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

1218
	HDMI_WRITE(HDMI_CRP_CFG,
E
Eric Anholt 已提交
1219 1220 1221 1222 1223 1224 1225 1226
		   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
	 */
1227 1228
	HDMI_WRITE(HDMI_CTS_0, cts);
	HDMI_WRITE(HDMI_CTS_1, cts);
E
Eric Anholt 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237
}

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

1238
static int vc4_hdmi_audio_startup(struct device *dev, void *data)
E
Eric Anholt 已提交
1239
{
1240
	struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1241
	struct drm_connector *connector = &vc4_hdmi->connector;
E
Eric Anholt 已提交
1242 1243 1244 1245 1246

	/*
	 * If the HDMI encoder hasn't probed, or the encoder is
	 * currently in DVI mode, treat the codec dai as missing.
	 */
1247
	if (!connector->state || !(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
E
Eric Anholt 已提交
1248 1249 1250
				VC4_HDMI_RAM_PACKET_ENABLE))
		return -ENODEV;

1251
	vc4_hdmi->audio.streaming = true;
E
Eric Anholt 已提交
1252

1253 1254 1255 1256 1257 1258 1259 1260 1261
	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);

	if (vc4_hdmi->variant->phy_rng_enable)
		vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
E
Eric Anholt 已提交
1262 1263 1264 1265

	return 0;
}

1266
static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
1267
{
1268 1269
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
	struct device *dev = &vc4_hdmi->pdev->dev;
E
Eric Anholt 已提交
1270 1271
	int ret;

1272
	vc4_hdmi->audio.streaming = false;
1273
	ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
E
Eric Anholt 已提交
1274 1275 1276
	if (ret)
		dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);

1277 1278 1279
	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 已提交
1280 1281
}

1282
static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
E
Eric Anholt 已提交
1283
{
1284
	struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
E
Eric Anholt 已提交
1285

1286 1287 1288 1289
	HDMI_WRITE(HDMI_MAI_CTL,
		   VC4_HD_MAI_CTL_DLATE |
		   VC4_HD_MAI_CTL_ERRORE |
		   VC4_HD_MAI_CTL_ERRORF);
E
Eric Anholt 已提交
1290

1291 1292
	if (vc4_hdmi->variant->phy_rng_disable)
		vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
E
Eric Anholt 已提交
1293

1294
	vc4_hdmi->audio.streaming = false;
1295
	vc4_hdmi_audio_reset(vc4_hdmi);
E
Eric Anholt 已提交
1296 1297
}

D
Dom Cobley 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
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 已提交
1334 1335 1336
}

/* HDMI audio codec callbacks */
1337 1338 1339
static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
				  struct hdmi_codec_daifmt *daifmt,
				  struct hdmi_codec_params *params)
E
Eric Anholt 已提交
1340
{
1341
	struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1342
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
1343 1344
	unsigned int sample_rate = params->sample_rate;
	unsigned int channels = params->channels;
E
Eric Anholt 已提交
1345
	u32 audio_packet_config, channel_mask;
1346
	u32 channel_map;
D
Dom Cobley 已提交
1347 1348
	u32 mai_audio_format;
	u32 mai_sample_rate;
E
Eric Anholt 已提交
1349 1350

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

1353
	HDMI_WRITE(HDMI_MAI_CTL,
1354
		   VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
1355 1356 1357
		   VC4_HD_MAI_CTL_WHOLSMP |
		   VC4_HD_MAI_CTL_CHALIGN |
		   VC4_HD_MAI_CTL_ENABLE);
E
Eric Anholt 已提交
1358

1359
	vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
E
Eric Anholt 已提交
1360

1361
	mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
1362 1363 1364 1365 1366
	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 已提交
1367 1368 1369 1370 1371 1372
	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));

1373
	/* The B frame identifier should match the value used by alsa-lib (8) */
E
Eric Anholt 已提交
1374 1375 1376
	audio_packet_config =
		VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
		VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
1377
		VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
E
Eric Anholt 已提交
1378

1379
	channel_mask = GENMASK(channels - 1, 0);
E
Eric Anholt 已提交
1380 1381 1382
	audio_packet_config |= VC4_SET_FIELD(channel_mask,
					     VC4_HDMI_AUDIO_PACKET_CEA_MASK);

1383 1384 1385 1386 1387 1388
	/* Set the MAI threshold */
	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 已提交
1389

1390
	HDMI_WRITE(HDMI_MAI_CONFIG,
E
Eric Anholt 已提交
1391
		   VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
1392
		   VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
E
Eric Anholt 已提交
1393 1394
		   VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));

1395
	channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
1396 1397
	HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
	HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
1398
	vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
E
Eric Anholt 已提交
1399

1400
	memcpy(&vc4_hdmi->audio.infoframe, &params->cea, sizeof(params->cea));
1401 1402
	vc4_hdmi_set_audio_infoframe(encoder);

E
Eric Anholt 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	return 0;
}

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

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)
{
1420
	struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
E
Eric Anholt 已提交
1421

1422
	snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
E
Eric Anholt 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446

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

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
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;

	memcpy(buf, connector->eld, min(sizeof(connector->eld), len));

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

struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
	.ops = &vc4_hdmi_codec_ops,
	.max_i2s_channels = 8,
	.i2s = 1,
};

1471
static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
1472
{
1473 1474
	const struct vc4_hdmi_register *mai_data =
		&vc4_hdmi->variant->registers[HDMI_MAI_DATA];
1475 1476 1477
	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;
1478
	struct platform_device *codec_pdev;
E
Eric Anholt 已提交
1479
	const __be32 *addr;
1480
	int index;
E
Eric Anholt 已提交
1481 1482 1483 1484 1485 1486 1487 1488
	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;
	}

1489 1490 1491 1492 1493
	if (mai_data->reg != VC4_HD) {
		WARN_ONCE(true, "MAI isn't in the HD block\n");
		return -EINVAL;
	}

E
Eric Anholt 已提交
1494 1495 1496 1497 1498 1499 1500
	/*
	 * 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.
	 */
1501 1502 1503 1504 1505 1506 1507
	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);

1508
	vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
1509 1510
	vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
	vc4_hdmi->audio.dma_data.maxburst = 2;
E
Eric Anholt 已提交
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524

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

1525 1526 1527 1528 1529 1530 1531
	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 已提交
1532 1533
	}

1534 1535 1536
	dai_link->cpus		= &vc4_hdmi->audio.cpu;
	dai_link->codecs	= &vc4_hdmi->audio.codec;
	dai_link->platforms	= &vc4_hdmi->audio.platform;
1537 1538 1539

	dai_link->num_cpus	= 1;
	dai_link->num_codecs	= 1;
1540
	dai_link->num_platforms	= 1;
1541

E
Eric Anholt 已提交
1542 1543
	dai_link->name = "MAI";
	dai_link->stream_name = "MAI PCM";
1544
	dai_link->codecs->dai_name = "i2s-hifi";
1545
	dai_link->cpus->dai_name = dev_name(dev);
1546
	dai_link->codecs->name = dev_name(&codec_pdev->dev);
1547
	dai_link->platforms->name = dev_name(dev);
E
Eric Anholt 已提交
1548 1549 1550

	card->dai_link = dai_link;
	card->num_links = 1;
1551
	card->name = vc4_hdmi->variant->card_name;
1552
	card->driver_name = "vc4-hdmi";
E
Eric Anholt 已提交
1553
	card->dev = dev;
1554
	card->owner = THIS_MODULE;
E
Eric Anholt 已提交
1555 1556 1557 1558 1559 1560 1561 1562

	/*
	 * 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.
	 */
1563
	snd_soc_card_set_drvdata(card, vc4_hdmi);
E
Eric Anholt 已提交
1564
	ret = devm_snd_soc_register_card(dev, card);
1565
	if (ret)
1566
		dev_err_probe(dev, ret, "Could not register sound card\n");
E
Eric Anholt 已提交
1567 1568 1569 1570 1571

	return ret;

}

1572 1573 1574 1575 1576
static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
{
	struct vc4_hdmi *vc4_hdmi = priv;
	struct drm_device *dev = vc4_hdmi->connector.dev;

1577
	if (dev && dev->registered)
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
		drm_kms_helper_hotplug_event(dev);

	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) {
1590 1591 1592 1593 1594 1595 1596
		unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
		unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");

		ret = request_threaded_irq(hpd_con,
					   NULL,
					   vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
					   "vc4 hdmi hpd connected", vc4_hdmi);
1597 1598 1599
		if (ret)
			return ret;

1600 1601 1602 1603 1604 1605
		ret = request_threaded_irq(hpd_rm,
					   NULL,
					   vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
					   "vc4 hdmi hpd disconnected", vc4_hdmi);
		if (ret) {
			free_irq(hpd_con, vc4_hdmi);
1606
			return ret;
1607
		}
1608 1609 1610 1611 1612 1613 1614

		connector->polled = DRM_CONNECTOR_POLL_HPD;
	}

	return 0;
}

1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
static void vc4_hdmi_hotplug_exit(struct vc4_hdmi *vc4_hdmi)
{
	struct platform_device *pdev = vc4_hdmi->pdev;

	if (vc4_hdmi->variant->external_irq_controller) {
		free_irq(platform_get_irq_byname(pdev, "hpd-connected"), vc4_hdmi);
		free_irq(platform_get_irq_byname(pdev, "hpd-removed"), vc4_hdmi);
	}
}

H
Hans Verkuil 已提交
1625
#ifdef CONFIG_DRM_VC4_HDMI_CEC
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
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 已提交
1638
{
1639
	struct vc4_hdmi *vc4_hdmi = priv;
1640

1641
	if (vc4_hdmi->cec_tx_ok) {
1642
		cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
H
Hans Verkuil 已提交
1643 1644 1645 1646 1647 1648
				  0, 0, 0, 0);
	} else {
		/*
		 * This CEC implementation makes 1 retry, so if we
		 * get a NACK, then that means it made 2 attempts.
		 */
1649
		cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
H
Hans Verkuil 已提交
1650 1651 1652 1653 1654
				  0, 2, 0, 0);
	}
	return IRQ_HANDLED;
}

1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
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;
}

1668
static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
H
Hans Verkuil 已提交
1669
{
1670
	struct drm_device *dev = vc4_hdmi->connector.dev;
1671
	struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
H
Hans Verkuil 已提交
1672 1673 1674 1675
	unsigned int i;

	msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
					VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
1676 1677 1678 1679 1680 1681

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

H
Hans Verkuil 已提交
1682
	for (i = 0; i < msg->len; i += 4) {
1683
		u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
H
Hans Verkuil 已提交
1684 1685 1686 1687 1688 1689 1690 1691

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

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 1721
static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
{
	struct vc4_hdmi *vc4_hdmi = priv;
	u32 cntrl1;

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

static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
{
	struct vc4_hdmi *vc4_hdmi = priv;
	u32 cntrl1;

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

H
Hans Verkuil 已提交
1722 1723
static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
{
1724
	struct vc4_hdmi *vc4_hdmi = priv;
1725
	u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
1726 1727
	irqreturn_t ret;
	u32 cntrl5;
H
Hans Verkuil 已提交
1728 1729 1730

	if (!(stat & VC4_HDMI_CPU_CEC))
		return IRQ_NONE;
1731

1732
	cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
1733
	vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
1734 1735 1736 1737
	if (vc4_hdmi->cec_irq_was_rx)
		ret = vc4_cec_irq_handler_rx_bare(irq, priv);
	else
		ret = vc4_cec_irq_handler_tx_bare(irq, priv);
H
Hans Verkuil 已提交
1738

1739 1740
	HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
	return ret;
H
Hans Verkuil 已提交
1741 1742 1743 1744
}

static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
{
1745
	struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
H
Hans Verkuil 已提交
1746 1747
	/* clock period in microseconds */
	const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
1748
	u32 val = HDMI_READ(HDMI_CEC_CNTRL_5);
H
Hans Verkuil 已提交
1749 1750 1751 1752 1753 1754 1755 1756

	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) {
1757
		HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
H
Hans Verkuil 已提交
1758
			   VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
		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));

1777 1778
		if (!vc4_hdmi->variant->external_irq_controller)
			HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
H
Hans Verkuil 已提交
1779
	} else {
1780 1781
		if (!vc4_hdmi->variant->external_irq_controller)
			HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
1782
		HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
H
Hans Verkuil 已提交
1783 1784 1785 1786 1787 1788 1789
			   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)
{
1790
	struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
H
Hans Verkuil 已提交
1791

1792 1793
	HDMI_WRITE(HDMI_CEC_CNTRL_1,
		   (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
H
Hans Verkuil 已提交
1794 1795 1796 1797 1798 1799 1800
		   (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)
{
1801
	struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
1802
	struct drm_device *dev = vc4_hdmi->connector.dev;
H
Hans Verkuil 已提交
1803 1804 1805
	u32 val;
	unsigned int i;

1806 1807 1808 1809 1810
	if (msg->len > 16) {
		drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
		return -ENOMEM;
	}

H
Hans Verkuil 已提交
1811
	for (i = 0; i < msg->len; i += 4)
1812
		HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
H
Hans Verkuil 已提交
1813 1814 1815 1816 1817
			   (msg->msg[i]) |
			   (msg->msg[i + 1] << 8) |
			   (msg->msg[i + 2] << 16) |
			   (msg->msg[i + 3] << 24));

1818
	val = HDMI_READ(HDMI_CEC_CNTRL_1);
H
Hans Verkuil 已提交
1819
	val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1820
	HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
H
Hans Verkuil 已提交
1821 1822 1823 1824
	val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
	val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
	val |= VC4_HDMI_CEC_START_XMIT_BEGIN;

1825
	HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
H
Hans Verkuil 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834
	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,
};

1835
static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
1836
{
1837
	struct cec_connector_info conn_info;
1838
	struct platform_device *pdev = vc4_hdmi->pdev;
1839
	struct device *dev = &pdev->dev;
1840 1841 1842
	u32 value;
	int ret;

1843 1844 1845 1846 1847
	if (!of_find_property(dev->of_node, "interrupts", NULL)) {
		dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
		return 0;
	}

1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
	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);

	value = HDMI_READ(HDMI_CEC_CNTRL_1);
1860 1861
	/* Set the logical address to Unregistered */
	value |= VC4_HDMI_CEC_ADDR_MASK;
1862
	HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
1863 1864 1865

	vc4_hdmi_cec_update_clk_div(vc4_hdmi);

1866
	if (vc4_hdmi->variant->external_irq_controller) {
1867 1868 1869 1870
		ret = request_threaded_irq(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);
1871 1872 1873
		if (ret)
			goto err_delete_cec_adap;

1874 1875 1876 1877
		ret = request_threaded_irq(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);
1878
		if (ret)
1879
			goto err_remove_cec_rx_handler;
1880 1881 1882
	} else {
		HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);

1883 1884 1885 1886
		ret = request_threaded_irq(platform_get_irq(pdev, 0),
					   vc4_cec_irq_handler,
					   vc4_cec_irq_handler_thread, 0,
					   "vc4 hdmi cec", vc4_hdmi);
1887 1888 1889
		if (ret)
			goto err_delete_cec_adap;
	}
1890 1891 1892

	ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
	if (ret < 0)
1893
		goto err_remove_handlers;
1894 1895 1896

	return 0;

1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
err_remove_handlers:
	if (vc4_hdmi->variant->external_irq_controller)
		free_irq(platform_get_irq_byname(pdev, "cec-tx"), vc4_hdmi);
	else
		free_irq(platform_get_irq(pdev, 0), vc4_hdmi);

err_remove_cec_rx_handler:
	if (vc4_hdmi->variant->external_irq_controller)
		free_irq(platform_get_irq_byname(pdev, "cec-rx"), vc4_hdmi);

1907 1908 1909 1910 1911 1912 1913 1914
err_delete_cec_adap:
	cec_delete_adapter(vc4_hdmi->cec_adap);

	return ret;
}

static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi)
{
1915 1916 1917 1918 1919 1920 1921 1922 1923
	struct platform_device *pdev = vc4_hdmi->pdev;

	if (vc4_hdmi->variant->external_irq_controller) {
		free_irq(platform_get_irq_byname(pdev, "cec-rx"), vc4_hdmi);
		free_irq(platform_get_irq_byname(pdev, "cec-tx"), vc4_hdmi);
	} else {
		free_irq(platform_get_irq(pdev, 0), vc4_hdmi);
	}

1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
	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 已提交
1934 1935
#endif

1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
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)
1948 1949
		return -ENOMEM;

1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
	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)
1963
		return -ENOMEM;
1964 1965 1966 1967 1968 1969 1970 1971

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

	return 0;
}

1972
static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
1973
{
1974 1975
	struct platform_device *pdev = vc4_hdmi->pdev;
	struct device *dev = &pdev->dev;
1976 1977
	int ret;

1978 1979 1980
	vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
	if (IS_ERR(vc4_hdmi->hdmicore_regs))
		return PTR_ERR(vc4_hdmi->hdmicore_regs);
1981

1982 1983 1984
	vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
	if (IS_ERR(vc4_hdmi->hd_regs))
		return PTR_ERR(vc4_hdmi->hd_regs);
1985

1986 1987 1988
	ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
	if (ret)
		return ret;
1989

1990 1991 1992
	ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
	if (ret)
		return ret;
1993

1994 1995 1996
	vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
	if (IS_ERR(vc4_hdmi->pixel_clock)) {
		ret = PTR_ERR(vc4_hdmi->pixel_clock);
1997 1998 1999
		if (ret != -EPROBE_DEFER)
			DRM_ERROR("Failed to get pixel clock\n");
		return ret;
2000
	}
2001

2002 2003
	vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
	if (IS_ERR(vc4_hdmi->hsm_clock)) {
2004
		DRM_ERROR("Failed to get HDMI state machine clock\n");
2005
		return PTR_ERR(vc4_hdmi->hsm_clock);
2006
	}
2007
	vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
2008
	vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
2009

2010 2011 2012
	return 0;
}

2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
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));
2025 2026
	if (!vc4_hdmi->hdmicore_regs)
		return -ENOMEM;
2027 2028 2029 2030 2031 2032

	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));
2033 2034
	if (!vc4_hdmi->hd_regs)
		return -ENOMEM;
2035 2036 2037 2038 2039 2040

	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));
2041 2042
	if (!vc4_hdmi->cec_regs)
		return -ENOMEM;
2043 2044 2045 2046 2047 2048

	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));
2049 2050
	if (!vc4_hdmi->csc_regs)
		return -ENOMEM;
2051 2052 2053 2054 2055 2056

	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));
2057 2058
	if (!vc4_hdmi->dvp_regs)
		return -ENOMEM;
2059 2060 2061 2062 2063 2064

	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));
2065 2066
	if (!vc4_hdmi->phy_regs)
		return -ENOMEM;
2067 2068 2069 2070 2071 2072

	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));
2073 2074
	if (!vc4_hdmi->ram_regs)
		return -ENOMEM;
2075 2076 2077 2078 2079 2080

	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));
2081 2082
	if (!vc4_hdmi->rm_regs)
		return -ENOMEM;
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101

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

2102 2103 2104 2105 2106 2107
	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);
	}

2108 2109 2110 2111 2112 2113 2114 2115 2116
	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;
}

2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
#ifdef CONFIG_PM
static int vc4_hdmi_runtime_suspend(struct device *dev)
{
	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);
	int ret;

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

	return 0;
}
#endif

2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
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;

	vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
	if (!vc4_hdmi)
		return -ENOMEM;
2153
	INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
2154 2155 2156

	dev_set_drvdata(dev, vc4_hdmi);
	encoder = &vc4_hdmi->encoder.base.base;
2157
	vc4_hdmi->encoder.base.type = variant->encoder_type;
2158 2159 2160 2161 2162
	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;
2163 2164 2165 2166 2167 2168
	vc4_hdmi->pdev = pdev;
	vc4_hdmi->variant = variant;

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

2170 2171 2172 2173 2174 2175
	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;
	}

2176
	vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
2177
	of_node_put(ddc_node);
2178
	if (!vc4_hdmi->ddc) {
2179 2180 2181 2182 2183 2184 2185
		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.
	 */
M
Maxime Ripard 已提交
2186 2187 2188 2189
	vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
	if (IS_ERR(vc4_hdmi->hpd_gpio)) {
		ret = PTR_ERR(vc4_hdmi->hpd_gpio);
		goto err_put_ddc;
2190 2191
	}

2192 2193 2194
	vc4_hdmi->disable_wifi_frequencies =
		of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");

2195 2196 2197 2198 2199 2200 2201 2202
	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;
	}

2203 2204 2205
	if (vc4_hdmi->variant->reset)
		vc4_hdmi->variant->reset(vc4_hdmi);

2206 2207 2208 2209 2210 2211 2212 2213
	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);
	}

2214
	pm_runtime_enable(dev);
2215

2216 2217
	drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
	drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
2218

2219
	ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
2220
	if (ret)
2221
		goto err_destroy_encoder;
2222

2223
	ret = vc4_hdmi_hotplug_init(vc4_hdmi);
H
Hans Verkuil 已提交
2224
	if (ret)
2225
		goto err_destroy_conn;
2226

2227
	ret = vc4_hdmi_cec_init(vc4_hdmi);
H
Hans Verkuil 已提交
2228
	if (ret)
2229
		goto err_free_hotplug;
2230

2231
	ret = vc4_hdmi_audio_init(vc4_hdmi);
E
Eric Anholt 已提交
2232
	if (ret)
2233
		goto err_free_cec;
E
Eric Anholt 已提交
2234

2235 2236 2237
	vc4_debugfs_add_file(drm, variant->debugfs_name,
			     vc4_hdmi_debugfs_regs,
			     vc4_hdmi);
2238

2239 2240
	return 0;

2241 2242
err_free_cec:
	vc4_hdmi_cec_exit(vc4_hdmi);
2243 2244
err_free_hotplug:
	vc4_hdmi_hotplug_exit(vc4_hdmi);
H
Hans Verkuil 已提交
2245
err_destroy_conn:
2246
	vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
2247
err_destroy_encoder:
2248
	drm_encoder_cleanup(encoder);
2249
	pm_runtime_disable(dev);
2250
err_put_ddc:
2251
	put_device(&vc4_hdmi->ddc->dev);
2252 2253 2254 2255 2256 2257 2258

	return ret;
}

static void vc4_hdmi_unbind(struct device *dev, struct device *master,
			    void *data)
{
2259
	struct vc4_hdmi *vc4_hdmi;
2260

2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
	/*
	 * 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);
2281

2282 2283
	kfree(vc4_hdmi->hdmi_regset.regs);
	kfree(vc4_hdmi->hd_regset.regs);
2284

2285
	vc4_hdmi_cec_exit(vc4_hdmi);
2286
	vc4_hdmi_hotplug_exit(vc4_hdmi);
2287
	vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
2288
	drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
2289

2290
	pm_runtime_disable(dev);
2291

2292
	put_device(&vc4_hdmi->ddc->dev);
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
}

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

2311
static const struct vc4_hdmi_variant bcm2835_variant = {
2312
	.encoder_type		= VC4_ENCODER_TYPE_HDMI0,
2313
	.debugfs_name		= "hdmi_regs",
2314
	.card_name		= "vc4-hdmi",
2315
	.max_pixel_clock	= 162000000,
2316 2317 2318
	.registers		= vc4_hdmi_fields,
	.num_registers		= ARRAY_SIZE(vc4_hdmi_fields),

2319
	.init_resources		= vc4_hdmi_init_resources,
2320
	.csc_setup		= vc4_hdmi_csc_setup,
2321
	.reset			= vc4_hdmi_reset,
2322
	.set_timings		= vc4_hdmi_set_timings,
2323 2324
	.phy_init		= vc4_hdmi_phy_init,
	.phy_disable		= vc4_hdmi_phy_disable,
2325 2326
	.phy_rng_enable		= vc4_hdmi_phy_rng_enable,
	.phy_rng_disable	= vc4_hdmi_phy_rng_disable,
2327
	.channel_map		= vc4_hdmi_channel_map,
2328
	.supports_hdr		= false,
2329 2330
};

2331 2332 2333 2334
static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
	.encoder_type		= VC4_ENCODER_TYPE_HDMI0,
	.debugfs_name		= "hdmi0_regs",
	.card_name		= "vc4-hdmi-0",
2335
	.max_pixel_clock	= HDMI_14_MAX_TMDS_CLK,
2336 2337 2338 2339 2340 2341 2342 2343
	.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,
	},
2344
	.unsupported_odd_h_timings	= true,
2345
	.external_irq_controller	= true,
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355

	.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,
2356
	.supports_hdr		= true,
2357 2358 2359 2360 2361 2362
};

static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
	.encoder_type		= VC4_ENCODER_TYPE_HDMI1,
	.debugfs_name		= "hdmi1_regs",
	.card_name		= "vc4-hdmi-1",
2363
	.max_pixel_clock	= HDMI_14_MAX_TMDS_CLK,
2364 2365 2366 2367 2368 2369 2370 2371
	.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,
	},
2372
	.unsupported_odd_h_timings	= true,
2373
	.external_irq_controller	= true,
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383

	.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,
2384
	.supports_hdr		= true,
2385 2386
};

2387
static const struct of_device_id vc4_hdmi_dt_match[] = {
2388
	{ .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
2389 2390
	{ .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
	{ .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
2391 2392 2393
	{}
};

2394 2395 2396 2397 2398 2399
static const struct dev_pm_ops vc4_hdmi_pm_ops = {
	SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
			   vc4_hdmi_runtime_resume,
			   NULL)
};

2400 2401 2402 2403 2404 2405
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,
2406
		.pm = &vc4_hdmi_pm_ops,
2407 2408
	},
};