vc4_hdmi.c 42.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 39 40 41 42 43 44 45 46 47 48 49
#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>
#include <sound/dmaengine_pcm.h>
#include <sound/pcm_drm_eld.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
H
Hans Verkuil 已提交
50
#include "media/cec.h"
51
#include "vc4_drv.h"
52
#include "vc4_hdmi.h"
53 54
#include "vc4_regs.h"

H
Hans Verkuil 已提交
55 56 57 58
#define HSM_CLOCK_FREQ 163682864
#define CEC_CLOCK_FREQ 40000
#define CEC_CLOCK_DIV  (HSM_CLOCK_FREQ / CEC_CLOCK_FREQ)

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static const struct debugfs_reg32 hdmi_regs[] = {
	VC4_REG32(VC4_HDMI_CORE_REV),
	VC4_REG32(VC4_HDMI_SW_RESET_CONTROL),
	VC4_REG32(VC4_HDMI_HOTPLUG_INT),
	VC4_REG32(VC4_HDMI_HOTPLUG),
	VC4_REG32(VC4_HDMI_MAI_CHANNEL_MAP),
	VC4_REG32(VC4_HDMI_MAI_CONFIG),
	VC4_REG32(VC4_HDMI_MAI_FORMAT),
	VC4_REG32(VC4_HDMI_AUDIO_PACKET_CONFIG),
	VC4_REG32(VC4_HDMI_RAM_PACKET_CONFIG),
	VC4_REG32(VC4_HDMI_HORZA),
	VC4_REG32(VC4_HDMI_HORZB),
	VC4_REG32(VC4_HDMI_FIFO_CTL),
	VC4_REG32(VC4_HDMI_SCHEDULER_CONTROL),
	VC4_REG32(VC4_HDMI_VERTA0),
	VC4_REG32(VC4_HDMI_VERTA1),
	VC4_REG32(VC4_HDMI_VERTB0),
	VC4_REG32(VC4_HDMI_VERTB1),
	VC4_REG32(VC4_HDMI_TX_PHY_RESET_CTL),
	VC4_REG32(VC4_HDMI_TX_PHY_CTL0),

	VC4_REG32(VC4_HDMI_CEC_CNTRL_1),
	VC4_REG32(VC4_HDMI_CEC_CNTRL_2),
	VC4_REG32(VC4_HDMI_CEC_CNTRL_3),
	VC4_REG32(VC4_HDMI_CEC_CNTRL_4),
	VC4_REG32(VC4_HDMI_CEC_CNTRL_5),
	VC4_REG32(VC4_HDMI_CPU_STATUS),
	VC4_REG32(VC4_HDMI_CPU_MASK_STATUS),

	VC4_REG32(VC4_HDMI_CEC_RX_DATA_1),
	VC4_REG32(VC4_HDMI_CEC_RX_DATA_2),
	VC4_REG32(VC4_HDMI_CEC_RX_DATA_3),
	VC4_REG32(VC4_HDMI_CEC_RX_DATA_4),
	VC4_REG32(VC4_HDMI_CEC_TX_DATA_1),
	VC4_REG32(VC4_HDMI_CEC_TX_DATA_2),
	VC4_REG32(VC4_HDMI_CEC_TX_DATA_3),
	VC4_REG32(VC4_HDMI_CEC_TX_DATA_4),
96 97
};

98 99 100 101 102 103 104 105 106
static const struct debugfs_reg32 hd_regs[] = {
	VC4_REG32(VC4_HD_M_CTL),
	VC4_REG32(VC4_HD_MAI_CTL),
	VC4_REG32(VC4_HD_MAI_THR),
	VC4_REG32(VC4_HD_MAI_FMT),
	VC4_REG32(VC4_HD_MAI_SMP),
	VC4_REG32(VC4_HD_VID_CTL),
	VC4_REG32(VC4_HD_CSC_CTL),
	VC4_REG32(VC4_HD_FRAME_COUNT),
107 108
};

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

115 116
	drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
	drm_print_regset32(&p, &vc4_hdmi->hd_regset);
117 118 119 120 121 122 123 124 125 126 127

	return 0;
}

