1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2015 Broadcom 4 * Copyright (c) 2014 The Linux Foundation. All rights reserved. 5 * Copyright (C) 2013 Red Hat 6 * Author: Rob Clark <robdclark@gmail.com> 7 */ 8 9 /** 10 * DOC: VC4 Falcon HDMI module 11 * 12 * The HDMI core has a state machine and a PHY. On BCM2835, most of 13 * the unit operates off of the HSM clock from CPRMAN. It also 14 * internally uses the PLLH_PIX clock for the PHY. 15 * 16 * HDMI infoframes are kept within a small packet ram, where each 17 * packet can be individually enabled for including in a frame. 18 * 19 * HDMI audio is implemented entirely within the HDMI IP block. A 20 * register in the HDMI encoder takes SPDIF frames from the DMA engine 21 * and transfers them over an internal MAI (multi-channel audio 22 * interconnect) bus to the encoder side for insertion into the video 23 * blank regions. 24 * 25 * The driver's HDMI encoder does not yet support power management. 26 * The HDMI encoder's power domain and the HSM/pixel clocks are kept 27 * continuously running, and only the HDMI logic and packet ram are 28 * powered off/on at disable/enable time. 29 * 30 * The driver does not yet support CEC control, though the HDMI 31 * encoder block has CEC support. 32 */ 33 34 #include <drm/display/drm_hdmi_audio_helper.h> 35 #include <drm/display/drm_hdmi_cec_helper.h> 36 #include <drm/display/drm_hdmi_helper.h> 37 #include <drm/display/drm_hdmi_state_helper.h> 38 #include <drm/display/drm_scdc_helper.h> 39 #include <drm/drm_atomic_helper.h> 40 #include <drm/drm_drv.h> 41 #include <drm/drm_edid.h> 42 #include <drm/drm_print.h> 43 #include <drm/drm_probe_helper.h> 44 #include <drm/drm_simple_kms_helper.h> 45 #include <linux/clk.h> 46 #include <linux/component.h> 47 #include <linux/gpio/consumer.h> 48 #include <linux/i2c.h> 49 #include <linux/of.h> 50 #include <linux/of_address.h> 51 #include <linux/pm_runtime.h> 52 #include <linux/rational.h> 53 #include <linux/reset.h> 54 #include <sound/dmaengine_pcm.h> 55 #include <sound/hdmi-codec.h> 56 #include <sound/jack.h> 57 #include <sound/pcm_drm_eld.h> 58 #include <sound/pcm_params.h> 59 #include <sound/soc.h> 60 #include "media/cec.h" 61 #include "vc4_drv.h" 62 #include "vc4_hdmi.h" 63 #include "vc4_hdmi_regs.h" 64 #include "vc4_regs.h" 65 66 #define VC5_HDMI_HORZA_HFP_SHIFT 16 67 #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16) 68 #define VC5_HDMI_HORZA_VPOS BIT(15) 69 #define VC5_HDMI_HORZA_HPOS BIT(14) 70 #define VC5_HDMI_HORZA_HAP_SHIFT 0 71 #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0) 72 73 #define VC5_HDMI_HORZB_HBP_SHIFT 16 74 #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16) 75 #define VC5_HDMI_HORZB_HSP_SHIFT 0 76 #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0) 77 78 #define VC5_HDMI_VERTA_VSP_SHIFT 24 79 #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24) 80 #define VC5_HDMI_VERTA_VFP_SHIFT 16 81 #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16) 82 #define VC5_HDMI_VERTA_VAL_SHIFT 0 83 #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0) 84 85 #define VC5_HDMI_VERTB_VSPO_SHIFT 16 86 #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16) 87 88 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0 89 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0) 90 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0 91 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0) 92 93 #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0) 94 95 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8 96 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8) 97 98 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0 99 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0) 100 101 #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31) 102 103 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8 104 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8) 105 106 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK VC4_MASK(7, 0) 107 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_SET_AVMUTE BIT(0) 108 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE BIT(4) 109 110 # define VC4_HD_M_SW_RST BIT(2) 111 # define VC4_HD_M_ENABLE BIT(0) 112 113 #define HSM_MIN_CLOCK_FREQ 120000000 114 #define CEC_CLOCK_FREQ 40000 115 116 #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000) 117 118 static bool vc4_hdmi_supports_scrambling(struct vc4_hdmi *vc4_hdmi) 119 { 120 struct drm_display_info *display = &vc4_hdmi->connector.display_info; 121 122 lockdep_assert_held(&vc4_hdmi->mutex); 123 124 if (!display->is_hdmi) 125 return false; 126 127 if (!display->hdmi.scdc.supported || 128 !display->hdmi.scdc.scrambling.supported) 129 return false; 130 131 return true; 132 } 133 134 static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode, 135 unsigned int bpc, 136 enum drm_output_color_format fmt) 137 { 138 unsigned long long clock = drm_hdmi_compute_mode_clock(mode, bpc, fmt); 139 140 return clock > HDMI_14_MAX_TMDS_CLK; 141 } 142 143 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused) 144 { 145 struct drm_debugfs_entry *entry = m->private; 146 struct vc4_hdmi *vc4_hdmi = entry->file.data; 147 struct drm_device *drm = vc4_hdmi->connector.dev; 148 struct drm_printer p = drm_seq_file_printer(m); 149 int idx; 150 151 if (!drm_dev_enter(drm, &idx)) 152 return -ENODEV; 153 154 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev)); 155 156 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset); 157 drm_print_regset32(&p, &vc4_hdmi->hd_regset); 158 drm_print_regset32(&p, &vc4_hdmi->cec_regset); 159 drm_print_regset32(&p, &vc4_hdmi->csc_regset); 160 drm_print_regset32(&p, &vc4_hdmi->dvp_regset); 161 drm_print_regset32(&p, &vc4_hdmi->phy_regset); 162 drm_print_regset32(&p, &vc4_hdmi->ram_regset); 163 drm_print_regset32(&p, &vc4_hdmi->rm_regset); 164 165 pm_runtime_put(&vc4_hdmi->pdev->dev); 166 167 drm_dev_exit(idx); 168 169 return 0; 170 } 171 172 static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi) 173 { 174 struct drm_device *drm = vc4_hdmi->connector.dev; 175 unsigned long flags; 176 int idx; 177 178 /* 179 * We can be called by our bind callback, when the 180 * connector->dev pointer might not be initialised yet. 181 */ 182 if (drm && !drm_dev_enter(drm, &idx)) 183 return; 184 185 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 186 187 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST); 188 udelay(1); 189 HDMI_WRITE(HDMI_M_CTL, 0); 190 191 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE); 192 193 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 194 VC4_HDMI_SW_RESET_HDMI | 195 VC4_HDMI_SW_RESET_FORMAT_DETECT); 196 197 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0); 198 199 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 200 201 if (drm) 202 drm_dev_exit(idx); 203 } 204 205 static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi) 206 { 207 struct drm_device *drm = vc4_hdmi->connector.dev; 208 unsigned long flags; 209 int idx; 210 211 /* 212 * We can be called by our bind callback, when the 213 * connector->dev pointer might not be initialised yet. 214 */ 215 if (drm && !drm_dev_enter(drm, &idx)) 216 return; 217 218 reset_control_reset(vc4_hdmi->reset); 219 220 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 221 222 HDMI_WRITE(HDMI_DVP_CTL, 0); 223 224 HDMI_WRITE(HDMI_CLOCK_STOP, 225 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL); 226 227 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 228 229 if (drm) 230 drm_dev_exit(idx); 231 } 232 233 #ifdef CONFIG_DRM_VC4_HDMI_CEC 234 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) 235 { 236 struct drm_device *drm = vc4_hdmi->connector.dev; 237 unsigned long cec_rate; 238 unsigned long flags; 239 u16 clk_cnt; 240 u32 value; 241 int idx; 242 243 /* 244 * This function is called by our runtime_resume implementation 245 * and thus at bind time, when we haven't registered our 246 * connector yet and thus don't have a pointer to the DRM 247 * device. 248 */ 249 if (drm && !drm_dev_enter(drm, &idx)) 250 return; 251 252 cec_rate = clk_get_rate(vc4_hdmi->cec_clock); 253 254 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 255 256 value = HDMI_READ(HDMI_CEC_CNTRL_1); 257 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK; 258 259 /* 260 * Set the clock divider: the hsm_clock rate and this divider 261 * setting will give a 40 kHz CEC clock. 262 */ 263 clk_cnt = cec_rate / CEC_CLOCK_FREQ; 264 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT; 265 HDMI_WRITE(HDMI_CEC_CNTRL_1, value); 266 267 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 268 269 if (drm) 270 drm_dev_exit(idx); 271 } 272 #else 273 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {} 274 #endif 275 276 static int vc4_hdmi_reset_link(struct drm_connector *connector, 277 struct drm_modeset_acquire_ctx *ctx) 278 { 279 struct drm_device *drm; 280 struct vc4_hdmi *vc4_hdmi; 281 struct drm_connector_state *conn_state; 282 struct drm_crtc_state *crtc_state; 283 struct drm_crtc *crtc; 284 bool scrambling_needed; 285 u8 config; 286 int ret; 287 288 if (!connector) 289 return 0; 290 291 drm = connector->dev; 292 ret = drm_modeset_lock(&drm->mode_config.connection_mutex, ctx); 293 if (ret) 294 return ret; 295 296 conn_state = connector->state; 297 crtc = conn_state->crtc; 298 if (!crtc) 299 return 0; 300 301 ret = drm_modeset_lock(&crtc->mutex, ctx); 302 if (ret) 303 return ret; 304 305 crtc_state = crtc->state; 306 if (!crtc_state->active) 307 return 0; 308 309 vc4_hdmi = connector_to_vc4_hdmi(connector); 310 mutex_lock(&vc4_hdmi->mutex); 311 312 if (!vc4_hdmi_supports_scrambling(vc4_hdmi)) { 313 mutex_unlock(&vc4_hdmi->mutex); 314 return 0; 315 } 316 317 scrambling_needed = vc4_hdmi_mode_needs_scrambling(&vc4_hdmi->saved_adjusted_mode, 318 vc4_hdmi->output_bpc, 319 vc4_hdmi->output_format); 320 if (!scrambling_needed) { 321 mutex_unlock(&vc4_hdmi->mutex); 322 return 0; 323 } 324 325 if (conn_state->commit && 326 !try_wait_for_completion(&conn_state->commit->hw_done)) { 327 mutex_unlock(&vc4_hdmi->mutex); 328 return 0; 329 } 330 331 ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config); 332 if (ret < 0) { 333 drm_err(drm, "Failed to read TMDS config: %d\n", ret); 334 mutex_unlock(&vc4_hdmi->mutex); 335 return 0; 336 } 337 338 if (!!(config & SCDC_SCRAMBLING_ENABLE) == scrambling_needed) { 339 mutex_unlock(&vc4_hdmi->mutex); 340 return 0; 341 } 342 343 mutex_unlock(&vc4_hdmi->mutex); 344 345 /* 346 * HDMI 2.0 says that one should not send scrambled data 347 * prior to configuring the sink scrambling, and that 348 * TMDS clock/data transmission should be suspended when 349 * changing the TMDS clock rate in the sink. So let's 350 * just do a full modeset here, even though some sinks 351 * would be perfectly happy if were to just reconfigure 352 * the SCDC settings on the fly. 353 */ 354 return drm_atomic_helper_reset_crtc(crtc, ctx); 355 } 356 357 static void vc4_hdmi_handle_hotplug(struct vc4_hdmi *vc4_hdmi, 358 struct drm_modeset_acquire_ctx *ctx, 359 enum drm_connector_status status) 360 { 361 struct drm_connector *connector = &vc4_hdmi->connector; 362 int ret; 363 364 /* 365 * NOTE: This function should really be called with vc4_hdmi->mutex 366 * held, but doing so results in reentrancy issues since 367 * cec_s_phys_addr() might call .adap_enable, which leads to that 368 * funtion being called with our mutex held. 369 * 370 * A similar situation occurs with vc4_hdmi_reset_link() that 371 * will call into our KMS hooks if the scrambling was enabled. 372 * 373 * Concurrency isn't an issue at the moment since we don't share 374 * any state with any of the other frameworks so we can ignore 375 * the lock for now. 376 */ 377 378 drm_atomic_helper_connector_hdmi_hotplug(connector, ctx, status); 379 380 if (status != connector_status_connected) 381 return; 382 383 for (;;) { 384 ret = vc4_hdmi_reset_link(connector, ctx); 385 if (ret == -EDEADLK) { 386 drm_modeset_backoff(ctx); 387 continue; 388 } 389 390 break; 391 } 392 } 393 394 static int vc4_hdmi_connector_detect_ctx(struct drm_connector *connector, 395 struct drm_modeset_acquire_ctx *ctx, 396 bool force) 397 { 398 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 399 enum drm_connector_status status = connector_status_disconnected; 400 int ret; 401 402 /* 403 * NOTE: This function should really take vc4_hdmi->mutex, but 404 * doing so results in reentrancy issues since 405 * vc4_hdmi_handle_hotplug() can call into other functions that 406 * would take the mutex while it's held here. 407 * 408 * Concurrency isn't an issue at the moment since we don't share 409 * any state with any of the other frameworks so we can ignore 410 * the lock for now. 411 */ 412 413 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev); 414 if (ret) { 415 drm_err_once(connector->dev, "Failed to retain HDMI power domain: %d\n", 416 ret); 417 return connector_status_unknown; 418 } 419 420 if (vc4_hdmi->hpd_gpio) { 421 if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio)) 422 status = connector_status_connected; 423 } else { 424 if (vc4_hdmi->variant->hp_detect && 425 vc4_hdmi->variant->hp_detect(vc4_hdmi)) 426 status = connector_status_connected; 427 } 428 429 vc4_hdmi_handle_hotplug(vc4_hdmi, ctx, status); 430 pm_runtime_put(&vc4_hdmi->pdev->dev); 431 432 return status; 433 } 434 435 static int vc4_hdmi_connector_get_modes(struct drm_connector *connector) 436 { 437 struct vc4_dev *vc4 = to_vc4_dev(connector->dev); 438 int ret = 0; 439 440 ret = drm_edid_connector_add_modes(connector); 441 442 if (!vc4->hvs->vc5_hdmi_enable_hdmi_20) { 443 struct drm_device *drm = connector->dev; 444 const struct drm_display_mode *mode; 445 446 list_for_each_entry(mode, &connector->probed_modes, head) { 447 if (vc4_hdmi_mode_needs_scrambling(mode, 8, DRM_OUTPUT_COLOR_FORMAT_RGB444)) { 448 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz."); 449 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60."); 450 } 451 } 452 } 453 454 return ret; 455 } 456 457 static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector, 458 struct drm_atomic_commit *state) 459 { 460 struct drm_connector_state *old_state = 461 drm_atomic_get_old_connector_state(state, connector); 462 struct drm_connector_state *new_state = 463 drm_atomic_get_new_connector_state(state, connector); 464 struct drm_crtc *crtc = new_state->crtc; 465 466 if (!crtc) 467 return 0; 468 469 if (old_state->tv.margins.left != new_state->tv.margins.left || 470 old_state->tv.margins.right != new_state->tv.margins.right || 471 old_state->tv.margins.top != new_state->tv.margins.top || 472 old_state->tv.margins.bottom != new_state->tv.margins.bottom) { 473 struct drm_crtc_state *crtc_state; 474 int ret; 475 476 crtc_state = drm_atomic_get_crtc_state(state, crtc); 477 if (IS_ERR(crtc_state)) 478 return PTR_ERR(crtc_state); 479 480 /* 481 * Strictly speaking, we should be calling 482 * drm_atomic_helper_check_planes() after our call to 483 * drm_atomic_add_affected_planes(). However, the 484 * connector atomic_check is called as part of 485 * drm_atomic_helper_check_modeset() that already 486 * happens before a call to 487 * drm_atomic_helper_check_planes() in 488 * drm_atomic_helper_check(). 489 */ 490 ret = drm_atomic_add_affected_planes(state, crtc); 491 if (ret) 492 return ret; 493 } 494 495 if (old_state->colorspace != new_state->colorspace) { 496 struct drm_crtc_state *crtc_state; 497 498 crtc_state = drm_atomic_get_crtc_state(state, crtc); 499 if (IS_ERR(crtc_state)) 500 return PTR_ERR(crtc_state); 501 502 crtc_state->mode_changed = true; 503 } 504 505 return drm_atomic_helper_connector_hdmi_check(connector, state); 506 } 507 508 static void vc4_hdmi_connector_reset(struct drm_connector *connector) 509 { 510 drm_atomic_helper_connector_reset(connector); 511 __drm_atomic_helper_connector_hdmi_state_init(connector, connector->state); 512 drm_atomic_helper_connector_tv_margins_reset(connector); 513 } 514 515 static const struct drm_connector_funcs vc4_hdmi_connector_funcs = { 516 .debugfs_init = drm_hdmi_connector_debugfs_init, 517 .force = drm_atomic_helper_connector_hdmi_force, 518 .fill_modes = drm_helper_probe_single_connector_modes, 519 .reset = vc4_hdmi_connector_reset, 520 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 521 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 522 }; 523 524 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = { 525 .detect_ctx = vc4_hdmi_connector_detect_ctx, 526 .get_modes = vc4_hdmi_connector_get_modes, 527 .atomic_check = vc4_hdmi_connector_atomic_check, 528 .mode_valid = drm_hdmi_connector_mode_valid, 529 }; 530 531 static const struct drm_connector_hdmi_funcs vc4_hdmi_hdmi_connector_funcs; 532 static const struct drm_connector_hdmi_audio_funcs vc4_hdmi_audio_funcs; 533 534 static int vc4_hdmi_connector_init(struct drm_device *dev, 535 struct vc4_hdmi *vc4_hdmi) 536 { 537 struct drm_connector *connector = &vc4_hdmi->connector; 538 struct drm_encoder *encoder = &vc4_hdmi->encoder.base; 539 unsigned int max_bpc = 8; 540 int ret; 541 542 if (vc4_hdmi->variant->supports_hdr) 543 max_bpc = 12; 544 545 ret = drmm_connector_hdmi_ini2(dev, connector, 546 "Broadcom", "Videocore", 547 &vc4_hdmi_connector_funcs, 548 &vc4_hdmi_hdmi_connector_funcs, 549 DRM_MODE_CONNECTOR_HDMIA, 550 vc4_hdmi->ddc, 551 BIT(DRM_OUTPUT_COLOR_FORMAT_RGB444) | 552 BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR422) | 553 BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR444), 554 max_bpc); 555 if (ret) 556 return ret; 557 558 ret = drm_connector_hdmi_audio_init(connector, dev->dev, 559 &vc4_hdmi_audio_funcs, 560 8, 0, false, -1); 561 if (ret) 562 return ret; 563 564 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs); 565 566 /* 567 * Some of the properties below require access to state, like bpc. 568 * Allocate some default initial connector state with our reset helper. 569 */ 570 if (connector->funcs->reset) 571 connector->funcs->reset(connector); 572 573 /* Create and attach TV margin props to this connector. */ 574 ret = drm_mode_create_tv_margin_properties(dev); 575 if (ret) 576 return ret; 577 578 ret = drm_mode_create_hdmi_colorspace_property(connector, 0); 579 if (ret) 580 return ret; 581 582 drm_connector_attach_colorspace_property(connector); 583 drm_connector_attach_tv_margin_properties(connector); 584 585 connector->polled = (DRM_CONNECTOR_POLL_CONNECT | 586 DRM_CONNECTOR_POLL_DISCONNECT); 587 588 connector->interlace_allowed = 1; 589 connector->doublescan_allowed = 0; 590 connector->stereo_allowed = 1; 591 592 ret = drm_connector_attach_broadcast_rgb_property(connector); 593 if (ret) 594 return ret; 595 596 drm_connector_attach_encoder(connector, encoder); 597 598 return 0; 599 } 600 601 static int vc4_hdmi_stop_packet(struct vc4_hdmi *vc4_hdmi, 602 enum hdmi_infoframe_type type, 603 bool poll) 604 { 605 struct drm_device *drm = vc4_hdmi->connector.dev; 606 u32 packet_id = type - 0x80; 607 unsigned long flags; 608 int ret = 0; 609 int idx; 610 611 if (!drm_dev_enter(drm, &idx)) 612 return -ENODEV; 613 614 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 615 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 616 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id)); 617 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 618 619 if (poll) { 620 ret = wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) & 621 BIT(packet_id)), 100); 622 } 623 624 drm_dev_exit(idx); 625 return ret; 626 } 627 628 static int vc4_hdmi_clear_infoframe(struct drm_connector *connector, 629 enum hdmi_infoframe_type type) 630 { 631 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 632 struct drm_device *drm = connector->dev; 633 int ret; 634 int idx; 635 636 if (!drm_dev_enter(drm, &idx)) 637 return 0; 638 639 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) & 640 VC4_HDMI_RAM_PACKET_ENABLE), 641 "Packet RAM has to be on to store the packet."); 642 643 ret = vc4_hdmi_stop_packet(vc4_hdmi, type, true); 644 if (ret) 645 drm_err(drm, "Failed to wait for infoframe to go idle: %d\n", ret); 646 647 drm_dev_exit(idx); 648 649 return ret; 650 } 651 652 static int vc4_hdmi_write_infoframe(struct drm_connector *connector, 653 enum hdmi_infoframe_type type, 654 const u8 *infoframe, size_t len) 655 { 656 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 657 struct drm_device *drm = connector->dev; 658 u32 packet_id = type - 0x80; 659 const struct vc4_hdmi_register *ram_packet_start = 660 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START]; 661 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id; 662 u32 packet_reg_next = ram_packet_start->offset + 663 VC4_HDMI_PACKET_STRIDE * (packet_id + 1); 664 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi, 665 ram_packet_start->reg); 666 uint8_t buffer[VC4_HDMI_PACKET_STRIDE] = {}; 667 unsigned long flags; 668 ssize_t i; 669 int ret; 670 int idx; 671 672 if (!drm_dev_enter(drm, &idx)) 673 return 0; 674 675 if (len > sizeof(buffer)) { 676 ret = -ENOMEM; 677 goto out; 678 } 679 680 memcpy(buffer, infoframe, len); 681 682 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) & 683 VC4_HDMI_RAM_PACKET_ENABLE), 684 "Packet RAM has to be on to store the packet."); 685 686 ret = vc4_hdmi_stop_packet(vc4_hdmi, type, true); 687 if (ret) { 688 drm_err(drm, "Failed to wait for infoframe to go idle: %d\n", ret); 689 goto out; 690 } 691 692 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 693 694 for (i = 0; i < len; i += 7) { 695 writel(buffer[i + 0] << 0 | 696 buffer[i + 1] << 8 | 697 buffer[i + 2] << 16, 698 base + packet_reg); 699 packet_reg += 4; 700 701 writel(buffer[i + 3] << 0 | 702 buffer[i + 4] << 8 | 703 buffer[i + 5] << 16 | 704 buffer[i + 6] << 24, 705 base + packet_reg); 706 packet_reg += 4; 707 } 708 709 /* 710 * clear remainder of packet ram as it's included in the 711 * infoframe and triggers a checksum error on hdmi analyser 712 */ 713 for (; packet_reg < packet_reg_next; packet_reg += 4) 714 writel(0, base + packet_reg); 715 716 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 717 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id)); 718 719 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 720 721 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) & 722 BIT(packet_id)), 100); 723 if (ret) 724 drm_err(drm, "Failed to wait for infoframe to start: %d\n", ret); 725 726 out: 727 drm_dev_exit(idx); 728 return ret; 729 } 730 731 static int vc4_hdmi_clear_avi_infoframe(struct drm_connector *connector) 732 { 733 return vc4_hdmi_clear_infoframe(connector, HDMI_INFOFRAME_TYPE_AVI); 734 } 735 736 static int vc4_hdmi_clear_hdmi_infoframe(struct drm_connector *connector) 737 { 738 return vc4_hdmi_clear_infoframe(connector, HDMI_INFOFRAME_TYPE_VENDOR); 739 } 740 741 static int vc4_hdmi_clear_audio_infoframe(struct drm_connector *connector) 742 { 743 return vc4_hdmi_clear_infoframe(connector, HDMI_INFOFRAME_TYPE_AUDIO); 744 } 745 746 static int vc4_hdmi_clear_hdr_drm_infoframe(struct drm_connector *connector) 747 { 748 return vc4_hdmi_clear_infoframe(connector, HDMI_INFOFRAME_TYPE_DRM); 749 } 750 751 static int vc4_hdmi_clear_spd_infoframe(struct drm_connector *connector) 752 { 753 return vc4_hdmi_clear_infoframe(connector, HDMI_INFOFRAME_TYPE_SPD); 754 } 755 756 static int vc4_hdmi_write_avi_infoframe(struct drm_connector *connector, 757 const u8 *buffer, size_t len) 758 { 759 return vc4_hdmi_write_infoframe(connector, HDMI_INFOFRAME_TYPE_AVI, 760 buffer, len); 761 } 762 763 static int vc4_hdmi_write_hdmi_infoframe(struct drm_connector *connector, 764 const u8 *buffer, size_t len) 765 { 766 return vc4_hdmi_write_infoframe(connector, HDMI_INFOFRAME_TYPE_VENDOR, 767 buffer, len); 768 } 769 770 static int vc4_hdmi_write_audio_infoframe(struct drm_connector *connector, 771 const u8 *buffer, size_t len) 772 { 773 return vc4_hdmi_write_infoframe(connector, HDMI_INFOFRAME_TYPE_AUDIO, 774 buffer, len); 775 } 776 777 static int vc4_hdmi_write_hdr_drm_infoframe(struct drm_connector *connector, 778 const u8 *buffer, size_t len) 779 { 780 return vc4_hdmi_write_infoframe(connector, HDMI_INFOFRAME_TYPE_DRM, 781 buffer, len); 782 } 783 784 static int vc4_hdmi_write_spd_infoframe(struct drm_connector *connector, 785 const u8 *buffer, size_t len) 786 { 787 return vc4_hdmi_write_infoframe(connector, HDMI_INFOFRAME_TYPE_SPD, 788 buffer, len); 789 } 790 791 #define SCRAMBLING_POLLING_DELAY_MS 1000 792 793 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder) 794 { 795 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 796 struct drm_connector *connector = &vc4_hdmi->connector; 797 struct drm_device *drm = connector->dev; 798 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode; 799 unsigned long flags; 800 int idx; 801 802 lockdep_assert_held(&vc4_hdmi->mutex); 803 804 if (!vc4_hdmi_supports_scrambling(vc4_hdmi)) 805 return; 806 807 if (!vc4_hdmi_mode_needs_scrambling(mode, 808 vc4_hdmi->output_bpc, 809 vc4_hdmi->output_format)) 810 return; 811 812 if (!drm_dev_enter(drm, &idx)) 813 return; 814 815 drm_scdc_set_high_tmds_clock_ratio(connector, true); 816 drm_scdc_set_scrambling(connector, true); 817 818 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 819 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) | 820 VC5_HDMI_SCRAMBLER_CTL_ENABLE); 821 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 822 823 drm_dev_exit(idx); 824 825 vc4_hdmi->scdc_enabled = true; 826 827 queue_delayed_work(system_percpu_wq, &vc4_hdmi->scrambling_work, 828 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS)); 829 } 830 831 static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder) 832 { 833 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 834 struct drm_connector *connector = &vc4_hdmi->connector; 835 struct drm_device *drm = connector->dev; 836 unsigned long flags; 837 int idx; 838 839 lockdep_assert_held(&vc4_hdmi->mutex); 840 841 if (!vc4_hdmi->scdc_enabled) 842 return; 843 844 vc4_hdmi->scdc_enabled = false; 845 846 if (delayed_work_pending(&vc4_hdmi->scrambling_work)) 847 cancel_delayed_work_sync(&vc4_hdmi->scrambling_work); 848 849 if (!drm_dev_enter(drm, &idx)) 850 return; 851 852 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 853 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) & 854 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE); 855 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 856 857 drm_scdc_set_scrambling(connector, false); 858 drm_scdc_set_high_tmds_clock_ratio(connector, false); 859 860 drm_dev_exit(idx); 861 } 862 863 static void vc4_hdmi_scrambling_wq(struct work_struct *work) 864 { 865 struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work), 866 struct vc4_hdmi, 867 scrambling_work); 868 struct drm_connector *connector = &vc4_hdmi->connector; 869 870 if (drm_scdc_get_scrambling_status(connector)) 871 return; 872 873 drm_scdc_set_high_tmds_clock_ratio(connector, true); 874 drm_scdc_set_scrambling(connector, true); 875 876 queue_delayed_work(system_percpu_wq, &vc4_hdmi->scrambling_work, 877 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS)); 878 } 879 880 static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder, 881 struct drm_atomic_commit *state) 882 { 883 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 884 struct drm_device *drm = vc4_hdmi->connector.dev; 885 struct vc4_dev *vc4 = to_vc4_dev(drm); 886 unsigned long flags; 887 int idx; 888 889 mutex_lock(&vc4_hdmi->mutex); 890 891 vc4_hdmi->packet_ram_enabled = false; 892 893 if (!drm_dev_enter(drm, &idx)) 894 goto out; 895 896 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 897 898 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0); 899 900 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB); 901 902 if (vc4->gen >= VC4_GEN_6_C) 903 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | 904 VC4_HD_VID_CTL_BLANKPIX); 905 906 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 907 908 mdelay(1); 909 910 /* 911 * TODO: This should work on BCM2712, but doesn't for some 912 * reason and result in a system lockup. 913 */ 914 if (vc4->gen < VC4_GEN_6_C) { 915 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 916 HDMI_WRITE(HDMI_VID_CTL, 917 HDMI_READ(HDMI_VID_CTL) & 918 ~VC4_HD_VID_CTL_ENABLE); 919 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 920 } 921 922 vc4_hdmi_disable_scrambling(encoder); 923 924 drm_dev_exit(idx); 925 926 out: 927 mutex_unlock(&vc4_hdmi->mutex); 928 } 929 930 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder, 931 struct drm_atomic_commit *state) 932 { 933 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 934 struct drm_device *drm = vc4_hdmi->connector.dev; 935 unsigned long flags; 936 int idx; 937 938 mutex_lock(&vc4_hdmi->mutex); 939 940 if (!drm_dev_enter(drm, &idx)) 941 goto out; 942 943 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 944 HDMI_WRITE(HDMI_VID_CTL, 945 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX); 946 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 947 948 if (vc4_hdmi->variant->phy_disable) 949 vc4_hdmi->variant->phy_disable(vc4_hdmi); 950 951 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock); 952 clk_disable_unprepare(vc4_hdmi->pixel_clock); 953 954 pm_runtime_put(&vc4_hdmi->pdev->dev); 955 956 drm_dev_exit(idx); 957 958 out: 959 mutex_unlock(&vc4_hdmi->mutex); 960 } 961 962 static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, 963 struct drm_connector_state *state, 964 const struct drm_display_mode *mode) 965 { 966 struct drm_device *drm = vc4_hdmi->connector.dev; 967 unsigned long flags; 968 u32 csc_ctl; 969 int idx; 970 971 if (!drm_dev_enter(drm, &idx)) 972 return; 973 974 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 975 976 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR, 977 VC4_HD_CSC_CTL_ORDER); 978 979 if (state->hdmi.is_limited_range) { 980 /* CEA VICs other than #1 requre limited range RGB 981 * output unless overridden by an AVI infoframe. 982 * Apply a colorspace conversion to squash 0-255 down 983 * to 16-235. The matrix here is: 984 * 985 * [ 0 0 0.8594 16] 986 * [ 0 0.8594 0 16] 987 * [ 0.8594 0 0 16] 988 * [ 0 0 0 1] 989 */ 990 csc_ctl |= VC4_HD_CSC_CTL_ENABLE; 991 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC; 992 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM, 993 VC4_HD_CSC_CTL_MODE); 994 995 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000); 996 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0); 997 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000); 998 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000); 999 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0); 1000 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000); 1001 } 1002 1003 /* The RGB order applies even when CSC is disabled. */ 1004 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl); 1005 1006 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1007 1008 drm_dev_exit(idx); 1009 } 1010 1011 /* 1012 * Matrices for (internal) RGB to RGB output. 1013 * 1014 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets 1015 */ 1016 static const u16 vc5_hdmi_csc_full_rgb_to_rgb[2][3][4] = { 1017 { 1018 /* 1019 * Full range - unity 1020 * 1021 * [ 1 0 0 0] 1022 * [ 0 1 0 0] 1023 * [ 0 0 1 0] 1024 */ 1025 { 0x2000, 0x0000, 0x0000, 0x0000 }, 1026 { 0x0000, 0x2000, 0x0000, 0x0000 }, 1027 { 0x0000, 0x0000, 0x2000, 0x0000 }, 1028 }, 1029 { 1030 /* 1031 * Limited range 1032 * 1033 * CEA VICs other than #1 require limited range RGB 1034 * output unless overridden by an AVI infoframe. Apply a 1035 * colorspace conversion to squash 0-255 down to 16-235. 1036 * The matrix here is: 1037 * 1038 * [ 0.8594 0 0 16] 1039 * [ 0 0.8594 0 16] 1040 * [ 0 0 0.8594 16] 1041 */ 1042 { 0x1b80, 0x0000, 0x0000, 0x0400 }, 1043 { 0x0000, 0x1b80, 0x0000, 0x0400 }, 1044 { 0x0000, 0x0000, 0x1b80, 0x0400 }, 1045 }, 1046 }; 1047 1048 /* 1049 * Conversion between Full Range RGB and YUV using the BT.601 Colorspace 1050 * 1051 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets 1052 */ 1053 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt601[2][3][4] = { 1054 { 1055 /* 1056 * Full Range 1057 * 1058 * [ 0.299000 0.587000 0.114000 0 ] 1059 * [ -0.168736 -0.331264 0.500000 128 ] 1060 * [ 0.500000 -0.418688 -0.081312 128 ] 1061 */ 1062 { 0x0991, 0x12c9, 0x03a6, 0x0000 }, 1063 { 0xfa9b, 0xf567, 0x1000, 0x2000 }, 1064 { 0x1000, 0xf29b, 0xfd67, 0x2000 }, 1065 }, 1066 { 1067 /* Limited Range 1068 * 1069 * [ 0.255785 0.502160 0.097523 16 ] 1070 * [ -0.147644 -0.289856 0.437500 128 ] 1071 * [ 0.437500 -0.366352 -0.071148 128 ] 1072 */ 1073 { 0x082f, 0x1012, 0x031f, 0x0400 }, 1074 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 }, 1075 { 0x0e00, 0xf448, 0xfdba, 0x2000 }, 1076 }, 1077 }; 1078 1079 /* 1080 * Conversion between Full Range RGB and YUV using the BT.709 Colorspace 1081 * 1082 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets 1083 */ 1084 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt709[2][3][4] = { 1085 { 1086 /* 1087 * Full Range 1088 * 1089 * [ 0.212600 0.715200 0.072200 0 ] 1090 * [ -0.114572 -0.385428 0.500000 128 ] 1091 * [ 0.500000 -0.454153 -0.045847 128 ] 1092 */ 1093 { 0x06ce, 0x16e3, 0x024f, 0x0000 }, 1094 { 0xfc56, 0xf3ac, 0x1000, 0x2000 }, 1095 { 0x1000, 0xf179, 0xfe89, 0x2000 }, 1096 }, 1097 { 1098 /* 1099 * Limited Range 1100 * 1101 * [ 0.181906 0.611804 0.061758 16 ] 1102 * [ -0.100268 -0.337232 0.437500 128 ] 1103 * [ 0.437500 -0.397386 -0.040114 128 ] 1104 */ 1105 { 0x05d2, 0x1394, 0x01fa, 0x0400 }, 1106 { 0xfccc, 0xf536, 0x0e00, 0x2000 }, 1107 { 0x0e00, 0xf34a, 0xfeb8, 0x2000 }, 1108 }, 1109 }; 1110 1111 /* 1112 * Conversion between Full Range RGB and YUV using the BT.2020 Colorspace 1113 * 1114 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets 1115 */ 1116 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt2020[2][3][4] = { 1117 { 1118 /* 1119 * Full Range 1120 * 1121 * [ 0.262700 0.678000 0.059300 0 ] 1122 * [ -0.139630 -0.360370 0.500000 128 ] 1123 * [ 0.500000 -0.459786 -0.040214 128 ] 1124 */ 1125 { 0x0868, 0x15b2, 0x01e6, 0x0000 }, 1126 { 0xfb89, 0xf479, 0x1000, 0x2000 }, 1127 { 0x1000, 0xf14a, 0xfeb8, 0x2000 }, 1128 }, 1129 { 1130 /* Limited Range 1131 * 1132 * [ 0.224732 0.580008 0.050729 16 ] 1133 * [ -0.122176 -0.315324 0.437500 128 ] 1134 * [ 0.437500 -0.402312 -0.035188 128 ] 1135 */ 1136 { 0x082f, 0x1012, 0x031f, 0x0400 }, 1137 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 }, 1138 { 0x0e00, 0xf448, 0xfdba, 0x2000 }, 1139 }, 1140 }; 1141 1142 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi, 1143 const u16 coeffs[3][4]) 1144 { 1145 lockdep_assert_held(&vc4_hdmi->hw_lock); 1146 1147 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]); 1148 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]); 1149 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]); 1150 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]); 1151 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]); 1152 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]); 1153 } 1154 1155 static void vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi *vc4_hdmi, 1156 const u16 coeffs[3][4]) 1157 { 1158 lockdep_assert_held(&vc4_hdmi->hw_lock); 1159 1160 /* YUV444 needs the CSC matrices using the channels in a different order */ 1161 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[1][1] << 16) | coeffs[1][0]); 1162 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[1][3] << 16) | coeffs[1][2]); 1163 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[2][1] << 16) | coeffs[2][0]); 1164 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[2][3] << 16) | coeffs[2][2]); 1165 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[0][1] << 16) | coeffs[0][0]); 1166 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[0][3] << 16) | coeffs[0][2]); 1167 } 1168 1169 static const u16 1170 (*vc5_hdmi_find_yuv_csc_coeffs(struct vc4_hdmi *vc4_hdmi, u32 colorspace, bool limited))[4] 1171 { 1172 switch (colorspace) { 1173 case DRM_MODE_COLORIMETRY_SMPTE_170M_YCC: 1174 case DRM_MODE_COLORIMETRY_XVYCC_601: 1175 case DRM_MODE_COLORIMETRY_SYCC_601: 1176 case DRM_MODE_COLORIMETRY_OPYCC_601: 1177 case DRM_MODE_COLORIMETRY_BT601_YCC: 1178 return vc5_hdmi_csc_full_rgb_to_yuv_bt601[limited]; 1179 1180 default: 1181 case DRM_MODE_COLORIMETRY_NO_DATA: 1182 case DRM_MODE_COLORIMETRY_BT709_YCC: 1183 case DRM_MODE_COLORIMETRY_XVYCC_709: 1184 case DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED: 1185 case DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT: 1186 return vc5_hdmi_csc_full_rgb_to_yuv_bt709[limited]; 1187 1188 case DRM_MODE_COLORIMETRY_BT2020_CYCC: 1189 case DRM_MODE_COLORIMETRY_BT2020_YCC: 1190 case DRM_MODE_COLORIMETRY_BT2020_RGB: 1191 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65: 1192 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER: 1193 return vc5_hdmi_csc_full_rgb_to_yuv_bt2020[limited]; 1194 } 1195 } 1196 1197 static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, 1198 struct drm_connector_state *state, 1199 const struct drm_display_mode *mode) 1200 { 1201 struct drm_device *drm = vc4_hdmi->connector.dev; 1202 unsigned int lim_range = state->hdmi.is_limited_range ? 1 : 0; 1203 unsigned long flags; 1204 const u16 (*csc)[4]; 1205 u32 if_cfg = 0; 1206 u32 if_xbar = 0x543210; 1207 u32 csc_chan_ctl = 0; 1208 u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM, 1209 VC5_MT_CP_CSC_CTL_MODE); 1210 int idx; 1211 1212 if (!drm_dev_enter(drm, &idx)) 1213 return; 1214 1215 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1216 1217 switch (state->hdmi.output_format) { 1218 case DRM_OUTPUT_COLOR_FORMAT_YCBCR444: 1219 csc = vc5_hdmi_find_yuv_csc_coeffs(vc4_hdmi, state->colorspace, !!lim_range); 1220 1221 vc5_hdmi_set_csc_coeffs_swap(vc4_hdmi, csc); 1222 break; 1223 1224 case DRM_OUTPUT_COLOR_FORMAT_YCBCR422: 1225 csc = vc5_hdmi_find_yuv_csc_coeffs(vc4_hdmi, state->colorspace, !!lim_range); 1226 1227 csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD, 1228 VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) | 1229 VC5_MT_CP_CSC_CTL_USE_444_TO_422 | 1230 VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION; 1231 1232 csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE, 1233 VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP); 1234 1235 if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY, 1236 VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422); 1237 1238 vc5_hdmi_set_csc_coeffs(vc4_hdmi, csc); 1239 break; 1240 1241 case DRM_OUTPUT_COLOR_FORMAT_RGB444: 1242 if_xbar = 0x354021; 1243 1244 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_rgb[lim_range]); 1245 break; 1246 1247 default: 1248 break; 1249 } 1250 1251 HDMI_WRITE(HDMI_VEC_INTERFACE_CFG, if_cfg); 1252 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, if_xbar); 1253 HDMI_WRITE(HDMI_CSC_CHANNEL_CTL, csc_chan_ctl); 1254 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl); 1255 1256 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1257 1258 drm_dev_exit(idx); 1259 } 1260 1261 static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi, 1262 struct drm_connector_state *state, 1263 const struct drm_display_mode *mode) 1264 { 1265 struct drm_device *drm = vc4_hdmi->connector.dev; 1266 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC; 1267 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC; 1268 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE; 1269 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1; 1270 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start, 1271 VC4_HDMI_VERTA_VSP) | 1272 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay, 1273 VC4_HDMI_VERTA_VFP) | 1274 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL)); 1275 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) | 1276 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end + 1277 interlaced, 1278 VC4_HDMI_VERTB_VBP)); 1279 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) | 1280 VC4_SET_FIELD(mode->crtc_vtotal - 1281 mode->crtc_vsync_end, 1282 VC4_HDMI_VERTB_VBP)); 1283 unsigned long flags; 1284 u32 reg; 1285 int idx; 1286 1287 if (!drm_dev_enter(drm, &idx)) 1288 return; 1289 1290 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1291 1292 HDMI_WRITE(HDMI_HORZA, 1293 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) | 1294 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) | 1295 VC4_SET_FIELD(mode->hdisplay * pixel_rep, 1296 VC4_HDMI_HORZA_HAP)); 1297 1298 HDMI_WRITE(HDMI_HORZB, 1299 VC4_SET_FIELD((mode->htotal - 1300 mode->hsync_end) * pixel_rep, 1301 VC4_HDMI_HORZB_HBP) | 1302 VC4_SET_FIELD((mode->hsync_end - 1303 mode->hsync_start) * pixel_rep, 1304 VC4_HDMI_HORZB_HSP) | 1305 VC4_SET_FIELD((mode->hsync_start - 1306 mode->hdisplay) * pixel_rep, 1307 VC4_HDMI_HORZB_HFP)); 1308 1309 HDMI_WRITE(HDMI_VERTA0, verta); 1310 HDMI_WRITE(HDMI_VERTA1, verta); 1311 1312 HDMI_WRITE(HDMI_VERTB0, vertb_even); 1313 HDMI_WRITE(HDMI_VERTB1, vertb); 1314 1315 reg = HDMI_READ(HDMI_MISC_CONTROL); 1316 reg &= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK; 1317 reg |= VC4_SET_FIELD(pixel_rep - 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP); 1318 HDMI_WRITE(HDMI_MISC_CONTROL, reg); 1319 1320 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1321 1322 drm_dev_exit(idx); 1323 } 1324 1325 static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi, 1326 struct drm_connector_state *state, 1327 const struct drm_display_mode *mode) 1328 { 1329 struct drm_device *drm = vc4_hdmi->connector.dev; 1330 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC; 1331 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC; 1332 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE; 1333 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1; 1334 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start, 1335 VC5_HDMI_VERTA_VSP) | 1336 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay, 1337 VC5_HDMI_VERTA_VFP) | 1338 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL)); 1339 u32 vertb = (VC4_SET_FIELD(mode->htotal >> (2 - pixel_rep), 1340 VC5_HDMI_VERTB_VSPO) | 1341 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end + 1342 interlaced, 1343 VC4_HDMI_VERTB_VBP)); 1344 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) | 1345 VC4_SET_FIELD(mode->crtc_vtotal - 1346 mode->crtc_vsync_end, 1347 VC4_HDMI_VERTB_VBP)); 1348 unsigned long flags; 1349 unsigned char gcp; 1350 u32 reg; 1351 int idx; 1352 1353 if (!drm_dev_enter(drm, &idx)) 1354 return; 1355 1356 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1357 1358 HDMI_WRITE(HDMI_HORZA, 1359 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) | 1360 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) | 1361 VC4_SET_FIELD(mode->hdisplay * pixel_rep, 1362 VC5_HDMI_HORZA_HAP) | 1363 VC4_SET_FIELD((mode->hsync_start - 1364 mode->hdisplay) * pixel_rep, 1365 VC5_HDMI_HORZA_HFP)); 1366 1367 HDMI_WRITE(HDMI_HORZB, 1368 VC4_SET_FIELD((mode->htotal - 1369 mode->hsync_end) * pixel_rep, 1370 VC5_HDMI_HORZB_HBP) | 1371 VC4_SET_FIELD((mode->hsync_end - 1372 mode->hsync_start) * pixel_rep, 1373 VC5_HDMI_HORZB_HSP)); 1374 1375 HDMI_WRITE(HDMI_VERTA0, verta); 1376 HDMI_WRITE(HDMI_VERTA1, verta); 1377 1378 HDMI_WRITE(HDMI_VERTB0, vertb_even); 1379 HDMI_WRITE(HDMI_VERTB1, vertb); 1380 1381 switch (state->hdmi.output_bpc) { 1382 case 12: 1383 gcp = 6; 1384 break; 1385 case 10: 1386 gcp = 5; 1387 break; 1388 case 8: 1389 default: 1390 gcp = 0; 1391 break; 1392 } 1393 1394 /* 1395 * YCC422 is always 36-bit and not considered deep colour so 1396 * doesn't signal in GCP. 1397 */ 1398 if (state->hdmi.output_format == DRM_OUTPUT_COLOR_FORMAT_YCBCR422) { 1399 gcp = 0; 1400 } 1401 1402 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1); 1403 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK | 1404 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK); 1405 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) | 1406 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH); 1407 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg); 1408 1409 reg = HDMI_READ(HDMI_GCP_WORD_1); 1410 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK; 1411 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1); 1412 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK; 1413 reg |= VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE; 1414 HDMI_WRITE(HDMI_GCP_WORD_1, reg); 1415 1416 reg = HDMI_READ(HDMI_GCP_CONFIG); 1417 reg |= VC5_HDMI_GCP_CONFIG_GCP_ENABLE; 1418 HDMI_WRITE(HDMI_GCP_CONFIG, reg); 1419 1420 reg = HDMI_READ(HDMI_MISC_CONTROL); 1421 reg &= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK; 1422 reg |= VC4_SET_FIELD(pixel_rep - 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP); 1423 HDMI_WRITE(HDMI_MISC_CONTROL, reg); 1424 1425 HDMI_WRITE(HDMI_CLOCK_STOP, 0); 1426 1427 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1428 1429 drm_dev_exit(idx); 1430 } 1431 1432 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi) 1433 { 1434 struct drm_device *drm = vc4_hdmi->connector.dev; 1435 unsigned long flags; 1436 u32 drift; 1437 int ret; 1438 int idx; 1439 1440 if (!drm_dev_enter(drm, &idx)) 1441 return; 1442 1443 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1444 1445 drift = HDMI_READ(HDMI_FIFO_CTL); 1446 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK; 1447 1448 HDMI_WRITE(HDMI_FIFO_CTL, 1449 drift & ~VC4_HDMI_FIFO_CTL_RECENTER); 1450 HDMI_WRITE(HDMI_FIFO_CTL, 1451 drift | VC4_HDMI_FIFO_CTL_RECENTER); 1452 1453 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1454 1455 usleep_range(1000, 1100); 1456 1457 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1458 1459 HDMI_WRITE(HDMI_FIFO_CTL, 1460 drift & ~VC4_HDMI_FIFO_CTL_RECENTER); 1461 HDMI_WRITE(HDMI_FIFO_CTL, 1462 drift | VC4_HDMI_FIFO_CTL_RECENTER); 1463 1464 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1465 1466 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) & 1467 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1); 1468 WARN_ONCE(ret, "Timeout waiting for " 1469 "VC4_HDMI_FIFO_CTL_RECENTER_DONE"); 1470 1471 drm_dev_exit(idx); 1472 } 1473 1474 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder, 1475 struct drm_atomic_commit *state) 1476 { 1477 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 1478 struct drm_device *drm = vc4_hdmi->connector.dev; 1479 struct drm_connector *connector = &vc4_hdmi->connector; 1480 struct drm_connector_state *conn_state = 1481 drm_atomic_get_new_connector_state(state, connector); 1482 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode; 1483 unsigned long long tmds_char_rate = conn_state->hdmi.tmds_char_rate; 1484 unsigned long bvb_rate, hsm_rate; 1485 unsigned long flags; 1486 int ret; 1487 int idx; 1488 1489 mutex_lock(&vc4_hdmi->mutex); 1490 1491 if (!drm_dev_enter(drm, &idx)) 1492 goto out; 1493 1494 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev); 1495 if (ret < 0) { 1496 drm_err(drm, "Failed to retain power domain: %d\n", ret); 1497 goto err_dev_exit; 1498 } 1499 1500 /* 1501 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must 1502 * be faster than pixel clock, infinitesimally faster, tested in 1503 * simulation. Otherwise, exact value is unimportant for HDMI 1504 * operation." This conflicts with bcm2835's vc4 documentation, which 1505 * states HSM's clock has to be at least 108% of the pixel clock. 1506 * 1507 * Real life tests reveal that vc4's firmware statement holds up, and 1508 * users are able to use pixel clocks closer to HSM's, namely for 1509 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between 1510 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of 1511 * 162MHz. 1512 * 1513 * Additionally, the AXI clock needs to be at least 25% of 1514 * pixel clock, but HSM ends up being the limiting factor. 1515 */ 1516 hsm_rate = max_t(unsigned long, 1517 HSM_MIN_CLOCK_FREQ, 1518 div_u64(tmds_char_rate, 100) * 101); 1519 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate); 1520 if (ret) { 1521 drm_err(drm, "Failed to set HSM clock rate: %d\n", ret); 1522 goto err_put_runtime_pm; 1523 } 1524 1525 ret = clk_set_rate(vc4_hdmi->pixel_clock, tmds_char_rate); 1526 if (ret) { 1527 drm_err(drm, "Failed to set pixel clock rate: %d\n", ret); 1528 goto err_put_runtime_pm; 1529 } 1530 1531 ret = clk_prepare_enable(vc4_hdmi->pixel_clock); 1532 if (ret) { 1533 drm_err(drm, "Failed to turn on pixel clock: %d\n", ret); 1534 goto err_put_runtime_pm; 1535 } 1536 1537 vc4_hdmi_cec_update_clk_div(vc4_hdmi); 1538 1539 if (tmds_char_rate > 297000000) 1540 bvb_rate = 300000000; 1541 else if (tmds_char_rate > 148500000) 1542 bvb_rate = 150000000; 1543 else 1544 bvb_rate = 75000000; 1545 1546 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate); 1547 if (ret) { 1548 drm_err(drm, "Failed to set pixel bvb clock rate: %d\n", ret); 1549 goto err_disable_pixel_clock; 1550 } 1551 1552 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock); 1553 if (ret) { 1554 drm_err(drm, "Failed to turn on pixel bvb clock: %d\n", ret); 1555 goto err_disable_pixel_clock; 1556 } 1557 1558 if (vc4_hdmi->variant->phy_init) 1559 vc4_hdmi->variant->phy_init(vc4_hdmi, conn_state); 1560 1561 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1562 1563 HDMI_WRITE(HDMI_SCHEDULER_CONTROL, 1564 HDMI_READ(HDMI_SCHEDULER_CONTROL) | 1565 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT | 1566 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS); 1567 1568 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1569 1570 if (vc4_hdmi->variant->set_timings) 1571 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode); 1572 1573 drm_dev_exit(idx); 1574 1575 mutex_unlock(&vc4_hdmi->mutex); 1576 1577 return; 1578 1579 err_disable_pixel_clock: 1580 clk_disable_unprepare(vc4_hdmi->pixel_clock); 1581 err_put_runtime_pm: 1582 pm_runtime_put(&vc4_hdmi->pdev->dev); 1583 err_dev_exit: 1584 drm_dev_exit(idx); 1585 out: 1586 mutex_unlock(&vc4_hdmi->mutex); 1587 return; 1588 } 1589 1590 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder, 1591 struct drm_atomic_commit *state) 1592 { 1593 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 1594 struct drm_device *drm = vc4_hdmi->connector.dev; 1595 struct drm_connector *connector = &vc4_hdmi->connector; 1596 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode; 1597 struct drm_connector_state *conn_state = 1598 drm_atomic_get_new_connector_state(state, connector); 1599 unsigned long flags; 1600 int idx; 1601 1602 mutex_lock(&vc4_hdmi->mutex); 1603 1604 if (!drm_dev_enter(drm, &idx)) 1605 goto out; 1606 1607 if (vc4_hdmi->variant->csc_setup) 1608 vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode); 1609 1610 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1611 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N); 1612 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1613 1614 drm_dev_exit(idx); 1615 1616 out: 1617 mutex_unlock(&vc4_hdmi->mutex); 1618 } 1619 1620 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder, 1621 struct drm_atomic_commit *state) 1622 { 1623 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 1624 struct drm_connector *connector = &vc4_hdmi->connector; 1625 struct drm_device *drm = connector->dev; 1626 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode; 1627 struct drm_display_info *display = &vc4_hdmi->connector.display_info; 1628 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC; 1629 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC; 1630 unsigned long flags; 1631 int ret; 1632 int idx; 1633 1634 mutex_lock(&vc4_hdmi->mutex); 1635 1636 if (!drm_dev_enter(drm, &idx)) 1637 goto out; 1638 1639 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1640 1641 HDMI_WRITE(HDMI_VID_CTL, 1642 (HDMI_READ(HDMI_VID_CTL) & 1643 ~(VC4_HD_VID_CTL_VSYNC_LOW | VC4_HD_VID_CTL_HSYNC_LOW)) | 1644 VC4_HD_VID_CTL_ENABLE | 1645 VC4_HD_VID_CTL_CLRRGB | 1646 VC4_HD_VID_CTL_UNDERFLOW_ENABLE | 1647 VC4_HD_VID_CTL_FRAME_COUNTER_RESET | 1648 VC4_HD_VID_CTL_BLANK_INSERT_EN | 1649 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) | 1650 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW)); 1651 1652 HDMI_WRITE(HDMI_VID_CTL, 1653 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX); 1654 1655 if (display->is_hdmi) { 1656 HDMI_WRITE(HDMI_SCHEDULER_CONTROL, 1657 HDMI_READ(HDMI_SCHEDULER_CONTROL) | 1658 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI); 1659 1660 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1661 1662 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) & 1663 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000); 1664 WARN_ONCE(ret, "Timeout waiting for " 1665 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n"); 1666 } else { 1667 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 1668 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & 1669 ~(VC4_HDMI_RAM_PACKET_ENABLE)); 1670 HDMI_WRITE(HDMI_SCHEDULER_CONTROL, 1671 HDMI_READ(HDMI_SCHEDULER_CONTROL) & 1672 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI); 1673 1674 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1675 1676 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) & 1677 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000); 1678 WARN_ONCE(ret, "Timeout waiting for " 1679 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n"); 1680 } 1681 1682 if (display->is_hdmi) { 1683 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1684 1685 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) & 1686 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE)); 1687 1688 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 1689 VC4_HDMI_RAM_PACKET_ENABLE); 1690 1691 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1692 vc4_hdmi->packet_ram_enabled = true; 1693 1694 drm_atomic_helper_connector_hdmi_update_infoframes(connector, state); 1695 } 1696 1697 vc4_hdmi_recenter_fifo(vc4_hdmi); 1698 vc4_hdmi_enable_scrambling(encoder); 1699 1700 drm_dev_exit(idx); 1701 1702 out: 1703 mutex_unlock(&vc4_hdmi->mutex); 1704 } 1705 1706 static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder, 1707 struct drm_crtc_state *crtc_state, 1708 struct drm_connector_state *conn_state) 1709 { 1710 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 1711 1712 mutex_lock(&vc4_hdmi->mutex); 1713 drm_mode_copy(&vc4_hdmi->saved_adjusted_mode, 1714 &crtc_state->adjusted_mode); 1715 vc4_hdmi->output_bpc = conn_state->hdmi.output_bpc; 1716 vc4_hdmi->output_format = conn_state->hdmi.output_format; 1717 mutex_unlock(&vc4_hdmi->mutex); 1718 } 1719 1720 static enum drm_mode_status 1721 vc4_hdmi_connector_clock_valid(const struct drm_connector *connector, 1722 const struct drm_display_mode *mode, 1723 unsigned long long clock) 1724 { 1725 const struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 1726 struct vc4_dev *vc4 = to_vc4_dev(connector->dev); 1727 1728 if (clock > vc4_hdmi->variant->max_pixel_clock) 1729 return MODE_CLOCK_HIGH; 1730 1731 if (!vc4->hvs->vc5_hdmi_enable_hdmi_20 && clock > HDMI_14_MAX_TMDS_CLK) 1732 return MODE_CLOCK_HIGH; 1733 1734 /* 4096x2160@60 is not reliable without overclocking core */ 1735 if (!vc4->hvs->vc5_hdmi_enable_4096by2160 && 1736 mode->hdisplay > 3840 && mode->vdisplay >= 2160 && 1737 drm_mode_vrefresh(mode) >= 50) 1738 return MODE_CLOCK_HIGH; 1739 1740 return MODE_OK; 1741 } 1742 1743 static const struct drm_connector_hdmi_funcs vc4_hdmi_hdmi_connector_funcs = { 1744 .tmds_char_rate_valid = vc4_hdmi_connector_clock_valid, 1745 .avi = { 1746 .clear_infoframe = vc4_hdmi_clear_avi_infoframe, 1747 .write_infoframe = vc4_hdmi_write_avi_infoframe, 1748 }, 1749 .hdmi = { 1750 .clear_infoframe = vc4_hdmi_clear_hdmi_infoframe, 1751 .write_infoframe = vc4_hdmi_write_hdmi_infoframe, 1752 }, 1753 .audio = { 1754 .clear_infoframe = vc4_hdmi_clear_audio_infoframe, 1755 .write_infoframe = vc4_hdmi_write_audio_infoframe, 1756 }, 1757 .hdr_drm = { 1758 .clear_infoframe = vc4_hdmi_clear_hdr_drm_infoframe, 1759 .write_infoframe = vc4_hdmi_write_hdr_drm_infoframe, 1760 }, 1761 .spd = { 1762 .clear_infoframe = vc4_hdmi_clear_spd_infoframe, 1763 .write_infoframe = vc4_hdmi_write_spd_infoframe, 1764 }, 1765 }; 1766 1767 #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL 1768 #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL 1769 1770 static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder, 1771 struct drm_crtc_state *crtc_state, 1772 struct drm_connector_state *conn_state) 1773 { 1774 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 1775 struct drm_display_mode *mode = &crtc_state->adjusted_mode; 1776 unsigned long long tmds_char_rate = mode->clock * 1000; 1777 unsigned long long tmds_bit_rate; 1778 1779 if (vc4_hdmi->variant->unsupported_odd_h_timings) { 1780 if (mode->flags & DRM_MODE_FLAG_DBLCLK) { 1781 /* Only try to fixup DBLCLK modes to get 480i and 576i 1782 * working. 1783 * A generic solution for all modes with odd horizontal 1784 * timing values seems impossible based on trying to 1785 * solve it for 1366x768 monitors. 1786 */ 1787 if ((mode->hsync_start - mode->hdisplay) & 1) 1788 mode->hsync_start--; 1789 if ((mode->hsync_end - mode->hsync_start) & 1) 1790 mode->hsync_end--; 1791 } 1792 1793 /* Now check whether we still have odd values remaining */ 1794 if ((mode->hdisplay % 2) || (mode->hsync_start % 2) || 1795 (mode->hsync_end % 2) || (mode->htotal % 2)) 1796 return -EINVAL; 1797 } 1798 1799 /* 1800 * The 1440p@60 pixel rate is in the same range than the first 1801 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz 1802 * bandwidth). Slightly lower the frequency to bring it out of 1803 * the WiFi range. 1804 */ 1805 tmds_bit_rate = tmds_char_rate * 10; 1806 if (vc4_hdmi->disable_wifi_frequencies && 1807 (tmds_bit_rate >= WIFI_2_4GHz_CH1_MIN_FREQ && 1808 tmds_bit_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) { 1809 mode->clock = 238560; 1810 tmds_char_rate = mode->clock * 1000; 1811 } 1812 1813 return 0; 1814 } 1815 1816 static enum drm_mode_status 1817 vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder, 1818 const struct drm_display_mode *mode) 1819 { 1820 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 1821 1822 if (vc4_hdmi->variant->unsupported_odd_h_timings && 1823 !(mode->flags & DRM_MODE_FLAG_DBLCLK) && 1824 ((mode->hdisplay % 2) || (mode->hsync_start % 2) || 1825 (mode->hsync_end % 2) || (mode->htotal % 2))) 1826 return MODE_H_ILLEGAL; 1827 1828 return MODE_OK; 1829 } 1830 1831 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = { 1832 .atomic_check = vc4_hdmi_encoder_atomic_check, 1833 .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set, 1834 .mode_valid = vc4_hdmi_encoder_mode_valid, 1835 }; 1836 1837 static int vc4_hdmi_late_register(struct drm_encoder *encoder) 1838 { 1839 struct drm_device *drm = encoder->dev; 1840 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); 1841 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant; 1842 1843 drm_debugfs_add_file(drm, variant->debugfs_name, 1844 vc4_hdmi_debugfs_regs, vc4_hdmi); 1845 1846 return 0; 1847 } 1848 1849 static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = { 1850 .late_register = vc4_hdmi_late_register, 1851 }; 1852 1853 static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask) 1854 { 1855 int i; 1856 u32 channel_map = 0; 1857 1858 for (i = 0; i < 8; i++) { 1859 if (channel_mask & BIT(i)) 1860 channel_map |= i << (3 * i); 1861 } 1862 return channel_map; 1863 } 1864 1865 static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask) 1866 { 1867 int i; 1868 u32 channel_map = 0; 1869 1870 for (i = 0; i < 8; i++) { 1871 if (channel_mask & BIT(i)) 1872 channel_map |= i << (4 * i); 1873 } 1874 return channel_map; 1875 } 1876 1877 static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi) 1878 { 1879 struct drm_device *drm = vc4_hdmi->connector.dev; 1880 unsigned long flags; 1881 u32 hotplug; 1882 int idx; 1883 1884 if (!drm_dev_enter(drm, &idx)) 1885 return false; 1886 1887 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1888 hotplug = HDMI_READ(HDMI_HOTPLUG); 1889 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1890 1891 drm_dev_exit(idx); 1892 1893 return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED); 1894 } 1895 1896 /* HDMI audio codec callbacks */ 1897 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi, 1898 unsigned int samplerate) 1899 { 1900 struct drm_device *drm = vc4_hdmi->connector.dev; 1901 u32 hsm_clock; 1902 unsigned long flags; 1903 unsigned long n, m; 1904 int idx; 1905 1906 if (!drm_dev_enter(drm, &idx)) 1907 return; 1908 1909 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock); 1910 rational_best_approximation(hsm_clock, samplerate, 1911 VC4_HD_MAI_SMP_N_MASK >> 1912 VC4_HD_MAI_SMP_N_SHIFT, 1913 (VC4_HD_MAI_SMP_M_MASK >> 1914 VC4_HD_MAI_SMP_M_SHIFT) + 1, 1915 &n, &m); 1916 1917 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1918 HDMI_WRITE(HDMI_MAI_SMP, 1919 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) | 1920 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M)); 1921 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 1922 1923 drm_dev_exit(idx); 1924 } 1925 1926 static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate) 1927 { 1928 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode; 1929 u32 n, cts; 1930 u64 tmp; 1931 1932 lockdep_assert_held(&vc4_hdmi->mutex); 1933 lockdep_assert_held(&vc4_hdmi->hw_lock); 1934 1935 n = 128 * samplerate / 1000; 1936 tmp = (u64)(mode->clock * 1000) * n; 1937 do_div(tmp, 128 * samplerate); 1938 cts = tmp; 1939 1940 HDMI_WRITE(HDMI_CRP_CFG, 1941 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN | 1942 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N)); 1943 1944 /* 1945 * We could get slightly more accurate clocks in some cases by 1946 * providing a CTS_1 value. The two CTS values are alternated 1947 * between based on the period fields 1948 */ 1949 HDMI_WRITE(HDMI_CTS_0, cts); 1950 HDMI_WRITE(HDMI_CTS_1, cts); 1951 } 1952 1953 static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai) 1954 { 1955 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai); 1956 1957 return snd_soc_card_get_drvdata(card); 1958 } 1959 1960 static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi) 1961 { 1962 struct drm_display_info *display = &vc4_hdmi->connector.display_info; 1963 1964 lockdep_assert_held(&vc4_hdmi->mutex); 1965 1966 /* 1967 * If the encoder is currently in DVI mode, treat the codec DAI 1968 * as missing. 1969 */ 1970 if (!display->is_hdmi) 1971 return false; 1972 1973 return true; 1974 } 1975 1976 static int vc4_hdmi_audio_startup(struct drm_connector *connector) 1977 { 1978 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 1979 struct drm_device *drm = vc4_hdmi->connector.dev; 1980 unsigned long flags; 1981 int ret = 0; 1982 int idx; 1983 1984 mutex_lock(&vc4_hdmi->mutex); 1985 1986 if (!drm_dev_enter(drm, &idx)) { 1987 ret = -ENODEV; 1988 goto out; 1989 } 1990 1991 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) { 1992 ret = -ENOTSUPP; 1993 goto out_dev_exit; 1994 } 1995 1996 vc4_hdmi->audio.streaming = true; 1997 1998 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 1999 HDMI_WRITE(HDMI_MAI_CTL, 2000 VC4_HD_MAI_CTL_RESET | 2001 VC4_HD_MAI_CTL_FLUSH | 2002 VC4_HD_MAI_CTL_DLATE | 2003 VC4_HD_MAI_CTL_ERRORE | 2004 VC4_HD_MAI_CTL_ERRORF); 2005 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 2006 2007 if (vc4_hdmi->variant->phy_rng_enable) 2008 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi); 2009 2010 out_dev_exit: 2011 drm_dev_exit(idx); 2012 out: 2013 mutex_unlock(&vc4_hdmi->mutex); 2014 2015 return ret; 2016 } 2017 2018 static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi) 2019 { 2020 struct device *dev = &vc4_hdmi->pdev->dev; 2021 unsigned long flags; 2022 int ret; 2023 2024 lockdep_assert_held(&vc4_hdmi->mutex); 2025 2026 vc4_hdmi->audio.streaming = false; 2027 ret = vc4_hdmi_stop_packet(vc4_hdmi, HDMI_INFOFRAME_TYPE_AUDIO, false); 2028 if (ret) 2029 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret); 2030 2031 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 2032 2033 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET); 2034 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF); 2035 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH); 2036 2037 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 2038 } 2039 2040 static void vc4_hdmi_audio_shutdown(struct drm_connector *connector) 2041 { 2042 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 2043 struct drm_device *drm = vc4_hdmi->connector.dev; 2044 unsigned long flags; 2045 int idx; 2046 2047 mutex_lock(&vc4_hdmi->mutex); 2048 2049 if (!drm_dev_enter(drm, &idx)) 2050 goto out; 2051 2052 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 2053 2054 HDMI_WRITE(HDMI_MAI_CTL, 2055 VC4_HD_MAI_CTL_DLATE | 2056 VC4_HD_MAI_CTL_ERRORE | 2057 VC4_HD_MAI_CTL_ERRORF); 2058 2059 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 2060 2061 if (vc4_hdmi->variant->phy_rng_disable) 2062 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi); 2063 2064 vc4_hdmi->audio.streaming = false; 2065 vc4_hdmi_audio_reset(vc4_hdmi); 2066 2067 drm_dev_exit(idx); 2068 2069 out: 2070 mutex_unlock(&vc4_hdmi->mutex); 2071 } 2072 2073 static int sample_rate_to_mai_fmt(int samplerate) 2074 { 2075 switch (samplerate) { 2076 case 8000: 2077 return VC4_HDMI_MAI_SAMPLE_RATE_8000; 2078 case 11025: 2079 return VC4_HDMI_MAI_SAMPLE_RATE_11025; 2080 case 12000: 2081 return VC4_HDMI_MAI_SAMPLE_RATE_12000; 2082 case 16000: 2083 return VC4_HDMI_MAI_SAMPLE_RATE_16000; 2084 case 22050: 2085 return VC4_HDMI_MAI_SAMPLE_RATE_22050; 2086 case 24000: 2087 return VC4_HDMI_MAI_SAMPLE_RATE_24000; 2088 case 32000: 2089 return VC4_HDMI_MAI_SAMPLE_RATE_32000; 2090 case 44100: 2091 return VC4_HDMI_MAI_SAMPLE_RATE_44100; 2092 case 48000: 2093 return VC4_HDMI_MAI_SAMPLE_RATE_48000; 2094 case 64000: 2095 return VC4_HDMI_MAI_SAMPLE_RATE_64000; 2096 case 88200: 2097 return VC4_HDMI_MAI_SAMPLE_RATE_88200; 2098 case 96000: 2099 return VC4_HDMI_MAI_SAMPLE_RATE_96000; 2100 case 128000: 2101 return VC4_HDMI_MAI_SAMPLE_RATE_128000; 2102 case 176400: 2103 return VC4_HDMI_MAI_SAMPLE_RATE_176400; 2104 case 192000: 2105 return VC4_HDMI_MAI_SAMPLE_RATE_192000; 2106 default: 2107 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED; 2108 } 2109 } 2110 2111 /* HDMI audio codec callbacks */ 2112 static int vc4_hdmi_audio_prepare(struct drm_connector *connector, 2113 struct hdmi_codec_daifmt *daifmt, 2114 struct hdmi_codec_params *params) 2115 { 2116 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 2117 struct drm_device *drm = vc4_hdmi->connector.dev; 2118 struct vc4_dev *vc4 = to_vc4_dev(drm); 2119 unsigned int sample_rate = params->sample_rate; 2120 unsigned int channels = params->channels; 2121 unsigned long flags; 2122 u32 audio_packet_config, channel_mask; 2123 u32 channel_map; 2124 u32 mai_audio_format; 2125 u32 mai_sample_rate; 2126 int ret = 0; 2127 int idx; 2128 2129 dev_dbg(&vc4_hdmi->pdev->dev, "%s: %u Hz, %d bit, %d channels\n", __func__, 2130 sample_rate, params->sample_width, channels); 2131 2132 mutex_lock(&vc4_hdmi->mutex); 2133 2134 if (!drm_dev_enter(drm, &idx)) { 2135 ret = -ENODEV; 2136 goto out; 2137 } 2138 2139 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) { 2140 ret = -EINVAL; 2141 goto out_dev_exit; 2142 } 2143 2144 vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate); 2145 2146 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 2147 HDMI_WRITE(HDMI_MAI_CTL, 2148 VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) | 2149 VC4_HD_MAI_CTL_WHOLSMP | 2150 VC4_HD_MAI_CTL_CHALIGN | 2151 VC4_HD_MAI_CTL_ENABLE); 2152 2153 mai_sample_rate = sample_rate_to_mai_fmt(sample_rate); 2154 if (params->iec.status[0] & IEC958_AES0_NONAUDIO && 2155 params->channels == 8) 2156 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR; 2157 else 2158 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM; 2159 HDMI_WRITE(HDMI_MAI_FMT, 2160 VC4_SET_FIELD(mai_sample_rate, 2161 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) | 2162 VC4_SET_FIELD(mai_audio_format, 2163 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT)); 2164 2165 /* The B frame identifier should match the value used by alsa-lib (8) */ 2166 audio_packet_config = 2167 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT | 2168 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS | 2169 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER); 2170 2171 channel_mask = GENMASK(channels - 1, 0); 2172 audio_packet_config |= VC4_SET_FIELD(channel_mask, 2173 VC4_HDMI_AUDIO_PACKET_CEA_MASK); 2174 2175 /* Set the MAI threshold */ 2176 switch (vc4->gen) { 2177 case VC4_GEN_6_D: 2178 HDMI_WRITE(HDMI_MAI_THR, 2179 VC4_SET_FIELD(0x10, VC6_D_HD_MAI_THR_PANICHIGH) | 2180 VC4_SET_FIELD(0x10, VC6_D_HD_MAI_THR_PANICLOW) | 2181 VC4_SET_FIELD(0x1c, VC6_D_HD_MAI_THR_DREQHIGH) | 2182 VC4_SET_FIELD(0x1c, VC6_D_HD_MAI_THR_DREQLOW)); 2183 break; 2184 case VC4_GEN_6_C: 2185 case VC4_GEN_5: 2186 HDMI_WRITE(HDMI_MAI_THR, 2187 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) | 2188 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) | 2189 VC4_SET_FIELD(0x1c, VC4_HD_MAI_THR_DREQHIGH) | 2190 VC4_SET_FIELD(0x1c, VC4_HD_MAI_THR_DREQLOW)); 2191 break; 2192 case VC4_GEN_4: 2193 HDMI_WRITE(HDMI_MAI_THR, 2194 VC4_SET_FIELD(0x8, VC4_HD_MAI_THR_PANICHIGH) | 2195 VC4_SET_FIELD(0x8, VC4_HD_MAI_THR_PANICLOW) | 2196 VC4_SET_FIELD(0x6, VC4_HD_MAI_THR_DREQHIGH) | 2197 VC4_SET_FIELD(0x8, VC4_HD_MAI_THR_DREQLOW)); 2198 break; 2199 default: 2200 drm_err(drm, "Unknown VC4 generation: %d", vc4->gen); 2201 break; 2202 } 2203 2204 HDMI_WRITE(HDMI_MAI_CONFIG, 2205 VC4_HDMI_MAI_CONFIG_BIT_REVERSE | 2206 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE | 2207 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK)); 2208 2209 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask); 2210 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map); 2211 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config); 2212 2213 vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate); 2214 2215 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 2216 2217 ret = drm_atomic_helper_connector_hdmi_update_audio_infoframe(connector, 2218 ¶ms->cea); 2219 if (ret) 2220 goto out_dev_exit; 2221 2222 out_dev_exit: 2223 drm_dev_exit(idx); 2224 out: 2225 mutex_unlock(&vc4_hdmi->mutex); 2226 2227 return ret; 2228 } 2229 2230 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = { 2231 .name = "vc4-hdmi-cpu-dai-component", 2232 .legacy_dai_naming = 1, 2233 }; 2234 2235 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai) 2236 { 2237 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); 2238 2239 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL); 2240 2241 return 0; 2242 } 2243 2244 static const struct snd_soc_dai_ops vc4_snd_dai_ops = { 2245 .probe = vc4_hdmi_audio_cpu_dai_probe, 2246 }; 2247 2248 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = { 2249 .name = "vc4-hdmi-cpu-dai", 2250 .ops = &vc4_snd_dai_ops, 2251 .playback = { 2252 .stream_name = "Playback", 2253 .channels_min = 1, 2254 .channels_max = 8, 2255 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | 2256 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | 2257 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | 2258 SNDRV_PCM_RATE_192000, 2259 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE, 2260 }, 2261 }; 2262 2263 static const struct snd_dmaengine_pcm_config pcm_conf = { 2264 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx", 2265 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, 2266 }; 2267 2268 static const struct drm_connector_hdmi_audio_funcs vc4_hdmi_audio_funcs = { 2269 .startup = vc4_hdmi_audio_startup, 2270 .prepare = vc4_hdmi_audio_prepare, 2271 .shutdown = vc4_hdmi_audio_shutdown, 2272 }; 2273 2274 static int vc4_hdmi_codec_init(struct snd_soc_pcm_runtime *rtd) 2275 { 2276 struct vc4_hdmi *vc4_hdmi = snd_soc_card_get_drvdata(rtd->card); 2277 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component; 2278 int ret; 2279 2280 ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT, 2281 &vc4_hdmi->hdmi_jack); 2282 if (ret) { 2283 dev_err(rtd->dev, "HDMI Jack creation failed: %d\n", ret); 2284 return ret; 2285 } 2286 2287 return snd_soc_component_set_jack(component, &vc4_hdmi->hdmi_jack, NULL); 2288 } 2289 2290 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi) 2291 { 2292 const struct vc4_hdmi_register *mai_data = 2293 &vc4_hdmi->variant->registers[HDMI_MAI_DATA]; 2294 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link; 2295 struct snd_soc_card *card = &vc4_hdmi->audio.card; 2296 struct device *dev = &vc4_hdmi->pdev->dev; 2297 const __be32 *addr; 2298 int index, len; 2299 int ret; 2300 2301 /* 2302 * ASoC makes it a bit hard to retrieve a pointer to the 2303 * vc4_hdmi structure. Registering the card will overwrite our 2304 * device drvdata with a pointer to the snd_soc_card structure, 2305 * which can then be used to retrieve whatever drvdata we want 2306 * to associate. 2307 * 2308 * However, that doesn't fly in the case where we wouldn't 2309 * register an ASoC card (because of an old DT that is missing 2310 * the dmas properties for example), then the card isn't 2311 * registered and the device drvdata wouldn't be set. 2312 * 2313 * We can deal with both cases by making sure a snd_soc_card 2314 * pointer and a vc4_hdmi structure are pointing to the same 2315 * memory address, so we can treat them indistinctly without any 2316 * issue. 2317 */ 2318 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0); 2319 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0); 2320 2321 if (!of_find_property(dev->of_node, "dmas", &len) || !len) { 2322 dev_warn(dev, 2323 "'dmas' DT property is missing or empty, no HDMI audio\n"); 2324 return 0; 2325 } 2326 2327 if (mai_data->reg != VC4_HD) { 2328 WARN_ONCE(true, "MAI isn't in the HD block\n"); 2329 return -EINVAL; 2330 } 2331 2332 /* 2333 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve 2334 * the bus address specified in the DT, because the physical address 2335 * (the one returned by platform_get_resource()) is not appropriate 2336 * for DMA transfers. 2337 * This VC/MMU should probably be exposed to avoid this kind of hacks. 2338 */ 2339 index = of_property_match_string(dev->of_node, "reg-names", "hd"); 2340 /* Before BCM2711, we don't have a named register range */ 2341 if (index < 0) 2342 index = 1; 2343 2344 addr = of_get_address(dev->of_node, index, NULL, NULL); 2345 if (!addr) 2346 return -EINVAL; 2347 2348 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset; 2349 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 2350 vc4_hdmi->audio.dma_data.maxburst = 2; 2351 2352 /* 2353 * NOTE: Strictly speaking, we should probably use a DRM-managed 2354 * registration there to avoid removing all the audio components 2355 * by the time the driver doesn't have any user anymore. 2356 * 2357 * However, the ASoC core uses a number of devm_kzalloc calls 2358 * when registering, even when using non-device-managed 2359 * functions (such as in snd_soc_register_component()). 2360 * 2361 * If we call snd_soc_unregister_component() in a DRM-managed 2362 * action, the device-managed actions have already been executed 2363 * and thus we would access memory that has been freed. 2364 * 2365 * Using device-managed hooks here probably leaves us open to a 2366 * bunch of issues if userspace still has a handle on the ALSA 2367 * device when the device is removed. However, this is mitigated 2368 * by the use of drm_dev_enter()/drm_dev_exit() in the audio 2369 * path to prevent the access to the device resources if it 2370 * isn't there anymore. 2371 * 2372 * Then, the vc4_hdmi structure is DRM-managed and thus only 2373 * freed whenever the last user has closed the DRM device file. 2374 * It should thus outlive ALSA in most situations. 2375 */ 2376 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0); 2377 if (ret) { 2378 dev_err(dev, "Could not register PCM component: %d\n", ret); 2379 return ret; 2380 } 2381 2382 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp, 2383 &vc4_hdmi_audio_cpu_dai_drv, 1); 2384 if (ret) { 2385 dev_err(dev, "Could not register CPU DAI: %d\n", ret); 2386 return ret; 2387 } 2388 2389 ret = drm_connector_hdmi_audio_init(&vc4_hdmi->connector, dev, 2390 &vc4_hdmi_audio_funcs, 8, 0, false, 2391 -1); 2392 if (ret) 2393 return ret; 2394 2395 dai_link->cpus = &vc4_hdmi->audio.cpu; 2396 dai_link->codecs = &vc4_hdmi->audio.codec; 2397 dai_link->platforms = &vc4_hdmi->audio.platform; 2398 2399 dai_link->num_cpus = 1; 2400 dai_link->num_codecs = 1; 2401 dai_link->num_platforms = 1; 2402 2403 dai_link->name = "MAI"; 2404 dai_link->stream_name = "MAI PCM"; 2405 dai_link->codecs->dai_name = "i2s-hifi"; 2406 dai_link->cpus->dai_name = dev_name(dev); 2407 dai_link->codecs->name = dev_name(&vc4_hdmi->connector.hdmi_audio.codec_pdev->dev); 2408 dai_link->platforms->name = dev_name(dev); 2409 dai_link->init = vc4_hdmi_codec_init; 2410 2411 card->dai_link = dai_link; 2412 card->num_links = 1; 2413 card->name = vc4_hdmi->variant->card_name; 2414 card->driver_name = "vc4-hdmi"; 2415 card->dev = dev; 2416 card->owner = THIS_MODULE; 2417 2418 /* 2419 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and 2420 * stores a pointer to the snd card object in dev->driver_data. This 2421 * means we cannot use it for something else. The hdmi back-pointer is 2422 * now stored in card->drvdata and should be retrieved with 2423 * snd_soc_card_get_drvdata() if needed. 2424 */ 2425 snd_soc_card_set_drvdata(card, vc4_hdmi); 2426 ret = devm_snd_soc_register_card(dev, card); 2427 if (ret) 2428 dev_err_probe(dev, ret, "Could not register sound card\n"); 2429 2430 return ret; 2431 2432 } 2433 2434 static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv) 2435 { 2436 struct vc4_hdmi *vc4_hdmi = priv; 2437 struct drm_connector *connector = &vc4_hdmi->connector; 2438 struct drm_device *dev = connector->dev; 2439 2440 if (dev && dev->registered) 2441 drm_connector_helper_hpd_irq_event(connector); 2442 2443 return IRQ_HANDLED; 2444 } 2445 2446 static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi) 2447 { 2448 struct drm_connector *connector = &vc4_hdmi->connector; 2449 struct platform_device *pdev = vc4_hdmi->pdev; 2450 int ret; 2451 2452 if (vc4_hdmi->variant->external_irq_controller) { 2453 int hpd = platform_get_irq_byname(pdev, "hpd-connected"); 2454 2455 if (hpd < 0) 2456 return hpd; 2457 2458 ret = devm_request_threaded_irq(&pdev->dev, hpd, 2459 NULL, 2460 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT, 2461 "vc4 hdmi hpd connected", vc4_hdmi); 2462 if (ret) 2463 return ret; 2464 2465 hpd = platform_get_irq_byname(pdev, "hpd-removed"); 2466 if (hpd < 0) 2467 return hpd; 2468 2469 ret = devm_request_threaded_irq(&pdev->dev, hpd, 2470 NULL, 2471 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT, 2472 "vc4 hdmi hpd disconnected", vc4_hdmi); 2473 if (ret) 2474 return ret; 2475 2476 connector->polled = DRM_CONNECTOR_POLL_HPD; 2477 } 2478 2479 return 0; 2480 } 2481 2482 #ifdef CONFIG_DRM_VC4_HDMI_CEC 2483 static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv) 2484 { 2485 struct vc4_hdmi *vc4_hdmi = priv; 2486 2487 if (vc4_hdmi->cec_rx_msg.len) 2488 drm_connector_hdmi_cec_received_msg(&vc4_hdmi->connector, 2489 &vc4_hdmi->cec_rx_msg); 2490 2491 return IRQ_HANDLED; 2492 } 2493 2494 static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv) 2495 { 2496 struct vc4_hdmi *vc4_hdmi = priv; 2497 2498 if (vc4_hdmi->cec_tx_ok) { 2499 drm_connector_hdmi_cec_transmit_done(&vc4_hdmi->connector, 2500 CEC_TX_STATUS_OK, 2501 0, 0, 0, 0); 2502 } else { 2503 /* 2504 * This CEC implementation makes 1 retry, so if we 2505 * get a NACK, then that means it made 2 attempts. 2506 */ 2507 drm_connector_hdmi_cec_transmit_done(&vc4_hdmi->connector, 2508 CEC_TX_STATUS_NACK, 2509 0, 2, 0, 0); 2510 } 2511 return IRQ_HANDLED; 2512 } 2513 2514 static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv) 2515 { 2516 struct vc4_hdmi *vc4_hdmi = priv; 2517 irqreturn_t ret; 2518 2519 if (vc4_hdmi->cec_irq_was_rx) 2520 ret = vc4_cec_irq_handler_rx_thread(irq, priv); 2521 else 2522 ret = vc4_cec_irq_handler_tx_thread(irq, priv); 2523 2524 return ret; 2525 } 2526 2527 static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1) 2528 { 2529 struct drm_device *dev = vc4_hdmi->connector.dev; 2530 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg; 2531 unsigned int i; 2532 2533 lockdep_assert_held(&vc4_hdmi->hw_lock); 2534 2535 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >> 2536 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT); 2537 2538 if (msg->len > 16) { 2539 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len); 2540 return; 2541 } 2542 2543 for (i = 0; i < msg->len; i += 4) { 2544 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2)); 2545 2546 msg->msg[i] = val & 0xff; 2547 msg->msg[i + 1] = (val >> 8) & 0xff; 2548 msg->msg[i + 2] = (val >> 16) & 0xff; 2549 msg->msg[i + 3] = (val >> 24) & 0xff; 2550 } 2551 } 2552 2553 static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi) 2554 { 2555 u32 cntrl1; 2556 2557 /* 2558 * We don't need to protect the register access using 2559 * drm_dev_enter() there because the interrupt handler lifetime 2560 * is tied to the device itself, and not to the DRM device. 2561 * 2562 * So when the device will be gone, one of the first thing we 2563 * will be doing will be to unregister the interrupt handler, 2564 * and then unregister the DRM device. drm_dev_enter() would 2565 * thus always succeed if we are here. 2566 */ 2567 2568 lockdep_assert_held(&vc4_hdmi->hw_lock); 2569 2570 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1); 2571 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD; 2572 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN; 2573 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1); 2574 2575 return IRQ_WAKE_THREAD; 2576 } 2577 2578 static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv) 2579 { 2580 struct vc4_hdmi *vc4_hdmi = priv; 2581 irqreturn_t ret; 2582 2583 spin_lock(&vc4_hdmi->hw_lock); 2584 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi); 2585 spin_unlock(&vc4_hdmi->hw_lock); 2586 2587 return ret; 2588 } 2589 2590 static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi) 2591 { 2592 u32 cntrl1; 2593 2594 lockdep_assert_held(&vc4_hdmi->hw_lock); 2595 2596 /* 2597 * We don't need to protect the register access using 2598 * drm_dev_enter() there because the interrupt handler lifetime 2599 * is tied to the device itself, and not to the DRM device. 2600 * 2601 * So when the device will be gone, one of the first thing we 2602 * will be doing will be to unregister the interrupt handler, 2603 * and then unregister the DRM device. drm_dev_enter() would 2604 * thus always succeed if we are here. 2605 */ 2606 2607 vc4_hdmi->cec_rx_msg.len = 0; 2608 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1); 2609 vc4_cec_read_msg(vc4_hdmi, cntrl1); 2610 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF; 2611 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1); 2612 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF; 2613 2614 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1); 2615 2616 return IRQ_WAKE_THREAD; 2617 } 2618 2619 static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv) 2620 { 2621 struct vc4_hdmi *vc4_hdmi = priv; 2622 irqreturn_t ret; 2623 2624 spin_lock(&vc4_hdmi->hw_lock); 2625 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi); 2626 spin_unlock(&vc4_hdmi->hw_lock); 2627 2628 return ret; 2629 } 2630 2631 static irqreturn_t vc4_cec_irq_handler(int irq, void *priv) 2632 { 2633 struct vc4_hdmi *vc4_hdmi = priv; 2634 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS); 2635 irqreturn_t ret; 2636 u32 cntrl5; 2637 2638 /* 2639 * We don't need to protect the register access using 2640 * drm_dev_enter() there because the interrupt handler lifetime 2641 * is tied to the device itself, and not to the DRM device. 2642 * 2643 * So when the device will be gone, one of the first thing we 2644 * will be doing will be to unregister the interrupt handler, 2645 * and then unregister the DRM device. drm_dev_enter() would 2646 * thus always succeed if we are here. 2647 */ 2648 2649 if (!(stat & VC4_HDMI_CPU_CEC)) 2650 return IRQ_NONE; 2651 2652 spin_lock(&vc4_hdmi->hw_lock); 2653 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5); 2654 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT; 2655 if (vc4_hdmi->cec_irq_was_rx) 2656 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi); 2657 else 2658 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi); 2659 2660 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC); 2661 spin_unlock(&vc4_hdmi->hw_lock); 2662 2663 return ret; 2664 } 2665 2666 static int vc4_hdmi_cec_enable(struct drm_connector *connector) 2667 { 2668 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 2669 struct drm_device *drm = vc4_hdmi->connector.dev; 2670 /* clock period in microseconds */ 2671 const u32 usecs = 1000000 / CEC_CLOCK_FREQ; 2672 unsigned long flags; 2673 u32 val; 2674 int ret; 2675 int idx; 2676 2677 if (!drm_dev_enter(drm, &idx)) 2678 /* 2679 * We can't return an error code, because the CEC 2680 * framework will emit WARN_ON messages at unbind 2681 * otherwise. 2682 */ 2683 return 0; 2684 2685 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev); 2686 if (ret) { 2687 drm_dev_exit(idx); 2688 return ret; 2689 } 2690 2691 mutex_lock(&vc4_hdmi->mutex); 2692 2693 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 2694 2695 val = HDMI_READ(HDMI_CEC_CNTRL_5); 2696 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET | 2697 VC4_HDMI_CEC_CNT_TO_4700_US_MASK | 2698 VC4_HDMI_CEC_CNT_TO_4500_US_MASK); 2699 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) | 2700 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT); 2701 2702 HDMI_WRITE(HDMI_CEC_CNTRL_5, val | 2703 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET); 2704 HDMI_WRITE(HDMI_CEC_CNTRL_5, val); 2705 HDMI_WRITE(HDMI_CEC_CNTRL_2, 2706 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) | 2707 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) | 2708 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) | 2709 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) | 2710 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT)); 2711 HDMI_WRITE(HDMI_CEC_CNTRL_3, 2712 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) | 2713 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) | 2714 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) | 2715 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT)); 2716 HDMI_WRITE(HDMI_CEC_CNTRL_4, 2717 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) | 2718 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) | 2719 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) | 2720 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT)); 2721 2722 if (!vc4_hdmi->variant->external_irq_controller) 2723 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC); 2724 2725 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 2726 2727 mutex_unlock(&vc4_hdmi->mutex); 2728 drm_dev_exit(idx); 2729 2730 return 0; 2731 } 2732 2733 static int vc4_hdmi_cec_disable(struct drm_connector *connector) 2734 { 2735 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 2736 struct drm_device *drm = vc4_hdmi->connector.dev; 2737 unsigned long flags; 2738 int idx; 2739 2740 if (!drm_dev_enter(drm, &idx)) 2741 /* 2742 * We can't return an error code, because the CEC 2743 * framework will emit WARN_ON messages at unbind 2744 * otherwise. 2745 */ 2746 return 0; 2747 2748 mutex_lock(&vc4_hdmi->mutex); 2749 2750 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 2751 2752 if (!vc4_hdmi->variant->external_irq_controller) 2753 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC); 2754 2755 HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) | 2756 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET); 2757 2758 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 2759 2760 mutex_unlock(&vc4_hdmi->mutex); 2761 2762 pm_runtime_put(&vc4_hdmi->pdev->dev); 2763 2764 drm_dev_exit(idx); 2765 2766 return 0; 2767 } 2768 2769 static int vc4_hdmi_cec_adap_enable(struct drm_connector *connector, bool enable) 2770 { 2771 if (enable) 2772 return vc4_hdmi_cec_enable(connector); 2773 else 2774 return vc4_hdmi_cec_disable(connector); 2775 } 2776 2777 static int vc4_hdmi_cec_adap_log_addr(struct drm_connector *connector, u8 log_addr) 2778 { 2779 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 2780 struct drm_device *drm = vc4_hdmi->connector.dev; 2781 unsigned long flags; 2782 int idx; 2783 2784 if (!drm_dev_enter(drm, &idx)) 2785 /* 2786 * We can't return an error code, because the CEC 2787 * framework will emit WARN_ON messages at unbind 2788 * otherwise. 2789 */ 2790 return 0; 2791 2792 mutex_lock(&vc4_hdmi->mutex); 2793 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 2794 HDMI_WRITE(HDMI_CEC_CNTRL_1, 2795 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) | 2796 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT); 2797 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 2798 mutex_unlock(&vc4_hdmi->mutex); 2799 2800 drm_dev_exit(idx); 2801 2802 return 0; 2803 } 2804 2805 static int vc4_hdmi_cec_adap_transmit(struct drm_connector *connector, u8 attempts, 2806 u32 signal_free_time, struct cec_msg *msg) 2807 { 2808 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 2809 struct drm_device *dev = vc4_hdmi->connector.dev; 2810 unsigned long flags; 2811 u32 val; 2812 unsigned int i; 2813 int idx; 2814 2815 if (!drm_dev_enter(dev, &idx)) 2816 return -ENODEV; 2817 2818 if (msg->len > 16) { 2819 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len); 2820 drm_dev_exit(idx); 2821 return -ENOMEM; 2822 } 2823 2824 mutex_lock(&vc4_hdmi->mutex); 2825 2826 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 2827 2828 for (i = 0; i < msg->len; i += 4) 2829 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2), 2830 (msg->msg[i]) | 2831 (msg->msg[i + 1] << 8) | 2832 (msg->msg[i + 2] << 16) | 2833 (msg->msg[i + 3] << 24)); 2834 2835 val = HDMI_READ(HDMI_CEC_CNTRL_1); 2836 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN; 2837 HDMI_WRITE(HDMI_CEC_CNTRL_1, val); 2838 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK; 2839 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT; 2840 val |= VC4_HDMI_CEC_START_XMIT_BEGIN; 2841 2842 HDMI_WRITE(HDMI_CEC_CNTRL_1, val); 2843 2844 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 2845 mutex_unlock(&vc4_hdmi->mutex); 2846 drm_dev_exit(idx); 2847 2848 return 0; 2849 } 2850 2851 static int vc4_hdmi_cec_init(struct drm_connector *connector) 2852 { 2853 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); 2854 struct platform_device *pdev = vc4_hdmi->pdev; 2855 struct device *dev = &pdev->dev; 2856 int ret; 2857 2858 if (vc4_hdmi->variant->external_irq_controller) { 2859 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-rx"), 2860 vc4_cec_irq_handler_rx_bare, 2861 vc4_cec_irq_handler_rx_thread, 0, 2862 "vc4 hdmi cec rx", vc4_hdmi); 2863 if (ret) 2864 return ret; 2865 2866 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-tx"), 2867 vc4_cec_irq_handler_tx_bare, 2868 vc4_cec_irq_handler_tx_thread, 0, 2869 "vc4 hdmi cec tx", vc4_hdmi); 2870 if (ret) 2871 return ret; 2872 } else { 2873 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0), 2874 vc4_cec_irq_handler, 2875 vc4_cec_irq_handler_thread, 0, 2876 "vc4 hdmi cec", vc4_hdmi); 2877 if (ret) 2878 return ret; 2879 } 2880 2881 return 0; 2882 } 2883 2884 static const struct drm_connector_hdmi_cec_funcs vc4_hdmi_cec_funcs = { 2885 .init = vc4_hdmi_cec_init, 2886 .enable = vc4_hdmi_cec_adap_enable, 2887 .log_addr = vc4_hdmi_cec_adap_log_addr, 2888 .transmit = vc4_hdmi_cec_adap_transmit, 2889 }; 2890 2891 static int vc4_hdmi_cec_register(struct vc4_hdmi *vc4_hdmi) 2892 { 2893 struct platform_device *pdev = vc4_hdmi->pdev; 2894 struct device *dev = &pdev->dev; 2895 2896 if (!of_property_present(dev->of_node, "interrupts")) { 2897 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n"); 2898 return 0; 2899 } 2900 2901 /* 2902 * NOTE: the CEC adapter will be unregistered by drmm cleanup from 2903 * drm_managed_release(), which is called from drm_dev_release() 2904 * during device unbind. 2905 * 2906 * However, the CEC framework already cleans up the CEC adapter 2907 * only when the last user has closed its file descriptor, so we 2908 * don't need to handle it in DRM. 2909 * 2910 * There's still a problematic sequence: if we unregister our 2911 * CEC adapter, but the userspace keeps a handle on the CEC 2912 * adapter but not the DRM device for some reason. In such a 2913 * case, our vc4_hdmi structure will be freed, but the 2914 * cec_adapter structure will have a dangling pointer to what 2915 * used to be our HDMI controller. If we get a CEC call at that 2916 * moment, we could end up with a use-after-free. Fortunately, 2917 * the CEC framework already handles this too, by calling 2918 * cec_is_registered() in cec_ioctl() and cec_poll(). 2919 */ 2920 return drmm_connector_hdmi_cec_register(&vc4_hdmi->connector, 2921 &vc4_hdmi_cec_funcs, 2922 vc4_hdmi->variant->card_name, 2923 1, 2924 &pdev->dev); 2925 } 2926 #else 2927 static int vc4_hdmi_cec_register(struct vc4_hdmi *vc4_hdmi) 2928 { 2929 return 0; 2930 } 2931 #endif 2932 2933 static void vc4_hdmi_free_regset(struct drm_device *drm, void *ptr) 2934 { 2935 struct debugfs_reg32 *regs = ptr; 2936 2937 kfree(regs); 2938 } 2939 2940 static int vc4_hdmi_build_regset(struct drm_device *drm, 2941 struct vc4_hdmi *vc4_hdmi, 2942 struct debugfs_regset32 *regset, 2943 enum vc4_hdmi_regs reg) 2944 { 2945 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant; 2946 struct debugfs_reg32 *regs, *new_regs; 2947 unsigned int count = 0; 2948 unsigned int i; 2949 int ret; 2950 2951 regs = kzalloc_objs(*regs, variant->num_registers); 2952 if (!regs) 2953 return -ENOMEM; 2954 2955 for (i = 0; i < variant->num_registers; i++) { 2956 const struct vc4_hdmi_register *field = &variant->registers[i]; 2957 2958 if (field->reg != reg) 2959 continue; 2960 2961 regs[count].name = field->name; 2962 regs[count].offset = field->offset; 2963 count++; 2964 } 2965 2966 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL); 2967 if (!new_regs) 2968 return -ENOMEM; 2969 2970 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg); 2971 regset->regs = new_regs; 2972 regset->nregs = count; 2973 2974 ret = drmm_add_action_or_reset(drm, vc4_hdmi_free_regset, new_regs); 2975 if (ret) 2976 return ret; 2977 2978 return 0; 2979 } 2980 2981 static int vc4_hdmi_init_resources(struct drm_device *drm, 2982 struct vc4_hdmi *vc4_hdmi) 2983 { 2984 struct platform_device *pdev = vc4_hdmi->pdev; 2985 struct device *dev = &pdev->dev; 2986 int ret; 2987 2988 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0); 2989 if (IS_ERR(vc4_hdmi->hdmicore_regs)) 2990 return PTR_ERR(vc4_hdmi->hdmicore_regs); 2991 2992 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1); 2993 if (IS_ERR(vc4_hdmi->hd_regs)) 2994 return PTR_ERR(vc4_hdmi->hd_regs); 2995 2996 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD); 2997 if (ret) 2998 return ret; 2999 3000 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI); 3001 if (ret) 3002 return ret; 3003 3004 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel"); 3005 if (IS_ERR(vc4_hdmi->pixel_clock)) { 3006 ret = PTR_ERR(vc4_hdmi->pixel_clock); 3007 if (ret != -EPROBE_DEFER) 3008 drm_err(drm, "Failed to get pixel clock\n"); 3009 return ret; 3010 } 3011 3012 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi"); 3013 if (IS_ERR(vc4_hdmi->hsm_clock)) { 3014 drm_err(drm, "Failed to get HDMI state machine clock\n"); 3015 return PTR_ERR(vc4_hdmi->hsm_clock); 3016 } 3017 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock; 3018 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock; 3019 3020 return 0; 3021 } 3022 3023 static int vc5_hdmi_init_resources(struct drm_device *drm, 3024 struct vc4_hdmi *vc4_hdmi) 3025 { 3026 struct platform_device *pdev = vc4_hdmi->pdev; 3027 struct device *dev = &pdev->dev; 3028 struct resource *res; 3029 int ret; 3030 3031 vc4_hdmi->hdmicore_regs = devm_platform_ioremap_resource_byname(pdev, 3032 "hdmi"); 3033 if (IS_ERR(vc4_hdmi->hdmicore_regs)) 3034 return PTR_ERR(vc4_hdmi->hdmicore_regs); 3035 3036 /* This is shared between both HDMI controllers. Cannot 3037 * claim for both instances. Lets not convert to using 3038 * devm_platform_ioremap_resource_byname() like 3039 * the rest 3040 */ 3041 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd"); 3042 if (!res) 3043 return -ENODEV; 3044 3045 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res)); 3046 if (!vc4_hdmi->hd_regs) 3047 return -ENOMEM; 3048 3049 vc4_hdmi->cec_regs = devm_platform_ioremap_resource_byname(pdev, 3050 "cec"); 3051 if (IS_ERR(vc4_hdmi->cec_regs)) 3052 return PTR_ERR(vc4_hdmi->cec_regs); 3053 3054 vc4_hdmi->csc_regs = devm_platform_ioremap_resource_byname(pdev, 3055 "csc"); 3056 if (IS_ERR(vc4_hdmi->csc_regs)) 3057 return PTR_ERR(vc4_hdmi->csc_regs); 3058 3059 vc4_hdmi->dvp_regs = devm_platform_ioremap_resource_byname(pdev, 3060 "dvp"); 3061 if (IS_ERR(vc4_hdmi->dvp_regs)) 3062 return PTR_ERR(vc4_hdmi->dvp_regs); 3063 3064 vc4_hdmi->phy_regs = devm_platform_ioremap_resource_byname(pdev, 3065 "phy"); 3066 3067 if (IS_ERR(vc4_hdmi->phy_regs)) 3068 return PTR_ERR(vc4_hdmi->phy_regs); 3069 3070 vc4_hdmi->ram_regs = devm_platform_ioremap_resource_byname(pdev, 3071 "packet"); 3072 if (IS_ERR(vc4_hdmi->ram_regs)) 3073 return PTR_ERR(vc4_hdmi->ram_regs); 3074 3075 vc4_hdmi->rm_regs = devm_platform_ioremap_resource_byname(pdev, "rm"); 3076 if (IS_ERR(vc4_hdmi->rm_regs)) 3077 return PTR_ERR(vc4_hdmi->rm_regs); 3078 3079 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi"); 3080 if (IS_ERR(vc4_hdmi->hsm_clock)) { 3081 drm_err(drm, "Failed to get HDMI state machine clock\n"); 3082 return PTR_ERR(vc4_hdmi->hsm_clock); 3083 } 3084 3085 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb"); 3086 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) { 3087 drm_err(drm, "Failed to get pixel bvb clock\n"); 3088 return PTR_ERR(vc4_hdmi->pixel_bvb_clock); 3089 } 3090 3091 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio"); 3092 if (IS_ERR(vc4_hdmi->audio_clock)) { 3093 drm_err(drm, "Failed to get audio clock\n"); 3094 return PTR_ERR(vc4_hdmi->audio_clock); 3095 } 3096 3097 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec"); 3098 if (IS_ERR(vc4_hdmi->cec_clock)) { 3099 drm_err(drm, "Failed to get CEC clock\n"); 3100 return PTR_ERR(vc4_hdmi->cec_clock); 3101 } 3102 3103 vc4_hdmi->reset = devm_reset_control_get(dev, NULL); 3104 if (IS_ERR(vc4_hdmi->reset)) { 3105 drm_err(drm, "Failed to get HDMI reset line\n"); 3106 return PTR_ERR(vc4_hdmi->reset); 3107 } 3108 3109 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI); 3110 if (ret) 3111 return ret; 3112 3113 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD); 3114 if (ret) 3115 return ret; 3116 3117 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->cec_regset, VC5_CEC); 3118 if (ret) 3119 return ret; 3120 3121 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->csc_regset, VC5_CSC); 3122 if (ret) 3123 return ret; 3124 3125 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->dvp_regset, VC5_DVP); 3126 if (ret) 3127 return ret; 3128 3129 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->phy_regset, VC5_PHY); 3130 if (ret) 3131 return ret; 3132 3133 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->ram_regset, VC5_RAM); 3134 if (ret) 3135 return ret; 3136 3137 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->rm_regset, VC5_RM); 3138 if (ret) 3139 return ret; 3140 3141 return 0; 3142 } 3143 3144 static int vc4_hdmi_runtime_suspend(struct device *dev) 3145 { 3146 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev); 3147 3148 clk_disable_unprepare(vc4_hdmi->audio_clock); 3149 clk_disable_unprepare(vc4_hdmi->hsm_clock); 3150 3151 return 0; 3152 } 3153 3154 static int vc4_hdmi_runtime_resume(struct device *dev) 3155 { 3156 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev); 3157 unsigned long __maybe_unused flags; 3158 u32 __maybe_unused value; 3159 unsigned long rate; 3160 int ret; 3161 3162 ret = clk_prepare_enable(vc4_hdmi->hsm_clock); 3163 if (ret) 3164 return ret; 3165 3166 /* 3167 * Whenever the RaspberryPi boots without an HDMI monitor 3168 * plugged in, the firmware won't have initialized the HSM clock 3169 * rate and it will be reported as 0. 3170 * 3171 * If we try to access a register of the controller in such a 3172 * case, it will lead to a silent CPU stall. Let's make sure we 3173 * prevent such a case. 3174 */ 3175 rate = clk_get_rate(vc4_hdmi->hsm_clock); 3176 if (!rate) { 3177 ret = -EINVAL; 3178 goto err_disable_clk; 3179 } 3180 3181 ret = clk_prepare_enable(vc4_hdmi->audio_clock); 3182 if (ret) 3183 goto err_disable_clk; 3184 3185 if (vc4_hdmi->variant->reset) 3186 vc4_hdmi->variant->reset(vc4_hdmi); 3187 3188 #ifdef CONFIG_DRM_VC4_HDMI_CEC 3189 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 3190 value = HDMI_READ(HDMI_CEC_CNTRL_1); 3191 /* Set the logical address to Unregistered */ 3192 value |= VC4_HDMI_CEC_ADDR_MASK; 3193 HDMI_WRITE(HDMI_CEC_CNTRL_1, value); 3194 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 3195 3196 vc4_hdmi_cec_update_clk_div(vc4_hdmi); 3197 3198 if (!vc4_hdmi->variant->external_irq_controller) { 3199 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags); 3200 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff); 3201 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags); 3202 } 3203 #endif 3204 3205 return 0; 3206 3207 err_disable_clk: 3208 clk_disable_unprepare(vc4_hdmi->hsm_clock); 3209 return ret; 3210 } 3211 3212 static void vc4_hdmi_put_ddc(void *ptr) 3213 { 3214 struct vc4_hdmi *vc4_hdmi = ptr; 3215 3216 i2c_put_adapter(vc4_hdmi->ddc); 3217 } 3218 3219 static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data) 3220 { 3221 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev); 3222 struct platform_device *pdev = to_platform_device(dev); 3223 struct drm_device *drm = dev_get_drvdata(master); 3224 struct vc4_hdmi *vc4_hdmi; 3225 struct drm_encoder *encoder; 3226 struct device_node *ddc_node; 3227 int ret; 3228 3229 vc4_hdmi = drmm_kzalloc(drm, sizeof(*vc4_hdmi), GFP_KERNEL); 3230 if (!vc4_hdmi) 3231 return -ENOMEM; 3232 3233 ret = drmm_mutex_init(drm, &vc4_hdmi->mutex); 3234 if (ret) 3235 return ret; 3236 3237 spin_lock_init(&vc4_hdmi->hw_lock); 3238 INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq); 3239 3240 dev_set_drvdata(dev, vc4_hdmi); 3241 encoder = &vc4_hdmi->encoder.base; 3242 vc4_hdmi->encoder.type = variant->encoder_type; 3243 vc4_hdmi->encoder.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure; 3244 vc4_hdmi->encoder.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable; 3245 vc4_hdmi->encoder.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable; 3246 vc4_hdmi->encoder.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable; 3247 vc4_hdmi->encoder.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown; 3248 vc4_hdmi->pdev = pdev; 3249 vc4_hdmi->variant = variant; 3250 3251 /* 3252 * Since we don't know the state of the controller and its 3253 * display (if any), let's assume it's always enabled. 3254 * vc4_hdmi_disable_scrambling() will thus run at boot, make 3255 * sure it's disabled, and avoid any inconsistency. 3256 */ 3257 if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK) 3258 vc4_hdmi->scdc_enabled = true; 3259 3260 ret = variant->init_resources(drm, vc4_hdmi); 3261 if (ret) 3262 return ret; 3263 3264 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0); 3265 if (!ddc_node) { 3266 drm_err(drm, "Failed to find ddc node in device tree\n"); 3267 return -ENODEV; 3268 } 3269 3270 vc4_hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node); 3271 of_node_put(ddc_node); 3272 if (!vc4_hdmi->ddc) { 3273 drm_err(drm, "Failed to get ddc i2c adapter by node\n"); 3274 return -EPROBE_DEFER; 3275 } 3276 3277 ret = devm_add_action_or_reset(dev, vc4_hdmi_put_ddc, vc4_hdmi); 3278 if (ret) 3279 return ret; 3280 3281 /* Only use the GPIO HPD pin if present in the DT, otherwise 3282 * we'll use the HDMI core's register. 3283 */ 3284 vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN); 3285 if (IS_ERR(vc4_hdmi->hpd_gpio)) { 3286 return PTR_ERR(vc4_hdmi->hpd_gpio); 3287 } 3288 3289 vc4_hdmi->disable_wifi_frequencies = 3290 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence"); 3291 3292 ret = devm_pm_runtime_enable(dev); 3293 if (ret) 3294 return ret; 3295 3296 /* 3297 * We need to have the device powered up at this point to call 3298 * our reset hook and for the CEC init. 3299 */ 3300 ret = pm_runtime_resume_and_get(dev); 3301 if (ret) 3302 return ret; 3303 3304 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") || 3305 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1") || 3306 of_device_is_compatible(dev->of_node, "brcm,bcm2712-hdmi0") || 3307 of_device_is_compatible(dev->of_node, "brcm,bcm2712-hdmi1")) && 3308 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) { 3309 clk_prepare_enable(vc4_hdmi->pixel_clock); 3310 clk_prepare_enable(vc4_hdmi->hsm_clock); 3311 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock); 3312 } 3313 3314 ret = drmm_encoder_init(drm, encoder, 3315 &vc4_hdmi_encoder_funcs, 3316 DRM_MODE_ENCODER_TMDS, 3317 NULL); 3318 if (ret) 3319 goto err_put_runtime_pm; 3320 3321 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs); 3322 3323 ret = vc4_hdmi_connector_init(drm, vc4_hdmi); 3324 if (ret) 3325 goto err_put_runtime_pm; 3326 3327 ret = vc4_hdmi_hotplug_init(vc4_hdmi); 3328 if (ret) 3329 goto err_put_runtime_pm; 3330 3331 ret = vc4_hdmi_cec_register(vc4_hdmi); 3332 if (ret) 3333 goto err_put_runtime_pm; 3334 3335 ret = vc4_hdmi_audio_init(vc4_hdmi); 3336 if (ret) 3337 goto err_put_runtime_pm; 3338 3339 pm_runtime_put_sync(dev); 3340 3341 return 0; 3342 3343 err_put_runtime_pm: 3344 pm_runtime_put_sync(dev); 3345 3346 return ret; 3347 } 3348 3349 static const struct component_ops vc4_hdmi_ops = { 3350 .bind = vc4_hdmi_bind, 3351 }; 3352 3353 static int vc4_hdmi_dev_probe(struct platform_device *pdev) 3354 { 3355 return component_add(&pdev->dev, &vc4_hdmi_ops); 3356 } 3357 3358 static void vc4_hdmi_dev_remove(struct platform_device *pdev) 3359 { 3360 component_del(&pdev->dev, &vc4_hdmi_ops); 3361 } 3362 3363 static const struct vc4_hdmi_variant bcm2835_variant = { 3364 .encoder_type = VC4_ENCODER_TYPE_HDMI0, 3365 .debugfs_name = "hdmi_regs", 3366 .card_name = "vc4-hdmi", 3367 .max_pixel_clock = 162000000, 3368 .registers = vc4_hdmi_fields, 3369 .num_registers = ARRAY_SIZE(vc4_hdmi_fields), 3370 3371 .init_resources = vc4_hdmi_init_resources, 3372 .csc_setup = vc4_hdmi_csc_setup, 3373 .reset = vc4_hdmi_reset, 3374 .set_timings = vc4_hdmi_set_timings, 3375 .phy_init = vc4_hdmi_phy_init, 3376 .phy_disable = vc4_hdmi_phy_disable, 3377 .phy_rng_enable = vc4_hdmi_phy_rng_enable, 3378 .phy_rng_disable = vc4_hdmi_phy_rng_disable, 3379 .channel_map = vc4_hdmi_channel_map, 3380 .supports_hdr = false, 3381 }; 3382 3383 static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = { 3384 .encoder_type = VC4_ENCODER_TYPE_HDMI0, 3385 .debugfs_name = "hdmi0_regs", 3386 .card_name = "vc4-hdmi-0", 3387 .max_pixel_clock = 600000000, 3388 .registers = vc5_hdmi_hdmi0_fields, 3389 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields), 3390 .phy_lane_mapping = { 3391 PHY_LANE_0, 3392 PHY_LANE_1, 3393 PHY_LANE_2, 3394 PHY_LANE_CK, 3395 }, 3396 .unsupported_odd_h_timings = true, 3397 .external_irq_controller = true, 3398 3399 .init_resources = vc5_hdmi_init_resources, 3400 .csc_setup = vc5_hdmi_csc_setup, 3401 .reset = vc5_hdmi_reset, 3402 .set_timings = vc5_hdmi_set_timings, 3403 .phy_init = vc5_hdmi_phy_init, 3404 .phy_disable = vc5_hdmi_phy_disable, 3405 .phy_rng_enable = vc5_hdmi_phy_rng_enable, 3406 .phy_rng_disable = vc5_hdmi_phy_rng_disable, 3407 .channel_map = vc5_hdmi_channel_map, 3408 .supports_hdr = true, 3409 .hp_detect = vc5_hdmi_hp_detect, 3410 }; 3411 3412 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = { 3413 .encoder_type = VC4_ENCODER_TYPE_HDMI1, 3414 .debugfs_name = "hdmi1_regs", 3415 .card_name = "vc4-hdmi-1", 3416 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK, 3417 .registers = vc5_hdmi_hdmi1_fields, 3418 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields), 3419 .phy_lane_mapping = { 3420 PHY_LANE_1, 3421 PHY_LANE_0, 3422 PHY_LANE_CK, 3423 PHY_LANE_2, 3424 }, 3425 .unsupported_odd_h_timings = true, 3426 .external_irq_controller = true, 3427 3428 .init_resources = vc5_hdmi_init_resources, 3429 .csc_setup = vc5_hdmi_csc_setup, 3430 .reset = vc5_hdmi_reset, 3431 .set_timings = vc5_hdmi_set_timings, 3432 .phy_init = vc5_hdmi_phy_init, 3433 .phy_disable = vc5_hdmi_phy_disable, 3434 .phy_rng_enable = vc5_hdmi_phy_rng_enable, 3435 .phy_rng_disable = vc5_hdmi_phy_rng_disable, 3436 .channel_map = vc5_hdmi_channel_map, 3437 .supports_hdr = true, 3438 .hp_detect = vc5_hdmi_hp_detect, 3439 }; 3440 3441 static const struct vc4_hdmi_variant bcm2712_hdmi0_variant = { 3442 .encoder_type = VC4_ENCODER_TYPE_HDMI0, 3443 .debugfs_name = "hdmi0_regs", 3444 .card_name = "vc4-hdmi-0", 3445 .max_pixel_clock = 600000000, 3446 .registers = vc6_hdmi_hdmi0_fields, 3447 .num_registers = ARRAY_SIZE(vc6_hdmi_hdmi0_fields), 3448 .phy_lane_mapping = { 3449 PHY_LANE_0, 3450 PHY_LANE_1, 3451 PHY_LANE_2, 3452 PHY_LANE_CK, 3453 }, 3454 .unsupported_odd_h_timings = false, 3455 .external_irq_controller = true, 3456 3457 .init_resources = vc5_hdmi_init_resources, 3458 .csc_setup = vc5_hdmi_csc_setup, 3459 .reset = vc5_hdmi_reset, 3460 .set_timings = vc5_hdmi_set_timings, 3461 .phy_init = vc6_hdmi_phy_init, 3462 .phy_disable = vc6_hdmi_phy_disable, 3463 .channel_map = vc5_hdmi_channel_map, 3464 .supports_hdr = true, 3465 .hp_detect = vc5_hdmi_hp_detect, 3466 }; 3467 3468 static const struct vc4_hdmi_variant bcm2712_hdmi1_variant = { 3469 .encoder_type = VC4_ENCODER_TYPE_HDMI1, 3470 .debugfs_name = "hdmi1_regs", 3471 .card_name = "vc4-hdmi-1", 3472 .max_pixel_clock = 600000000, 3473 .registers = vc6_hdmi_hdmi1_fields, 3474 .num_registers = ARRAY_SIZE(vc6_hdmi_hdmi1_fields), 3475 .phy_lane_mapping = { 3476 PHY_LANE_0, 3477 PHY_LANE_1, 3478 PHY_LANE_2, 3479 PHY_LANE_CK, 3480 }, 3481 .unsupported_odd_h_timings = false, 3482 .external_irq_controller = true, 3483 3484 .init_resources = vc5_hdmi_init_resources, 3485 .csc_setup = vc5_hdmi_csc_setup, 3486 .reset = vc5_hdmi_reset, 3487 .set_timings = vc5_hdmi_set_timings, 3488 .phy_init = vc6_hdmi_phy_init, 3489 .phy_disable = vc6_hdmi_phy_disable, 3490 .channel_map = vc5_hdmi_channel_map, 3491 .supports_hdr = true, 3492 .hp_detect = vc5_hdmi_hp_detect, 3493 }; 3494 3495 static const struct of_device_id vc4_hdmi_dt_match[] = { 3496 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant }, 3497 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant }, 3498 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant }, 3499 { .compatible = "brcm,bcm2712-hdmi0", .data = &bcm2712_hdmi0_variant }, 3500 { .compatible = "brcm,bcm2712-hdmi1", .data = &bcm2712_hdmi1_variant }, 3501 {} 3502 }; 3503 3504 static const struct dev_pm_ops vc4_hdmi_pm_ops = { 3505 SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend, 3506 vc4_hdmi_runtime_resume, 3507 NULL) 3508 }; 3509 3510 struct platform_driver vc4_hdmi_driver = { 3511 .probe = vc4_hdmi_dev_probe, 3512 .remove = vc4_hdmi_dev_remove, 3513 .driver = { 3514 .name = "vc4_hdmi", 3515 .of_match_table = vc4_hdmi_dt_match, 3516 .pm = &vc4_hdmi_pm_ops, 3517 }, 3518 }; 3519