static enum drm_connector_status
vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
{
	struct drm_device *dev = connector->dev;
	struct vc4_dev *vc4 = to_vc4_dev(dev);

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

135 136 137
	if (drm_probe_ddc(vc4->hdmi->ddc))
		return connector_status_connected;

138 139
	if (HDMI_READ(VC4_HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
		return connector_status_connected;
H
Hans Verkuil 已提交
140 141
	cec_phys_addr_invalidate(vc4->hdmi->cec_adap);
	return connector_status_disconnected;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
}

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)
{
	struct vc4_hdmi_connector *vc4_connector =
		to_vc4_hdmi_connector(connector);
	struct drm_encoder *encoder = vc4_connector->encoder;
	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
	struct drm_device *dev = connector->dev;
	struct vc4_dev *vc4 = to_vc4_dev(dev);
	int ret = 0;
	struct edid *edid;

	edid = drm_get_edid(connector, vc4->hdmi->ddc);
H
Hans Verkuil 已提交
162
	cec_s_phys_addr_from_edid(vc4->hdmi->cec_adap, edid);
163 164 165 166
	if (!edid)
		return -ENODEV;

	vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
167

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

	return ret;
}

175 176 177 178 179 180
static void vc4_hdmi_connector_reset(struct drm_connector *connector)
{
	drm_atomic_helper_connector_reset(connector);
	drm_atomic_helper_connector_tv_reset(connector);
}

181 182
static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
	.detect = vc4_hdmi_connector_detect,
183
	.fill_modes = drm_helper_probe_single_connector_modes,
184
	.destroy = vc4_hdmi_connector_destroy,
185
	.reset = vc4_hdmi_connector_reset,
186 187 188 189 190 191 192 193
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.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,
};

194
static int vc4_hdmi_connector_init(struct drm_device *dev,
195
				   struct vc4_hdmi *vc4_hdmi)
196
{
197 198 199
	struct vc4_hdmi_connector *hdmi_connector = &vc4_hdmi->connector;
	struct drm_connector *connector = &hdmi_connector->base;
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
200
	int ret;
201 202 203

	hdmi_connector->encoder = encoder;

204 205 206
	drm_connector_init_with_ddc(dev, connector,
				    &vc4_hdmi_connector_funcs,
				    DRM_MODE_CONNECTOR_HDMIA,
207
				    vc4_hdmi->ddc);
208 209
	drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);

210 211 212
	/* Create and attach TV margin props to this connector. */
	ret = drm_mode_create_tv_margin_properties(dev);
	if (ret)
213
		return ret;
214 215 216

	drm_connector_attach_tv_margin_properties(connector);

217 218 219
	connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
			     DRM_CONNECTOR_POLL_DISCONNECT);

220
	connector->interlace_allowed = 1;
221 222
	connector->doublescan_allowed = 0;

223
	drm_connector_attach_encoder(connector, encoder);
224

225
	return 0;
226 227
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
				enum hdmi_infoframe_type type)
{
	struct drm_device *dev = encoder->dev;
	struct vc4_dev *vc4 = to_vc4_dev(dev);
	u32 packet_id = type - 0x80;

	HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
		   HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));

	return wait_for(!(HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
			  BIT(packet_id)), 100);
}

static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
				     union hdmi_infoframe *frame)
{
	struct drm_device *dev = encoder->dev;
	struct vc4_dev *vc4 = to_vc4_dev(dev);
	u32 packet_id = frame->any.type - 0x80;
E
Eric Anholt 已提交
248
	u32 packet_reg = VC4_HDMI_RAM_PACKET(packet_id);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
	ssize_t len, i;
	int ret;

	WARN_ONCE(!(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
		    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;

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

	for (i = 0; i < len; i += 7) {
		HDMI_WRITE(packet_reg,
			   buffer[i + 0] << 0 |
			   buffer[i + 1] << 8 |
			   buffer[i + 2] << 16);
		packet_reg += 4;

		HDMI_WRITE(packet_reg,
			   buffer[i + 3] << 0 |
			   buffer[i + 4] << 8 |
			   buffer[i + 5] << 16 |
			   buffer[i + 6] << 24);
		packet_reg += 4;
	}

	HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
		   HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
	ret = wait_for((HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
			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)
{
	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
293
	struct vc4_dev *vc4 = encoder->dev->dev_private;
294 295
	struct vc4_hdmi *vc4_hdmi = vc4->hdmi;
	struct drm_connector *connector = &vc4_hdmi->connector.base;
296
	struct drm_connector_state *cstate = connector->state;
297 298 299 300 301
	struct drm_crtc *crtc = encoder->crtc;
	const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
	union hdmi_infoframe frame;
	int ret;

302
	ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
303
						       connector, mode);
304 305 306 307 308
	if (ret < 0) {
		DRM_ERROR("couldn't fill AVI infoframe\n");
		return;
	}

309
	drm_hdmi_avi_infoframe_quant_range(&frame.avi,
310
					   connector, mode,
311 312
					   vc4_encoder->limited_rgb_range ?
					   HDMI_QUANTIZATION_RANGE_LIMITED :
313
					   HDMI_QUANTIZATION_RANGE_FULL);
314

315
	drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
316

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	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 已提交
336 337 338 339
static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
{
	struct drm_device *drm = encoder->dev;
	struct vc4_dev *vc4 = drm->dev_private;
340
	struct vc4_hdmi *vc4_hdmi = vc4->hdmi;
E
Eric Anholt 已提交
341 342 343 344 345 346 347 348
	union hdmi_infoframe frame;
	int ret;

	ret = hdmi_audio_infoframe_init(&frame.audio);

	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;
349
	frame.audio.channels = vc4_hdmi->audio.channels;
E
Eric Anholt 已提交
350 351 352 353

	vc4_hdmi_write_infoframe(encoder, &frame);
}

354 355 356 357 358 359
static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
{
	vc4_hdmi_set_avi_infoframe(encoder);
	vc4_hdmi_set_spd_infoframe(encoder);
}

360 361 362 363
static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
{
	struct drm_device *dev = encoder->dev;
	struct vc4_dev *vc4 = to_vc4_dev(dev);
364
	struct vc4_hdmi *vc4_hdmi = vc4->hdmi;
365 366 367 368 369 370 371 372
	int ret;

	HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, 0);

	HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
	HD_WRITE(VC4_HD_VID_CTL,
		 HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);

373
	clk_disable_unprepare(vc4_hdmi->pixel_clock);
374

375
	ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
376 377 378 379 380
	if (ret < 0)
		DRM_ERROR("Failed to release power domain: %d\n", ret);
}

static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
381
{
382
	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
383
	struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
384 385
	struct drm_device *dev = encoder->dev;
	struct vc4_dev *vc4 = to_vc4_dev(dev);
386
	struct vc4_hdmi *vc4_hdmi = vc4->hdmi;
387 388 389
	bool debug_dump_regs = false;
	bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
	bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
390
	bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
391
	u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
392
	u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
393
				   VC4_HDMI_VERTA_VSP) |
394
		     VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
395
				   VC4_HDMI_VERTA_VFP) |
396
		     VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
397
	u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
398
		     VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
399
				   VC4_HDMI_VERTB_VBP));
400 401 402 403 404
	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));
405
	u32 csc_ctl;
406 407
	int ret;

408
	ret = pm_runtime_get_sync(&vc4_hdmi->pdev->dev);
409 410 411 412 413
	if (ret < 0) {
		DRM_ERROR("Failed to retain power domain: %d\n", ret);
		return;
	}

414
	ret = clk_set_rate(vc4_hdmi->pixel_clock,
415 416 417 418 419 420 421
			   mode->clock * 1000 *
			   ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1));
	if (ret) {
		DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
		return;
	}

422
	ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
	if (ret) {
		DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
		return;
	}

	HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL,
		   VC4_HDMI_SW_RESET_HDMI |
		   VC4_HDMI_SW_RESET_FORMAT_DETECT);

	HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL, 0);

	/* PHY should be in reset, like
	 * vc4_hdmi_encoder_disable() does.
	 */
	HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);

	HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
440 441

	if (debug_dump_regs) {
442
		struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev);
443

444 445 446
		dev_info(&vc4_hdmi->pdev->dev, "HDMI regs before:\n");
		drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
		drm_print_regset32(&p, &vc4_hdmi->hd_regset);
447 448 449 450 451 452 453 454 455 456 457 458
	}

	HD_WRITE(VC4_HD_VID_CTL, 0);

	HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
		   HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
		   VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
		   VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);

	HDMI_WRITE(VC4_HDMI_HORZA,
		   (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
		   (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
459 460
		   VC4_SET_FIELD(mode->hdisplay * pixel_rep,
				 VC4_HDMI_HORZA_HAP));
461 462

	HDMI_WRITE(VC4_HDMI_HORZB,
463 464
		   VC4_SET_FIELD((mode->htotal -
				  mode->hsync_end) * pixel_rep,
465
				 VC4_HDMI_HORZB_HBP) |
466 467
		   VC4_SET_FIELD((mode->hsync_end -
				  mode->hsync_start) * pixel_rep,
468
				 VC4_HDMI_HORZB_HSP) |
469 470
		   VC4_SET_FIELD((mode->hsync_start -
				  mode->hdisplay) * pixel_rep,
471 472 473 474 475
				 VC4_HDMI_HORZB_HFP));

	HDMI_WRITE(VC4_HDMI_VERTA0, verta);
	HDMI_WRITE(VC4_HDMI_VERTA1, verta);

476
	HDMI_WRITE(VC4_HDMI_VERTB0, vertb_even);
477 478 479 480 481 482
	HDMI_WRITE(VC4_HDMI_VERTB1, vertb);

	HD_WRITE(VC4_HD_VID_CTL,
		 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
		 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));

483 484 485
	csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
				VC4_HD_CSC_CTL_ORDER);

486 487 488
	if (vc4_encoder->hdmi_monitor &&
	    drm_default_rgb_quant_range(mode) ==
	    HDMI_QUANTIZATION_RANGE_LIMITED) {
489
		/* CEA VICs other than #1 requre limited range RGB
490 491 492
		 * output unless overridden by an AVI infoframe.
		 * Apply a colorspace conversion to squash 0-255 down
		 * to 16-235.  The matrix here is:
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
		 *
		 * [ 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);

		HD_WRITE(VC4_HD_CSC_12_11, (0x000 << 16) | 0x000);
		HD_WRITE(VC4_HD_CSC_14_13, (0x100 << 16) | 0x6e0);
		HD_WRITE(VC4_HD_CSC_22_21, (0x6e0 << 16) | 0x000);
		HD_WRITE(VC4_HD_CSC_24_23, (0x100 << 16) | 0x000);
		HD_WRITE(VC4_HD_CSC_32_31, (0x000 << 16) | 0x6e0);
		HD_WRITE(VC4_HD_CSC_34_33, (0x100 << 16) | 0x000);
510 511 512
		vc4_encoder->limited_rgb_range = true;
	} else {
		vc4_encoder->limited_rgb_range = false;
513 514
	}

515
	/* The RGB order applies even when CSC is disabled. */
516
	HD_WRITE(VC4_HD_CSC_CTL, csc_ctl);
517 518 519 520

	HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);

	if (debug_dump_regs) {
521
		struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev);
522

523 524 525
		dev_info(&vc4_hdmi->pdev->dev, "HDMI regs after:\n");
		drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
		drm_print_regset32(&p, &vc4_hdmi->hd_regset);
526 527 528 529 530 531 532 533 534 535 536 537 538 539
	}

	HD_WRITE(VC4_HD_VID_CTL,
		 HD_READ(VC4_HD_VID_CTL) |
		 VC4_HD_VID_CTL_ENABLE |
		 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
		 VC4_HD_VID_CTL_FRAME_COUNTER_RESET);

	if (vc4_encoder->hdmi_monitor) {
		HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
			   VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);

		ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
540
			       VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
541 542 543 544 545 546 547 548 549 550 551
		WARN_ONCE(ret, "Timeout waiting for "
			  "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
	} else {
		HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
			   HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
			   ~(VC4_HDMI_RAM_PACKET_ENABLE));
		HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
			   ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);

		ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
552
				 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
553 554 555 556 557 558 559 560 561 562 563 564 565
		WARN_ONCE(ret, "Timeout waiting for "
			  "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
	}

	if (vc4_encoder->hdmi_monitor) {
		u32 drift;

		WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
			  VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
		HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
			   HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
			   VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);

566 567 568 569
		HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
			   VC4_HDMI_RAM_PACKET_ENABLE);

		vc4_hdmi_set_infoframes(encoder);
570 571 572 573 574 575 576 577

		drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
		drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;

		HDMI_WRITE(VC4_HDMI_FIFO_CTL,
			   drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
		HDMI_WRITE(VC4_HDMI_FIFO_CTL,
			   drift | VC4_HDMI_FIFO_CTL_RECENTER);
578
		usleep_range(1000, 1100);
579 580 581 582 583 584 585 586 587 588 589 590
		HDMI_WRITE(VC4_HDMI_FIFO_CTL,
			   drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
		HDMI_WRITE(VC4_HDMI_FIFO_CTL,
			   drift | VC4_HDMI_FIFO_CTL_RECENTER);

		ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
			       VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
		WARN_ONCE(ret, "Timeout waiting for "
			  "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
	}
}

591 592 593 594
static enum drm_mode_status
vc4_hdmi_encoder_mode_valid(struct drm_encoder *crtc,
			    const struct drm_display_mode *mode)
{
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
	/*
	 * 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.
610
	 */
611
	if (mode->clock > HSM_CLOCK_FREQ / (1000 * 101 / 100))
612 613 614 615 616
		return MODE_CLOCK_HIGH;

	return MODE_OK;
}

617
static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
618
	.mode_valid = vc4_hdmi_encoder_mode_valid,
619 620 621 622
	.disable = vc4_hdmi_encoder_disable,
	.enable = vc4_hdmi_encoder_enable,
};

E
Eric Anholt 已提交
623
/* HDMI audio codec callbacks */
624
static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
625
{
626
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
627
	struct drm_device *drm = encoder->dev;
E
Eric Anholt 已提交
628
	struct vc4_dev *vc4 = to_vc4_dev(drm);
629
	u32 hsm_clock = clk_get_rate(vc4_hdmi->hsm_clock);
E
Eric Anholt 已提交
630 631
	unsigned long n, m;

632
	rational_best_approximation(hsm_clock, vc4_hdmi->audio.samplerate,
E
Eric Anholt 已提交
633 634 635 636 637 638 639 640 641 642 643
				    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);

	HD_WRITE(VC4_HD_MAI_SMP,
		 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
		 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
}

644
static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
645
{
646
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
E
Eric Anholt 已提交
647 648 649 650
	struct drm_crtc *crtc = encoder->crtc;
	struct drm_device *drm = encoder->dev;
	struct vc4_dev *vc4 = to_vc4_dev(drm);
	const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
651
	u32 samplerate = vc4_hdmi->audio.samplerate;
E
Eric Anholt 已提交
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
	u32 n, cts;
	u64 tmp;

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

	HDMI_WRITE(VC4_HDMI_CRP_CFG,
		   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
	 */
	HDMI_WRITE(VC4_HDMI_CTS_0, cts);
	HDMI_WRITE(VC4_HDMI_CTS_1, cts);
}

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)
{
683 684 685
	struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
	struct drm_connector *connector = &vc4_hdmi->connector.base;
E
Eric Anholt 已提交
686 687 688
	struct vc4_dev *vc4 = to_vc4_dev(encoder->dev);
	int ret;

689
	if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream)
E
Eric Anholt 已提交
690 691
		return -EINVAL;

692
	vc4_hdmi->audio.substream = substream;
E
Eric Anholt 已提交
693 694 695 696 697 698 699 700 701

	/*
	 * If the HDMI encoder hasn't probed, or the encoder is
	 * currently in DVI mode, treat the codec dai as missing.
	 */
	if (!encoder->crtc || !(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
				VC4_HDMI_RAM_PACKET_ENABLE))
		return -ENODEV;

702
	ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld);
E
Eric Anholt 已提交
703 704 705 706 707 708 709 710 711 712 713
	if (ret)
		return ret;

	return 0;
}

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

714
static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
715
{
716
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
E
Eric Anholt 已提交
717
	struct drm_device *drm = encoder->dev;
718
	struct device *dev = &vc4_hdmi->pdev->dev;
E
Eric Anholt 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	struct vc4_dev *vc4 = to_vc4_dev(drm);
	int ret;

	ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO);
	if (ret)
		dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);

	HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_RESET);
	HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
	HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
}

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

736
	if (substream != vc4_hdmi->audio.substream)
E
Eric Anholt 已提交
737 738
		return;

739
	vc4_hdmi_audio_reset(vc4_hdmi);
E
Eric Anholt 已提交
740

741
	vc4_hdmi->audio.substream = NULL;
E
Eric Anholt 已提交
742 743 744 745 746 747 748
}

/* 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)
{
749 750
	struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
E
Eric Anholt 已提交
751
	struct drm_device *drm = encoder->dev;
752
	struct device *dev = &vc4_hdmi->pdev->dev;
E
Eric Anholt 已提交
753 754 755 756
	struct vc4_dev *vc4 = to_vc4_dev(drm);
	u32 audio_packet_config, channel_mask;
	u32 channel_map, i;

757
	if (substream != vc4_hdmi->audio.substream)
E
Eric Anholt 已提交
758 759 760 761 762 763
		return -EINVAL;

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

764 765
	vc4_hdmi->audio.channels = params_channels(params);
	vc4_hdmi->audio.samplerate = params_rate(params);
E
Eric Anholt 已提交
766 767 768 769 770 771 772 773

	HD_WRITE(VC4_HD_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);

774
	vc4_hdmi_audio_set_mai_clock(vc4_hdmi);
E
Eric Anholt 已提交
775 776 777 778 779 780

	audio_packet_config =
		VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
		VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
		VC4_SET_FIELD(0xf, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);

781
	channel_mask = GENMASK(vc4_hdmi->audio.channels - 1, 0);
E
Eric Anholt 已提交
782 783 784 785
	audio_packet_config |= VC4_SET_FIELD(channel_mask,
					     VC4_HDMI_AUDIO_PACKET_CEA_MASK);

	/* Set the MAI threshold.  This logic mimics the firmware's. */
786
	if (vc4_hdmi->audio.samplerate > 96000) {
E
Eric Anholt 已提交
787 788 789
		HD_WRITE(VC4_HD_MAI_THR,
			 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) |
			 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
790
	} else if (vc4_hdmi->audio.samplerate > 48000) {
E
Eric Anholt 已提交
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
		HD_WRITE(VC4_HD_MAI_THR,
			 VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) |
			 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
	} else {
		HD_WRITE(VC4_HD_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));
	}

	HDMI_WRITE(VC4_HDMI_MAI_CONFIG,
		   VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
		   VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));

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

	HDMI_WRITE(VC4_HDMI_MAI_CHANNEL_MAP, channel_map);
	HDMI_WRITE(VC4_HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
814
	vc4_hdmi_set_n_cts(vc4_hdmi);
E
Eric Anholt 已提交
815 816 817 818 819 820 821

	return 0;
}

static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
				  struct snd_soc_dai *dai)
{
822 823
	struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
	struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
E
Eric Anholt 已提交
824 825 826 827 828 829 830 831 832 833
	struct drm_device *drm = encoder->dev;
	struct vc4_dev *vc4 = to_vc4_dev(drm);

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		vc4_hdmi_set_audio_infoframe(encoder);
		HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
			   HDMI_READ(VC4_HDMI_TX_PHY_CTL0) &
			   ~VC4_HDMI_TX_PHY_RNG_PWRDN);
		HD_WRITE(VC4_HD_MAI_CTL,
834
			 VC4_SET_FIELD(vc4_hdmi->audio.channels,
E
Eric Anholt 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
				       VC4_HD_MAI_CTL_CHNUM) |
			 VC4_HD_MAI_CTL_ENABLE);
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		HD_WRITE(VC4_HD_MAI_CTL,
			 VC4_HD_MAI_CTL_DLATE |
			 VC4_HD_MAI_CTL_ERRORE |
			 VC4_HD_MAI_CTL_ERRORF);
		HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
			   HDMI_READ(VC4_HDMI_TX_PHY_CTL0) |
			   VC4_HDMI_TX_PHY_RNG_PWRDN);
		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);
866 867
	struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
	struct drm_connector *connector = &vc4_hdmi->connector.base;
E
Eric Anholt 已提交
868 869

	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
870
	uinfo->count = sizeof(connector->eld);
E
Eric Anholt 已提交
871 872 873 874 875 876 877 878

	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);
879 880
	struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
	struct drm_connector *connector = &vc4_hdmi->connector.base;
E
Eric Anholt 已提交
881

882 883
	memcpy(ucontrol->value.bytes.data, connector->eld,
	       sizeof(connector->eld));
E
Eric Anholt 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

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

907 908 909 910 911 912 913 914 915 916 917
static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = {
	.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 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
};

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

950
	snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
E
Eric Anholt 已提交
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975

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

976
static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
E
Eric Anholt 已提交
977
{
978 979 980
	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 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
	const __be32 *addr;
	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;
	}

	/*
	 * 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.
	 */
	addr = of_get_address(dev->of_node, 1, NULL, NULL);
998 999 1000
	vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + VC4_HD_MAI_DATA;
	vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
	vc4_hdmi->audio.dma_data.maxburst = 2;
E
Eric Anholt 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

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

1015 1016
	/* register component and codec dai */
	ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv,
E
Eric Anholt 已提交
1017 1018
				     &vc4_hdmi_audio_codec_dai_drv, 1);
	if (ret) {
1019
		dev_err(dev, "Could not register component: %d\n", ret);
E
Eric Anholt 已提交
1020 1021 1022
		return ret;
	}

1023 1024 1025
	dai_link->cpus		= &vc4_hdmi->audio.cpu;
	dai_link->codecs	= &vc4_hdmi->audio.codec;
	dai_link->platforms	= &vc4_hdmi->audio.platform;
1026 1027 1028

	dai_link->num_cpus	= 1;
	dai_link->num_codecs	= 1;
1029
	dai_link->num_platforms	= 1;
1030

E
Eric Anholt 已提交
1031 1032
	dai_link->name = "MAI";
	dai_link->stream_name = "MAI PCM";
1033 1034 1035
	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);
1036
	dai_link->platforms->name = dev_name(dev);
E
Eric Anholt 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

	card->dai_link = dai_link;
	card->num_links = 1;
	card->name = "vc4-hdmi";
	card->dev = dev;

	/*
	 * 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.
	 */
1050
	snd_soc_card_set_drvdata(card, vc4_hdmi);
E
Eric Anholt 已提交
1051
	ret = devm_snd_soc_register_card(dev, card);
1052
	if (ret)
E
Eric Anholt 已提交
1053 1054 1055 1056 1057 1058
		dev_err(dev, "Could not register sound card: %d\n", ret);

	return ret;

}

H
Hans Verkuil 已提交
1059 1060 1061 1062
#ifdef CONFIG_DRM_VC4_HDMI_CEC
static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
{
	struct vc4_dev *vc4 = priv;
1063 1064 1065 1066 1067 1068 1069 1070
	struct vc4_hdmi *vc4_hdmi = vc4->hdmi;

	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 已提交
1071 1072 1073 1074 1075 1076
				  0, 0, 0, 0);
	} else {
		/*
		 * This CEC implementation makes 1 retry, so if we
		 * get a NACK, then that means it made 2 attempts.
		 */
1077
		cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
H
Hans Verkuil 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
				  0, 2, 0, 0);
	}
	return IRQ_HANDLED;
}

static void vc4_cec_read_msg(struct vc4_dev *vc4, u32 cntrl1)
{
	struct cec_msg *msg = &vc4->hdmi->cec_rx_msg;
	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) {
		u32 val = HDMI_READ(VC4_HDMI_CEC_RX_DATA_1 + i);

		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)
{
	struct vc4_dev *vc4 = priv;
1103
	struct vc4_hdmi *vc4_hdmi = vc4->hdmi;
H
Hans Verkuil 已提交
1104 1105 1106 1107 1108
	u32 stat = HDMI_READ(VC4_HDMI_CPU_STATUS);
	u32 cntrl1, cntrl5;

	if (!(stat & VC4_HDMI_CPU_CEC))
		return IRQ_NONE;
1109
	vc4_hdmi->cec_rx_msg.len = 0;
H
Hans Verkuil 已提交
1110 1111
	cntrl1 = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
	cntrl5 = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
1112 1113
	vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
	if (vc4_hdmi->cec_irq_was_rx) {
H
Hans Verkuil 已提交
1114 1115 1116 1117 1118
		vc4_cec_read_msg(vc4, cntrl1);
		cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
		HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
		cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
	} else {
1119
		vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
H
Hans Verkuil 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
		cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
	}
	HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
	HDMI_WRITE(VC4_HDMI_CPU_CLEAR, VC4_HDMI_CPU_CEC);

	return IRQ_WAKE_THREAD;
}

static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
{
	struct vc4_dev *vc4 = cec_get_drvdata(adap);
	/* clock period in microseconds */
	const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
	u32 val = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);

	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) {
		HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
			   VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
		HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val);
		HDMI_WRITE(VC4_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(VC4_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(VC4_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(VC4_HDMI_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
	} else {
		HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
		HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
			   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)
{
	struct vc4_dev *vc4 = cec_get_drvdata(adap);

	HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1,
		   (HDMI_READ(VC4_HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
		   (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)
{
	struct vc4_dev *vc4 = cec_get_drvdata(adap);
	u32 val;
	unsigned int i;

	for (i = 0; i < msg->len; i += 4)
		HDMI_WRITE(VC4_HDMI_CEC_TX_DATA_1 + i,
			   (msg->msg[i]) |
			   (msg->msg[i + 1] << 8) |
			   (msg->msg[i + 2] << 16) |
			   (msg->msg[i + 3] << 24));

	val = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
	val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
	HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
	val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
	val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
	val |= VC4_HDMI_CEC_START_XMIT_BEGIN;

	HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
	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,
};
#endif

1213 1214
static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
{
1215 1216 1217
#ifdef CONFIG_DRM_VC4_HDMI_CEC
	struct cec_connector_info conn_info;
#endif
1218 1219 1220
	struct platform_device *pdev = to_platform_device(dev);
	struct drm_device *drm = dev_get_drvdata(master);
	struct vc4_dev *vc4 = drm->dev_private;
1221
	struct vc4_hdmi *vc4_hdmi;
1222
	struct drm_encoder *encoder;
1223 1224 1225 1226
	struct device_node *ddc_node;
	u32 value;
	int ret;

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

1231 1232 1233
	encoder = &vc4_hdmi->encoder.base.base;
	vc4_hdmi->encoder.base.type = VC4_ENCODER_TYPE_HDMI0;
	vc4_hdmi->pdev = pdev;
1234

1235 1236 1237
	vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
	if (IS_ERR(vc4_hdmi->hdmicore_regs))
		return PTR_ERR(vc4_hdmi->hdmicore_regs);
1238

1239 1240 1241
	vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
	if (IS_ERR(vc4_hdmi->hd_regs))
		return PTR_ERR(vc4_hdmi->hd_regs);
1242

1243 1244 1245 1246 1247 1248
	vc4_hdmi->hdmi_regset.base = vc4_hdmi->hdmicore_regs;
	vc4_hdmi->hdmi_regset.regs = hdmi_regs;
	vc4_hdmi->hdmi_regset.nregs = ARRAY_SIZE(hdmi_regs);
	vc4_hdmi->hd_regset.base = vc4_hdmi->hd_regs;
	vc4_hdmi->hd_regset.regs = hd_regs;
	vc4_hdmi->hd_regset.nregs = ARRAY_SIZE(hd_regs);
1249

1250 1251 1252
	vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
	if (IS_ERR(vc4_hdmi->pixel_clock)) {
		ret = PTR_ERR(vc4_hdmi->pixel_clock);
1253 1254 1255
		if (ret != -EPROBE_DEFER)
			DRM_ERROR("Failed to get pixel clock\n");
		return ret;
1256
	}
1257 1258
	vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
	if (IS_ERR(vc4_hdmi->hsm_clock)) {
1259
		DRM_ERROR("Failed to get HDMI state machine clock\n");
1260
		return PTR_ERR(vc4_hdmi->hsm_clock);
1261 1262
	}

1263 1264 1265 1266 1267 1268
	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;
	}

1269
	vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
1270
	of_node_put(ddc_node);
1271
	if (!vc4_hdmi->ddc) {
1272 1273 1274 1275
		DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
		return -EPROBE_DEFER;
	}

H
Hans Verkuil 已提交
1276 1277 1278 1279
	/* This is the rate that is set by the firmware.  The number
	 * needs to be a bit higher than the pixel clock rate
	 * (generally 148.5Mhz).
	 */
1280
	ret = clk_set_rate(vc4_hdmi->hsm_clock, HSM_CLOCK_FREQ);
H
Hans Verkuil 已提交
1281 1282 1283 1284 1285
	if (ret) {
		DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
		goto err_put_i2c;
	}

1286
	ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
H
Hans Verkuil 已提交
1287 1288 1289 1290 1291 1292
	if (ret) {
		DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
			  ret);
		goto err_put_i2c;
	}

1293 1294 1295 1296
	/* 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)) {
1297 1298
		enum of_gpio_flags hpd_gpio_flags;

1299 1300 1301 1302 1303
		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 已提交
1304
			goto err_unprepare_hsm;
1305
		}
1306

1307
		vc4_hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
1308 1309
	}

1310
	vc4->hdmi = vc4_hdmi;
1311

H
Hans Verkuil 已提交
1312 1313 1314 1315 1316 1317 1318 1319
	/* HDMI core must be enabled. */
	if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
		HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
		udelay(1);
		HD_WRITE(VC4_HD_M_CTL, 0);

		HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
	}
1320
	pm_runtime_enable(dev);
1321

1322 1323
	drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
	drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
1324

1325
	ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
1326
	if (ret)
1327
		goto err_destroy_encoder;
1328

H
Hans Verkuil 已提交
1329
#ifdef CONFIG_DRM_VC4_HDMI_CEC
1330 1331 1332 1333 1334
	vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
						  vc4, "vc4",
						  CEC_CAP_DEFAULTS |
						  CEC_CAP_CONNECTOR_INFO, 1);
	ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
H
Hans Verkuil 已提交
1335 1336
	if (ret < 0)
		goto err_destroy_conn;
1337

1338 1339
	cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector.base);
	cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
1340

H
Hans Verkuil 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
	HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, 0xffffffff);
	value = HDMI_READ(VC4_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(VC4_HDMI_CEC_CNTRL_1, value);
	ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
					vc4_cec_irq_handler,
					vc4_cec_irq_handler_thread, 0,
					"vc4 hdmi cec", vc4);
	if (ret)
		goto err_delete_cec_adap;
1358
	ret = cec_register_adapter(vc4_hdmi->cec_adap, dev);
H
Hans Verkuil 已提交
1359 1360 1361
	if (ret < 0)
		goto err_delete_cec_adap;
#endif
1362

1363
	ret = vc4_hdmi_audio_init(vc4_hdmi);
E
Eric Anholt 已提交
1364 1365 1366
	if (ret)
		goto err_destroy_encoder;

1367
	vc4_debugfs_add_file(drm, "hdmi_regs", vc4_hdmi_debugfs_regs, vc4_hdmi);
1368

1369 1370
	return 0;

H
Hans Verkuil 已提交
1371 1372
#ifdef CONFIG_DRM_VC4_HDMI_CEC
err_delete_cec_adap:
1373
	cec_delete_adapter(vc4_hdmi->cec_adap);
H
Hans Verkuil 已提交
1374
err_destroy_conn:
1375
	vc4_hdmi_connector_destroy(&vc4_hdmi->connector.base);
H
Hans Verkuil 已提交
1376
#endif
1377
err_destroy_encoder:
1378
	drm_encoder_cleanup(encoder);
H
Hans Verkuil 已提交
1379
err_unprepare_hsm:
1380
	clk_disable_unprepare(vc4_hdmi->hsm_clock);
1381
	pm_runtime_disable(dev);
1382
err_put_i2c:
1383
	put_device(&vc4_hdmi->ddc->dev);
1384 1385 1386 1387 1388 1389 1390 1391 1392

	return ret;
}

static void vc4_hdmi_unbind(struct device *dev, struct device *master,
			    void *data)
{
	struct drm_device *drm = dev_get_drvdata(master);
	struct vc4_dev *vc4 = drm->dev_private;
1393
	struct vc4_hdmi *vc4_hdmi = vc4->hdmi;
1394

1395 1396 1397
	cec_unregister_adapter(vc4_hdmi->cec_adap);
	vc4_hdmi_connector_destroy(&vc4_hdmi->connector.base);
	drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
1398

1399
	clk_disable_unprepare(vc4_hdmi->hsm_clock);
1400 1401
	pm_runtime_disable(dev);

1402
	put_device(&vc4_hdmi->ddc->dev);
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435

	vc4->hdmi = NULL;
}

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

static const struct of_device_id vc4_hdmi_dt_match[] = {
	{ .compatible = "brcm,bcm2835-hdmi" },
	{}
};

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