1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Broadcom
4 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
5 * Copyright (C) 2013 Red Hat
10 * DOC: VC4 Falcon HDMI module
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.
16 * HDMI infoframes are kept within a small packet ram, where each
17 * packet can be individually enabled for including in a frame.
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
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.
30 * The driver does not yet support CEC control, though the HDMI
31 * encoder block has CEC support.
34 #include <drm/display/drm_hdmi_helper.h>
35 #include <drm/display/drm_scdc_helper.h>
36 #include <drm/drm_atomic_helper.h>
37 #include <drm/drm_drv.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_probe_helper.h>
40 #include <drm/drm_simple_kms_helper.h>
41 #include <linux/clk.h>
42 #include <linux/component.h>
43 #include <linux/gpio/consumer.h>
44 #include <linux/i2c.h>
46 #include <linux/of_address.h>
47 #include <linux/pm_runtime.h>
48 #include <linux/rational.h>
49 #include <linux/reset.h>
50 #include <sound/dmaengine_pcm.h>
51 #include <sound/hdmi-codec.h>
52 #include <sound/pcm_drm_eld.h>
53 #include <sound/pcm_params.h>
54 #include <sound/soc.h>
55 #include "media/cec.h"
58 #include "vc4_hdmi_regs.h"
61 #define VC5_HDMI_HORZA_HFP_SHIFT 16
62 #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
63 #define VC5_HDMI_HORZA_VPOS BIT(15)
64 #define VC5_HDMI_HORZA_HPOS BIT(14)
65 #define VC5_HDMI_HORZA_HAP_SHIFT 0
66 #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
68 #define VC5_HDMI_HORZB_HBP_SHIFT 16
69 #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
70 #define VC5_HDMI_HORZB_HSP_SHIFT 0
71 #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
73 #define VC5_HDMI_VERTA_VSP_SHIFT 24
74 #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
75 #define VC5_HDMI_VERTA_VFP_SHIFT 16
76 #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
77 #define VC5_HDMI_VERTA_VAL_SHIFT 0
78 #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
80 #define VC5_HDMI_VERTB_VSPO_SHIFT 16
81 #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
83 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
84 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
85 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
86 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
88 #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
90 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
91 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
93 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
94 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
96 #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
98 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
99 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
101 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK VC4_MASK(7, 0)
102 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_SET_AVMUTE BIT(0)
103 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE BIT(4)
105 # define VC4_HD_M_SW_RST BIT(2)
106 # define VC4_HD_M_ENABLE BIT(0)
108 #define HSM_MIN_CLOCK_FREQ 120000000
109 #define CEC_CLOCK_FREQ 40000
111 #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
113 static const char * const output_format_str[] = {
114 [VC4_HDMI_OUTPUT_RGB] = "RGB",
115 [VC4_HDMI_OUTPUT_YUV420] = "YUV 4:2:0",
116 [VC4_HDMI_OUTPUT_YUV422] = "YUV 4:2:2",
117 [VC4_HDMI_OUTPUT_YUV444] = "YUV 4:4:4",
120 static const char *vc4_hdmi_output_fmt_str(enum vc4_hdmi_output_format fmt)
122 if (fmt >= ARRAY_SIZE(output_format_str))
125 return output_format_str[fmt];
128 static unsigned long long
129 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
130 unsigned int bpc, enum vc4_hdmi_output_format fmt);
132 static bool vc4_hdmi_supports_scrambling(struct vc4_hdmi *vc4_hdmi)
134 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
136 lockdep_assert_held(&vc4_hdmi->mutex);
138 if (!display->is_hdmi)
141 if (!display->hdmi.scdc.supported ||
142 !display->hdmi.scdc.scrambling.supported)
148 static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
150 enum vc4_hdmi_output_format fmt)
152 unsigned long long clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
154 return clock > HDMI_14_MAX_TMDS_CLK;
157 static bool vc4_hdmi_is_full_range(struct vc4_hdmi *vc4_hdmi,
158 struct vc4_hdmi_connector_state *vc4_state)
160 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
161 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
163 if (vc4_state->broadcast_rgb == VC4_HDMI_BROADCAST_RGB_LIMITED)
165 else if (vc4_state->broadcast_rgb == VC4_HDMI_BROADCAST_RGB_FULL)
168 return !display->is_hdmi ||
169 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
172 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
174 struct drm_debugfs_entry *entry = m->private;
175 struct vc4_hdmi *vc4_hdmi = entry->file.data;
176 struct drm_device *drm = vc4_hdmi->connector.dev;
177 struct drm_printer p = drm_seq_file_printer(m);
180 if (!drm_dev_enter(drm, &idx))
183 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
184 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
185 drm_print_regset32(&p, &vc4_hdmi->cec_regset);
186 drm_print_regset32(&p, &vc4_hdmi->csc_regset);
187 drm_print_regset32(&p, &vc4_hdmi->dvp_regset);
188 drm_print_regset32(&p, &vc4_hdmi->phy_regset);
189 drm_print_regset32(&p, &vc4_hdmi->ram_regset);
190 drm_print_regset32(&p, &vc4_hdmi->rm_regset);
197 static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
199 struct drm_device *drm = vc4_hdmi->connector.dev;
204 * We can be called by our bind callback, when the
205 * connector->dev pointer might not be initialised yet.
207 if (drm && !drm_dev_enter(drm, &idx))
210 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
212 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
214 HDMI_WRITE(HDMI_M_CTL, 0);
216 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
218 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
219 VC4_HDMI_SW_RESET_HDMI |
220 VC4_HDMI_SW_RESET_FORMAT_DETECT);
222 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
224 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
230 static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
232 struct drm_device *drm = vc4_hdmi->connector.dev;
237 * We can be called by our bind callback, when the
238 * connector->dev pointer might not be initialised yet.
240 if (drm && !drm_dev_enter(drm, &idx))
243 reset_control_reset(vc4_hdmi->reset);
245 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
247 HDMI_WRITE(HDMI_DVP_CTL, 0);
249 HDMI_WRITE(HDMI_CLOCK_STOP,
250 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
252 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
258 #ifdef CONFIG_DRM_VC4_HDMI_CEC
259 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
261 struct drm_device *drm = vc4_hdmi->connector.dev;
262 unsigned long cec_rate;
269 * This function is called by our runtime_resume implementation
270 * and thus at bind time, when we haven't registered our
271 * connector yet and thus don't have a pointer to the DRM
274 if (drm && !drm_dev_enter(drm, &idx))
277 cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
279 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
281 value = HDMI_READ(HDMI_CEC_CNTRL_1);
282 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
285 * Set the clock divider: the hsm_clock rate and this divider
286 * setting will give a 40 kHz CEC clock.
288 clk_cnt = cec_rate / CEC_CLOCK_FREQ;
289 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
290 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
292 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
298 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
301 static int reset_pipe(struct drm_crtc *crtc,
302 struct drm_modeset_acquire_ctx *ctx)
304 struct drm_atomic_state *state;
305 struct drm_crtc_state *crtc_state;
308 state = drm_atomic_state_alloc(crtc->dev);
312 state->acquire_ctx = ctx;
314 crtc_state = drm_atomic_get_crtc_state(state, crtc);
315 if (IS_ERR(crtc_state)) {
316 ret = PTR_ERR(crtc_state);
320 crtc_state->connectors_changed = true;
322 ret = drm_atomic_commit(state);
324 drm_atomic_state_put(state);
329 static int vc4_hdmi_reset_link(struct drm_connector *connector,
330 struct drm_modeset_acquire_ctx *ctx)
332 struct drm_device *drm;
333 struct vc4_hdmi *vc4_hdmi;
334 struct drm_connector_state *conn_state;
335 struct drm_crtc_state *crtc_state;
336 struct drm_crtc *crtc;
337 bool scrambling_needed;
344 drm = connector->dev;
345 ret = drm_modeset_lock(&drm->mode_config.connection_mutex, ctx);
349 conn_state = connector->state;
350 crtc = conn_state->crtc;
354 ret = drm_modeset_lock(&crtc->mutex, ctx);
358 crtc_state = crtc->state;
359 if (!crtc_state->active)
362 vc4_hdmi = connector_to_vc4_hdmi(connector);
363 mutex_lock(&vc4_hdmi->mutex);
365 if (!vc4_hdmi_supports_scrambling(vc4_hdmi)) {
366 mutex_unlock(&vc4_hdmi->mutex);
370 scrambling_needed = vc4_hdmi_mode_needs_scrambling(&vc4_hdmi->saved_adjusted_mode,
371 vc4_hdmi->output_bpc,
372 vc4_hdmi->output_format);
373 if (!scrambling_needed) {
374 mutex_unlock(&vc4_hdmi->mutex);
378 if (conn_state->commit &&
379 !try_wait_for_completion(&conn_state->commit->hw_done)) {
380 mutex_unlock(&vc4_hdmi->mutex);
384 ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
386 drm_err(drm, "Failed to read TMDS config: %d\n", ret);
387 mutex_unlock(&vc4_hdmi->mutex);
391 if (!!(config & SCDC_SCRAMBLING_ENABLE) == scrambling_needed) {
392 mutex_unlock(&vc4_hdmi->mutex);
396 mutex_unlock(&vc4_hdmi->mutex);
399 * HDMI 2.0 says that one should not send scrambled data
400 * prior to configuring the sink scrambling, and that
401 * TMDS clock/data transmission should be suspended when
402 * changing the TMDS clock rate in the sink. So let's
403 * just do a full modeset here, even though some sinks
404 * would be perfectly happy if were to just reconfigure
405 * the SCDC settings on the fly.
407 return reset_pipe(crtc, ctx);
410 static void vc4_hdmi_handle_hotplug(struct vc4_hdmi *vc4_hdmi,
411 struct drm_modeset_acquire_ctx *ctx,
412 enum drm_connector_status status)
414 struct drm_connector *connector = &vc4_hdmi->connector;
419 * NOTE: This function should really be called with
420 * vc4_hdmi->mutex held, but doing so results in reentrancy
421 * issues since cec_s_phys_addr_from_edid might call
422 * .adap_enable, which leads to that funtion being called with
425 * A similar situation occurs with vc4_hdmi_reset_link() that
426 * will call into our KMS hooks if the scrambling was enabled.
428 * Concurrency isn't an issue at the moment since we don't share
429 * any state with any of the other frameworks so we can ignore
433 if (status == connector_status_disconnected) {
434 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
438 edid = drm_get_edid(connector, vc4_hdmi->ddc);
442 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
446 ret = vc4_hdmi_reset_link(connector, ctx);
447 if (ret == -EDEADLK) {
448 drm_modeset_backoff(ctx);
456 static int vc4_hdmi_connector_detect_ctx(struct drm_connector *connector,
457 struct drm_modeset_acquire_ctx *ctx,
460 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
461 enum drm_connector_status status = connector_status_disconnected;
464 * NOTE: This function should really take vc4_hdmi->mutex, but
465 * doing so results in reentrancy issues since
466 * vc4_hdmi_handle_hotplug() can call into other functions that
467 * would take the mutex while it's held here.
469 * Concurrency isn't an issue at the moment since we don't share
470 * any state with any of the other frameworks so we can ignore
474 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
476 if (vc4_hdmi->hpd_gpio) {
477 if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
478 status = connector_status_connected;
480 if (vc4_hdmi->variant->hp_detect &&
481 vc4_hdmi->variant->hp_detect(vc4_hdmi))
482 status = connector_status_connected;
485 vc4_hdmi_handle_hotplug(vc4_hdmi, ctx, status);
486 pm_runtime_put(&vc4_hdmi->pdev->dev);
491 static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
493 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
494 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
499 * NOTE: This function should really take vc4_hdmi->mutex, but
500 * doing so results in reentrancy issues since
501 * cec_s_phys_addr_from_edid might call .adap_enable, which
502 * leads to that funtion being called with our mutex held.
504 * Concurrency isn't an issue at the moment since we don't share
505 * any state with any of the other frameworks so we can ignore
509 edid = drm_get_edid(connector, vc4_hdmi->ddc);
510 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
514 drm_connector_update_edid_property(connector, edid);
515 ret = drm_add_edid_modes(connector, edid);
518 if (!vc4->hvs->vc5_hdmi_enable_hdmi_20) {
519 struct drm_device *drm = connector->dev;
520 const struct drm_display_mode *mode;
522 list_for_each_entry(mode, &connector->probed_modes, head) {
523 if (vc4_hdmi_mode_needs_scrambling(mode, 8, VC4_HDMI_OUTPUT_RGB)) {
524 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
525 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
533 static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
534 struct drm_atomic_state *state)
536 struct drm_connector_state *old_state =
537 drm_atomic_get_old_connector_state(state, connector);
538 struct vc4_hdmi_connector_state *old_vc4_state =
539 conn_state_to_vc4_hdmi_conn_state(old_state);
540 struct drm_connector_state *new_state =
541 drm_atomic_get_new_connector_state(state, connector);
542 struct vc4_hdmi_connector_state *new_vc4_state =
543 conn_state_to_vc4_hdmi_conn_state(new_state);
544 struct drm_crtc *crtc = new_state->crtc;
549 if (old_state->tv.margins.left != new_state->tv.margins.left ||
550 old_state->tv.margins.right != new_state->tv.margins.right ||
551 old_state->tv.margins.top != new_state->tv.margins.top ||
552 old_state->tv.margins.bottom != new_state->tv.margins.bottom) {
553 struct drm_crtc_state *crtc_state;
556 crtc_state = drm_atomic_get_crtc_state(state, crtc);
557 if (IS_ERR(crtc_state))
558 return PTR_ERR(crtc_state);
561 * Strictly speaking, we should be calling
562 * drm_atomic_helper_check_planes() after our call to
563 * drm_atomic_add_affected_planes(). However, the
564 * connector atomic_check is called as part of
565 * drm_atomic_helper_check_modeset() that already
566 * happens before a call to
567 * drm_atomic_helper_check_planes() in
568 * drm_atomic_helper_check().
570 ret = drm_atomic_add_affected_planes(state, crtc);
575 if (old_state->colorspace != new_state->colorspace ||
576 old_vc4_state->broadcast_rgb != new_vc4_state->broadcast_rgb ||
577 !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
578 struct drm_crtc_state *crtc_state;
580 crtc_state = drm_atomic_get_crtc_state(state, crtc);
581 if (IS_ERR(crtc_state))
582 return PTR_ERR(crtc_state);
584 crtc_state->mode_changed = true;
590 static int vc4_hdmi_connector_get_property(struct drm_connector *connector,
591 const struct drm_connector_state *state,
592 struct drm_property *property,
595 struct drm_device *drm = connector->dev;
596 struct vc4_hdmi *vc4_hdmi =
597 connector_to_vc4_hdmi(connector);
598 const struct vc4_hdmi_connector_state *vc4_conn_state =
599 conn_state_to_vc4_hdmi_conn_state(state);
601 if (property == vc4_hdmi->broadcast_rgb_property) {
602 *val = vc4_conn_state->broadcast_rgb;
604 drm_dbg(drm, "Unknown property [PROP:%d:%s]\n",
605 property->base.id, property->name);
612 static int vc4_hdmi_connector_set_property(struct drm_connector *connector,
613 struct drm_connector_state *state,
614 struct drm_property *property,
617 struct drm_device *drm = connector->dev;
618 struct vc4_hdmi *vc4_hdmi =
619 connector_to_vc4_hdmi(connector);
620 struct vc4_hdmi_connector_state *vc4_conn_state =
621 conn_state_to_vc4_hdmi_conn_state(state);
623 if (property == vc4_hdmi->broadcast_rgb_property) {
624 vc4_conn_state->broadcast_rgb = val;
628 drm_dbg(drm, "Unknown property [PROP:%d:%s]\n",
629 property->base.id, property->name);
633 static void vc4_hdmi_connector_reset(struct drm_connector *connector)
635 struct vc4_hdmi_connector_state *old_state =
636 conn_state_to_vc4_hdmi_conn_state(connector->state);
637 struct vc4_hdmi_connector_state *new_state =
638 kzalloc(sizeof(*new_state), GFP_KERNEL);
640 if (connector->state)
641 __drm_atomic_helper_connector_destroy_state(connector->state);
644 __drm_atomic_helper_connector_reset(connector, &new_state->base);
649 new_state->base.max_bpc = 8;
650 new_state->base.max_requested_bpc = 8;
651 new_state->output_format = VC4_HDMI_OUTPUT_RGB;
652 new_state->broadcast_rgb = VC4_HDMI_BROADCAST_RGB_AUTO;
653 drm_atomic_helper_connector_tv_margins_reset(connector);
656 static struct drm_connector_state *
657 vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
659 struct drm_connector_state *conn_state = connector->state;
660 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
661 struct vc4_hdmi_connector_state *new_state;
663 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
667 new_state->tmds_char_rate = vc4_state->tmds_char_rate;
668 new_state->output_bpc = vc4_state->output_bpc;
669 new_state->output_format = vc4_state->output_format;
670 new_state->broadcast_rgb = vc4_state->broadcast_rgb;
671 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
673 return &new_state->base;
676 static void vc4_hdmi_connector_destroy_state(struct drm_connector *connector,
677 struct drm_connector_state *state)
679 struct vc4_hdmi_connector_state *vc4_state =
680 conn_state_to_vc4_hdmi_conn_state(state);
682 __drm_atomic_helper_connector_destroy_state(state);
686 static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
687 .fill_modes = drm_helper_probe_single_connector_modes,
688 .reset = vc4_hdmi_connector_reset,
689 .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
690 .atomic_destroy_state = vc4_hdmi_connector_destroy_state,
691 .atomic_get_property = vc4_hdmi_connector_get_property,
692 .atomic_set_property = vc4_hdmi_connector_set_property,
695 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
696 .detect_ctx = vc4_hdmi_connector_detect_ctx,
697 .get_modes = vc4_hdmi_connector_get_modes,
698 .atomic_check = vc4_hdmi_connector_atomic_check,
701 static const struct drm_prop_enum_list broadcast_rgb_names[] = {
702 { VC4_HDMI_BROADCAST_RGB_AUTO, "Automatic" },
703 { VC4_HDMI_BROADCAST_RGB_FULL, "Full" },
704 { VC4_HDMI_BROADCAST_RGB_LIMITED, "Limited 16:235" },
708 vc4_hdmi_attach_broadcast_rgb_property(struct drm_device *dev,
709 struct vc4_hdmi *vc4_hdmi)
711 struct drm_property *prop = vc4_hdmi->broadcast_rgb_property;
714 prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
717 ARRAY_SIZE(broadcast_rgb_names));
721 vc4_hdmi->broadcast_rgb_property = prop;
724 drm_object_attach_property(&vc4_hdmi->connector.base, prop,
725 VC4_HDMI_BROADCAST_RGB_AUTO);
728 static int vc4_hdmi_connector_init(struct drm_device *dev,
729 struct vc4_hdmi *vc4_hdmi)
731 struct drm_connector *connector = &vc4_hdmi->connector;
732 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
735 ret = drmm_connector_init(dev, connector,
736 &vc4_hdmi_connector_funcs,
737 DRM_MODE_CONNECTOR_HDMIA,
742 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
745 * Some of the properties below require access to state, like bpc.
746 * Allocate some default initial connector state with our reset helper.
748 if (connector->funcs->reset)
749 connector->funcs->reset(connector);
751 /* Create and attach TV margin props to this connector. */
752 ret = drm_mode_create_tv_margin_properties(dev);
756 ret = drm_mode_create_hdmi_colorspace_property(connector, 0);
760 drm_connector_attach_colorspace_property(connector);
761 drm_connector_attach_tv_margin_properties(connector);
762 drm_connector_attach_max_bpc_property(connector, 8, 12);
764 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
765 DRM_CONNECTOR_POLL_DISCONNECT);
767 connector->interlace_allowed = 1;
768 connector->doublescan_allowed = 0;
769 connector->stereo_allowed = 1;
771 if (vc4_hdmi->variant->supports_hdr)
772 drm_connector_attach_hdr_output_metadata_property(connector);
774 vc4_hdmi_attach_broadcast_rgb_property(dev, vc4_hdmi);
776 drm_connector_attach_encoder(connector, encoder);
781 static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
782 enum hdmi_infoframe_type type,
785 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
786 struct drm_device *drm = vc4_hdmi->connector.dev;
787 u32 packet_id = type - 0x80;
792 if (!drm_dev_enter(drm, &idx))
795 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
796 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
797 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
798 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
801 ret = wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
802 BIT(packet_id)), 100);
809 static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
810 union hdmi_infoframe *frame)
812 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
813 struct drm_device *drm = vc4_hdmi->connector.dev;
814 u32 packet_id = frame->any.type - 0x80;
815 const struct vc4_hdmi_register *ram_packet_start =
816 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
817 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
818 u32 packet_reg_next = ram_packet_start->offset +
819 VC4_HDMI_PACKET_STRIDE * (packet_id + 1);
820 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
821 ram_packet_start->reg);
822 uint8_t buffer[VC4_HDMI_PACKET_STRIDE] = {};
828 if (!drm_dev_enter(drm, &idx))
831 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
832 VC4_HDMI_RAM_PACKET_ENABLE),
833 "Packet RAM has to be on to store the packet.");
835 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
839 ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
841 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
845 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
847 for (i = 0; i < len; i += 7) {
848 writel(buffer[i + 0] << 0 |
854 writel(buffer[i + 3] << 0 |
856 buffer[i + 5] << 16 |
863 * clear remainder of packet ram as it's included in the
864 * infoframe and triggers a checksum error on hdmi analyser
866 for (; packet_reg < packet_reg_next; packet_reg += 4)
867 writel(0, base + packet_reg);
869 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
870 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
872 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
874 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
875 BIT(packet_id)), 100);
877 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
883 static void vc4_hdmi_avi_infoframe_colorspace(struct hdmi_avi_infoframe *frame,
884 enum vc4_hdmi_output_format fmt)
887 case VC4_HDMI_OUTPUT_RGB:
888 frame->colorspace = HDMI_COLORSPACE_RGB;
891 case VC4_HDMI_OUTPUT_YUV420:
892 frame->colorspace = HDMI_COLORSPACE_YUV420;
895 case VC4_HDMI_OUTPUT_YUV422:
896 frame->colorspace = HDMI_COLORSPACE_YUV422;
899 case VC4_HDMI_OUTPUT_YUV444:
900 frame->colorspace = HDMI_COLORSPACE_YUV444;
908 static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
910 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
911 struct drm_connector *connector = &vc4_hdmi->connector;
912 struct drm_connector_state *cstate = connector->state;
913 struct vc4_hdmi_connector_state *vc4_state =
914 conn_state_to_vc4_hdmi_conn_state(cstate);
915 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
916 union hdmi_infoframe frame;
919 lockdep_assert_held(&vc4_hdmi->mutex);
921 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
924 DRM_ERROR("couldn't fill AVI infoframe\n");
928 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
930 vc4_hdmi_is_full_range(vc4_hdmi, vc4_state) ?
931 HDMI_QUANTIZATION_RANGE_FULL :
932 HDMI_QUANTIZATION_RANGE_LIMITED);
933 drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
934 vc4_hdmi_avi_infoframe_colorspace(&frame.avi, vc4_state->output_format);
935 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
937 vc4_hdmi_write_infoframe(encoder, &frame);
940 static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
942 union hdmi_infoframe frame;
945 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
947 DRM_ERROR("couldn't fill SPD infoframe\n");
951 frame.spd.sdi = HDMI_SPD_SDI_PC;
953 vc4_hdmi_write_infoframe(encoder, &frame);
956 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
958 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
959 struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
960 union hdmi_infoframe frame;
962 memcpy(&frame.audio, audio, sizeof(*audio));
964 if (vc4_hdmi->packet_ram_enabled)
965 vc4_hdmi_write_infoframe(encoder, &frame);
968 static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
970 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
971 struct drm_connector *connector = &vc4_hdmi->connector;
972 struct drm_connector_state *conn_state = connector->state;
973 union hdmi_infoframe frame;
975 lockdep_assert_held(&vc4_hdmi->mutex);
977 if (!vc4_hdmi->variant->supports_hdr)
980 if (!conn_state->hdr_output_metadata)
983 if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
986 vc4_hdmi_write_infoframe(encoder, &frame);
989 static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
991 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
993 lockdep_assert_held(&vc4_hdmi->mutex);
995 vc4_hdmi_set_avi_infoframe(encoder);
996 vc4_hdmi_set_spd_infoframe(encoder);
998 * If audio was streaming, then we need to reenabled the audio
999 * infoframe here during encoder_enable.
1001 if (vc4_hdmi->audio.streaming)
1002 vc4_hdmi_set_audio_infoframe(encoder);
1004 vc4_hdmi_set_hdr_infoframe(encoder);
1007 #define SCRAMBLING_POLLING_DELAY_MS 1000
1009 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
1011 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1012 struct drm_connector *connector = &vc4_hdmi->connector;
1013 struct drm_device *drm = connector->dev;
1014 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1015 unsigned long flags;
1018 lockdep_assert_held(&vc4_hdmi->mutex);
1020 if (!vc4_hdmi_supports_scrambling(vc4_hdmi))
1023 if (!vc4_hdmi_mode_needs_scrambling(mode,
1024 vc4_hdmi->output_bpc,
1025 vc4_hdmi->output_format))
1028 if (!drm_dev_enter(drm, &idx))
1031 drm_scdc_set_high_tmds_clock_ratio(connector, true);
1032 drm_scdc_set_scrambling(connector, true);
1034 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1035 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
1036 VC5_HDMI_SCRAMBLER_CTL_ENABLE);
1037 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1041 vc4_hdmi->scdc_enabled = true;
1043 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
1044 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
1047 static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
1049 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1050 struct drm_connector *connector = &vc4_hdmi->connector;
1051 struct drm_device *drm = connector->dev;
1052 unsigned long flags;
1055 lockdep_assert_held(&vc4_hdmi->mutex);
1057 if (!vc4_hdmi->scdc_enabled)
1060 vc4_hdmi->scdc_enabled = false;
1062 if (delayed_work_pending(&vc4_hdmi->scrambling_work))
1063 cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
1065 if (!drm_dev_enter(drm, &idx))
1068 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1069 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
1070 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
1071 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1073 drm_scdc_set_scrambling(connector, false);
1074 drm_scdc_set_high_tmds_clock_ratio(connector, false);
1079 static void vc4_hdmi_scrambling_wq(struct work_struct *work)
1081 struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
1084 struct drm_connector *connector = &vc4_hdmi->connector;
1086 if (drm_scdc_get_scrambling_status(connector))
1089 drm_scdc_set_high_tmds_clock_ratio(connector, true);
1090 drm_scdc_set_scrambling(connector, true);
1092 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
1093 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
1096 static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
1097 struct drm_atomic_state *state)
1099 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1100 struct drm_device *drm = vc4_hdmi->connector.dev;
1101 unsigned long flags;
1104 mutex_lock(&vc4_hdmi->mutex);
1106 vc4_hdmi->packet_ram_enabled = false;
1108 if (!drm_dev_enter(drm, &idx))
1111 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1113 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
1115 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
1117 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1121 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1122 HDMI_WRITE(HDMI_VID_CTL,
1123 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
1124 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1126 vc4_hdmi_disable_scrambling(encoder);
1131 mutex_unlock(&vc4_hdmi->mutex);
1134 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
1135 struct drm_atomic_state *state)
1137 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1138 struct drm_device *drm = vc4_hdmi->connector.dev;
1139 unsigned long flags;
1143 mutex_lock(&vc4_hdmi->mutex);
1145 if (!drm_dev_enter(drm, &idx))
1148 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1149 HDMI_WRITE(HDMI_VID_CTL,
1150 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
1151 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1153 if (vc4_hdmi->variant->phy_disable)
1154 vc4_hdmi->variant->phy_disable(vc4_hdmi);
1156 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
1157 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1159 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
1161 DRM_ERROR("Failed to release power domain: %d\n", ret);
1166 mutex_unlock(&vc4_hdmi->mutex);
1169 static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1170 struct drm_connector_state *state,
1171 const struct drm_display_mode *mode)
1173 struct vc4_hdmi_connector_state *vc4_state =
1174 conn_state_to_vc4_hdmi_conn_state(state);
1175 struct drm_device *drm = vc4_hdmi->connector.dev;
1176 unsigned long flags;
1180 if (!drm_dev_enter(drm, &idx))
1183 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1185 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
1186 VC4_HD_CSC_CTL_ORDER);
1188 if (!vc4_hdmi_is_full_range(vc4_hdmi, vc4_state)) {
1189 /* CEA VICs other than #1 requre limited range RGB
1190 * output unless overridden by an AVI infoframe.
1191 * Apply a colorspace conversion to squash 0-255 down
1192 * to 16-235. The matrix here is:
1199 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
1200 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
1201 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1202 VC4_HD_CSC_CTL_MODE);
1204 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
1205 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
1206 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
1207 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
1208 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
1209 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
1212 /* The RGB order applies even when CSC is disabled. */
1213 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1215 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1221 * Matrices for (internal) RGB to RGB output.
1223 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1225 static const u16 vc5_hdmi_csc_full_rgb_to_rgb[2][3][4] = {
1228 * Full range - unity
1234 { 0x2000, 0x0000, 0x0000, 0x0000 },
1235 { 0x0000, 0x2000, 0x0000, 0x0000 },
1236 { 0x0000, 0x0000, 0x2000, 0x0000 },
1242 * CEA VICs other than #1 require limited range RGB
1243 * output unless overridden by an AVI infoframe. Apply a
1244 * colorspace conversion to squash 0-255 down to 16-235.
1245 * The matrix here is:
1251 { 0x1b80, 0x0000, 0x0000, 0x0400 },
1252 { 0x0000, 0x1b80, 0x0000, 0x0400 },
1253 { 0x0000, 0x0000, 0x1b80, 0x0400 },
1258 * Conversion between Full Range RGB and YUV using the BT.601 Colorspace
1260 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1262 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt601[2][3][4] = {
1267 * [ 0.299000 0.587000 0.114000 0 ]
1268 * [ -0.168736 -0.331264 0.500000 128 ]
1269 * [ 0.500000 -0.418688 -0.081312 128 ]
1271 { 0x0991, 0x12c9, 0x03a6, 0x0000 },
1272 { 0xfa9b, 0xf567, 0x1000, 0x2000 },
1273 { 0x1000, 0xf29b, 0xfd67, 0x2000 },
1278 * [ 0.255785 0.502160 0.097523 16 ]
1279 * [ -0.147644 -0.289856 0.437500 128 ]
1280 * [ 0.437500 -0.366352 -0.071148 128 ]
1282 { 0x082f, 0x1012, 0x031f, 0x0400 },
1283 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
1284 { 0x0e00, 0xf448, 0xfdba, 0x2000 },
1289 * Conversion between Full Range RGB and YUV using the BT.709 Colorspace
1291 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1293 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt709[2][3][4] = {
1298 * [ 0.212600 0.715200 0.072200 0 ]
1299 * [ -0.114572 -0.385428 0.500000 128 ]
1300 * [ 0.500000 -0.454153 -0.045847 128 ]
1302 { 0x06ce, 0x16e3, 0x024f, 0x0000 },
1303 { 0xfc56, 0xf3ac, 0x1000, 0x2000 },
1304 { 0x1000, 0xf179, 0xfe89, 0x2000 },
1310 * [ 0.181906 0.611804 0.061758 16 ]
1311 * [ -0.100268 -0.337232 0.437500 128 ]
1312 * [ 0.437500 -0.397386 -0.040114 128 ]
1314 { 0x05d2, 0x1394, 0x01fa, 0x0400 },
1315 { 0xfccc, 0xf536, 0x0e00, 0x2000 },
1316 { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
1321 * Conversion between Full Range RGB and YUV using the BT.2020 Colorspace
1323 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1325 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt2020[2][3][4] = {
1330 * [ 0.262700 0.678000 0.059300 0 ]
1331 * [ -0.139630 -0.360370 0.500000 128 ]
1332 * [ 0.500000 -0.459786 -0.040214 128 ]
1334 { 0x0868, 0x15b2, 0x01e6, 0x0000 },
1335 { 0xfb89, 0xf479, 0x1000, 0x2000 },
1336 { 0x1000, 0xf14a, 0xfeb8, 0x2000 },
1341 * [ 0.224732 0.580008 0.050729 16 ]
1342 * [ -0.122176 -0.315324 0.437500 128 ]
1343 * [ 0.437500 -0.402312 -0.035188 128 ]
1345 { 0x082f, 0x1012, 0x031f, 0x0400 },
1346 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
1347 { 0x0e00, 0xf448, 0xfdba, 0x2000 },
1351 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
1352 const u16 coeffs[3][4])
1354 lockdep_assert_held(&vc4_hdmi->hw_lock);
1356 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
1357 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
1358 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
1359 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
1360 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
1361 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
1364 static void vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi *vc4_hdmi,
1365 const u16 coeffs[3][4])
1367 lockdep_assert_held(&vc4_hdmi->hw_lock);
1369 /* YUV444 needs the CSC matrices using the channels in a different order */
1370 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[1][1] << 16) | coeffs[1][0]);
1371 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[1][3] << 16) | coeffs[1][2]);
1372 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[2][1] << 16) | coeffs[2][0]);
1373 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[2][3] << 16) | coeffs[2][2]);
1374 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[0][1] << 16) | coeffs[0][0]);
1375 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[0][3] << 16) | coeffs[0][2]);
1379 (*vc5_hdmi_find_yuv_csc_coeffs(struct vc4_hdmi *vc4_hdmi, u32 colorspace, bool limited))[4]
1381 switch (colorspace) {
1382 case DRM_MODE_COLORIMETRY_SMPTE_170M_YCC:
1383 case DRM_MODE_COLORIMETRY_XVYCC_601:
1384 case DRM_MODE_COLORIMETRY_SYCC_601:
1385 case DRM_MODE_COLORIMETRY_OPYCC_601:
1386 case DRM_MODE_COLORIMETRY_BT601_YCC:
1387 return vc5_hdmi_csc_full_rgb_to_yuv_bt601[limited];
1390 case DRM_MODE_COLORIMETRY_NO_DATA:
1391 case DRM_MODE_COLORIMETRY_BT709_YCC:
1392 case DRM_MODE_COLORIMETRY_XVYCC_709:
1393 case DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED:
1394 case DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT:
1395 return vc5_hdmi_csc_full_rgb_to_yuv_bt709[limited];
1397 case DRM_MODE_COLORIMETRY_BT2020_CYCC:
1398 case DRM_MODE_COLORIMETRY_BT2020_YCC:
1399 case DRM_MODE_COLORIMETRY_BT2020_RGB:
1400 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
1401 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
1402 return vc5_hdmi_csc_full_rgb_to_yuv_bt2020[limited];
1406 static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1407 struct drm_connector_state *state,
1408 const struct drm_display_mode *mode)
1410 struct drm_device *drm = vc4_hdmi->connector.dev;
1411 struct vc4_hdmi_connector_state *vc4_state =
1412 conn_state_to_vc4_hdmi_conn_state(state);
1413 unsigned int lim_range = vc4_hdmi_is_full_range(vc4_hdmi, vc4_state) ? 0 : 1;
1414 unsigned long flags;
1415 const u16 (*csc)[4];
1417 u32 if_xbar = 0x543210;
1418 u32 csc_chan_ctl = 0;
1419 u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1420 VC5_MT_CP_CSC_CTL_MODE);
1423 if (!drm_dev_enter(drm, &idx))
1426 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1428 switch (vc4_state->output_format) {
1429 case VC4_HDMI_OUTPUT_YUV444:
1430 csc = vc5_hdmi_find_yuv_csc_coeffs(vc4_hdmi, state->colorspace, !!lim_range);
1432 vc5_hdmi_set_csc_coeffs_swap(vc4_hdmi, csc);
1435 case VC4_HDMI_OUTPUT_YUV422:
1436 csc = vc5_hdmi_find_yuv_csc_coeffs(vc4_hdmi, state->colorspace, !!lim_range);
1438 csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
1439 VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
1440 VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
1441 VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
1443 csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
1444 VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
1446 if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
1447 VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);
1449 vc5_hdmi_set_csc_coeffs(vc4_hdmi, csc);
1452 case VC4_HDMI_OUTPUT_RGB:
1455 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_rgb[lim_range]);
1462 HDMI_WRITE(HDMI_VEC_INTERFACE_CFG, if_cfg);
1463 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, if_xbar);
1464 HDMI_WRITE(HDMI_CSC_CHANNEL_CTL, csc_chan_ctl);
1465 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1467 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1472 static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1473 struct drm_connector_state *state,
1474 const struct drm_display_mode *mode)
1476 struct drm_device *drm = vc4_hdmi->connector.dev;
1477 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1478 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1479 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1480 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1481 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1482 VC4_HDMI_VERTA_VSP) |
1483 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1484 VC4_HDMI_VERTA_VFP) |
1485 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
1486 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1487 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1489 VC4_HDMI_VERTB_VBP));
1490 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1491 VC4_SET_FIELD(mode->crtc_vtotal -
1492 mode->crtc_vsync_end,
1493 VC4_HDMI_VERTB_VBP));
1494 unsigned long flags;
1498 if (!drm_dev_enter(drm, &idx))
1501 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1503 HDMI_WRITE(HDMI_HORZA,
1504 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
1505 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
1506 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1507 VC4_HDMI_HORZA_HAP));
1509 HDMI_WRITE(HDMI_HORZB,
1510 VC4_SET_FIELD((mode->htotal -
1511 mode->hsync_end) * pixel_rep,
1512 VC4_HDMI_HORZB_HBP) |
1513 VC4_SET_FIELD((mode->hsync_end -
1514 mode->hsync_start) * pixel_rep,
1515 VC4_HDMI_HORZB_HSP) |
1516 VC4_SET_FIELD((mode->hsync_start -
1517 mode->hdisplay) * pixel_rep,
1518 VC4_HDMI_HORZB_HFP));
1520 HDMI_WRITE(HDMI_VERTA0, verta);
1521 HDMI_WRITE(HDMI_VERTA1, verta);
1523 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1524 HDMI_WRITE(HDMI_VERTB1, vertb);
1526 reg = HDMI_READ(HDMI_MISC_CONTROL);
1527 reg &= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1528 reg |= VC4_SET_FIELD(pixel_rep - 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP);
1529 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1531 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1536 static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1537 struct drm_connector_state *state,
1538 const struct drm_display_mode *mode)
1540 struct drm_device *drm = vc4_hdmi->connector.dev;
1541 const struct vc4_hdmi_connector_state *vc4_state =
1542 conn_state_to_vc4_hdmi_conn_state(state);
1543 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1544 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1545 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1546 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1547 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1548 VC5_HDMI_VERTA_VSP) |
1549 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1550 VC5_HDMI_VERTA_VFP) |
1551 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
1552 u32 vertb = (VC4_SET_FIELD(mode->htotal >> (2 - pixel_rep),
1553 VC5_HDMI_VERTB_VSPO) |
1554 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1556 VC4_HDMI_VERTB_VBP));
1557 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
1558 VC4_SET_FIELD(mode->crtc_vtotal -
1559 mode->crtc_vsync_end,
1560 VC4_HDMI_VERTB_VBP));
1561 unsigned long flags;
1566 if (!drm_dev_enter(drm, &idx))
1569 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1571 HDMI_WRITE(HDMI_HORZA,
1572 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
1573 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
1574 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1575 VC5_HDMI_HORZA_HAP) |
1576 VC4_SET_FIELD((mode->hsync_start -
1577 mode->hdisplay) * pixel_rep,
1578 VC5_HDMI_HORZA_HFP));
1580 HDMI_WRITE(HDMI_HORZB,
1581 VC4_SET_FIELD((mode->htotal -
1582 mode->hsync_end) * pixel_rep,
1583 VC5_HDMI_HORZB_HBP) |
1584 VC4_SET_FIELD((mode->hsync_end -
1585 mode->hsync_start) * pixel_rep,
1586 VC5_HDMI_HORZB_HSP));
1588 HDMI_WRITE(HDMI_VERTA0, verta);
1589 HDMI_WRITE(HDMI_VERTA1, verta);
1591 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1592 HDMI_WRITE(HDMI_VERTB1, vertb);
1594 switch (vc4_state->output_bpc) {
1608 * YCC422 is always 36-bit and not considered deep colour so
1609 * doesn't signal in GCP.
1611 if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
1615 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
1616 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
1617 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
1618 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
1619 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
1620 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
1622 reg = HDMI_READ(HDMI_GCP_WORD_1);
1623 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
1624 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
1625 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK;
1626 reg |= VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE;
1627 HDMI_WRITE(HDMI_GCP_WORD_1, reg);
1629 reg = HDMI_READ(HDMI_GCP_CONFIG);
1630 reg |= VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
1631 HDMI_WRITE(HDMI_GCP_CONFIG, reg);
1633 reg = HDMI_READ(HDMI_MISC_CONTROL);
1634 reg &= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1635 reg |= VC4_SET_FIELD(pixel_rep - 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP);
1636 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1638 HDMI_WRITE(HDMI_CLOCK_STOP, 0);
1640 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1645 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
1647 struct drm_device *drm = vc4_hdmi->connector.dev;
1648 unsigned long flags;
1653 if (!drm_dev_enter(drm, &idx))
1656 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1658 drift = HDMI_READ(HDMI_FIFO_CTL);
1659 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
1661 HDMI_WRITE(HDMI_FIFO_CTL,
1662 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1663 HDMI_WRITE(HDMI_FIFO_CTL,
1664 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1666 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1668 usleep_range(1000, 1100);
1670 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1672 HDMI_WRITE(HDMI_FIFO_CTL,
1673 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1674 HDMI_WRITE(HDMI_FIFO_CTL,
1675 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1677 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1679 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
1680 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
1681 WARN_ONCE(ret, "Timeout waiting for "
1682 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
1687 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
1688 struct drm_atomic_state *state)
1690 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1691 struct drm_device *drm = vc4_hdmi->connector.dev;
1692 struct drm_connector *connector = &vc4_hdmi->connector;
1693 struct drm_connector_state *conn_state =
1694 drm_atomic_get_new_connector_state(state, connector);
1695 struct vc4_hdmi_connector_state *vc4_conn_state =
1696 conn_state_to_vc4_hdmi_conn_state(conn_state);
1697 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1698 unsigned long tmds_char_rate = vc4_conn_state->tmds_char_rate;
1699 unsigned long bvb_rate, hsm_rate;
1700 unsigned long flags;
1704 mutex_lock(&vc4_hdmi->mutex);
1706 if (!drm_dev_enter(drm, &idx))
1709 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
1711 DRM_ERROR("Failed to retain power domain: %d\n", ret);
1716 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
1717 * be faster than pixel clock, infinitesimally faster, tested in
1718 * simulation. Otherwise, exact value is unimportant for HDMI
1719 * operation." This conflicts with bcm2835's vc4 documentation, which
1720 * states HSM's clock has to be at least 108% of the pixel clock.
1722 * Real life tests reveal that vc4's firmware statement holds up, and
1723 * users are able to use pixel clocks closer to HSM's, namely for
1724 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
1725 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
1728 * Additionally, the AXI clock needs to be at least 25% of
1729 * pixel clock, but HSM ends up being the limiting factor.
1731 hsm_rate = max_t(unsigned long,
1733 (tmds_char_rate / 100) * 101);
1734 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
1736 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1737 goto err_put_runtime_pm;
1740 ret = clk_set_rate(vc4_hdmi->pixel_clock, tmds_char_rate);
1742 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
1743 goto err_put_runtime_pm;
1746 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
1748 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
1749 goto err_put_runtime_pm;
1753 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
1755 if (tmds_char_rate > 297000000)
1756 bvb_rate = 300000000;
1757 else if (tmds_char_rate > 148500000)
1758 bvb_rate = 150000000;
1760 bvb_rate = 75000000;
1762 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
1764 DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
1765 goto err_disable_pixel_clock;
1768 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
1770 DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
1771 goto err_disable_pixel_clock;
1774 if (vc4_hdmi->variant->phy_init)
1775 vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
1777 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1779 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1780 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1781 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
1782 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
1784 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1786 if (vc4_hdmi->variant->set_timings)
1787 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
1791 mutex_unlock(&vc4_hdmi->mutex);
1795 err_disable_pixel_clock:
1796 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1798 pm_runtime_put(&vc4_hdmi->pdev->dev);
1802 mutex_unlock(&vc4_hdmi->mutex);
1806 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
1807 struct drm_atomic_state *state)
1809 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1810 struct drm_device *drm = vc4_hdmi->connector.dev;
1811 struct drm_connector *connector = &vc4_hdmi->connector;
1812 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1813 struct drm_connector_state *conn_state =
1814 drm_atomic_get_new_connector_state(state, connector);
1815 unsigned long flags;
1818 mutex_lock(&vc4_hdmi->mutex);
1820 if (!drm_dev_enter(drm, &idx))
1823 if (vc4_hdmi->variant->csc_setup)
1824 vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
1826 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1827 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
1828 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1833 mutex_unlock(&vc4_hdmi->mutex);
1836 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
1837 struct drm_atomic_state *state)
1839 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1840 struct drm_device *drm = vc4_hdmi->connector.dev;
1841 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1842 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
1843 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1844 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1845 unsigned long flags;
1849 mutex_lock(&vc4_hdmi->mutex);
1851 if (!drm_dev_enter(drm, &idx))
1854 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1856 HDMI_WRITE(HDMI_VID_CTL,
1857 VC4_HD_VID_CTL_ENABLE |
1858 VC4_HD_VID_CTL_CLRRGB |
1859 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
1860 VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
1861 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
1862 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
1864 HDMI_WRITE(HDMI_VID_CTL,
1865 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1867 if (display->is_hdmi) {
1868 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1869 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1870 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1872 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1874 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1875 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1876 WARN_ONCE(ret, "Timeout waiting for "
1877 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1879 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1880 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
1881 ~(VC4_HDMI_RAM_PACKET_ENABLE));
1882 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1883 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1884 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1886 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1888 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1889 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1890 WARN_ONCE(ret, "Timeout waiting for "
1891 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1894 if (display->is_hdmi) {
1895 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1897 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1898 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
1900 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1901 VC4_HDMI_RAM_PACKET_ENABLE);
1903 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1904 vc4_hdmi->packet_ram_enabled = true;
1906 vc4_hdmi_set_infoframes(encoder);
1909 vc4_hdmi_recenter_fifo(vc4_hdmi);
1910 vc4_hdmi_enable_scrambling(encoder);
1915 mutex_unlock(&vc4_hdmi->mutex);
1918 static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
1919 struct drm_crtc_state *crtc_state,
1920 struct drm_connector_state *conn_state)
1922 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1923 struct vc4_hdmi_connector_state *vc4_state =
1924 conn_state_to_vc4_hdmi_conn_state(conn_state);
1926 mutex_lock(&vc4_hdmi->mutex);
1927 drm_mode_copy(&vc4_hdmi->saved_adjusted_mode,
1928 &crtc_state->adjusted_mode);
1929 vc4_hdmi->output_bpc = vc4_state->output_bpc;
1930 vc4_hdmi->output_format = vc4_state->output_format;
1931 mutex_unlock(&vc4_hdmi->mutex);
1935 vc4_hdmi_sink_supports_format_bpc(const struct vc4_hdmi *vc4_hdmi,
1936 const struct drm_display_info *info,
1937 const struct drm_display_mode *mode,
1938 unsigned int format, unsigned int bpc)
1940 struct drm_device *dev = vc4_hdmi->connector.dev;
1941 u8 vic = drm_match_cea_mode(mode);
1943 if (vic == 1 && bpc != 8) {
1944 drm_dbg(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
1948 if (!info->is_hdmi &&
1949 (format != VC4_HDMI_OUTPUT_RGB || bpc != 8)) {
1950 drm_dbg(dev, "DVI Monitors require an RGB output at 8 bpc\n");
1955 case VC4_HDMI_OUTPUT_RGB:
1956 drm_dbg(dev, "RGB Format, checking the constraints.\n");
1958 if (!(info->color_formats & DRM_COLOR_FORMAT_RGB444))
1961 if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
1962 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
1966 if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
1967 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
1971 drm_dbg(dev, "RGB format supported in that configuration.\n");
1975 case VC4_HDMI_OUTPUT_YUV422:
1976 drm_dbg(dev, "YUV422 format, checking the constraints.\n");
1978 if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR422)) {
1979 drm_dbg(dev, "Sink doesn't support YUV422.\n");
1984 drm_dbg(dev, "YUV422 only supports 12 bpc.\n");
1988 drm_dbg(dev, "YUV422 format supported in that configuration.\n");
1992 case VC4_HDMI_OUTPUT_YUV444:
1993 drm_dbg(dev, "YUV444 format, checking the constraints.\n");
1995 if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR444)) {
1996 drm_dbg(dev, "Sink doesn't support YUV444.\n");
2000 if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) {
2001 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
2005 if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) {
2006 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
2010 drm_dbg(dev, "YUV444 format supported in that configuration.\n");
2018 static enum drm_mode_status
2019 vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi *vc4_hdmi,
2020 const struct drm_display_mode *mode,
2021 unsigned long long clock)
2023 const struct drm_connector *connector = &vc4_hdmi->connector;
2024 const struct drm_display_info *info = &connector->display_info;
2025 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
2027 if (clock > vc4_hdmi->variant->max_pixel_clock)
2028 return MODE_CLOCK_HIGH;
2030 if (!vc4->hvs->vc5_hdmi_enable_hdmi_20 && clock > HDMI_14_MAX_TMDS_CLK)
2031 return MODE_CLOCK_HIGH;
2033 /* 4096x2160@60 is not reliable without overclocking core */
2034 if (!vc4->hvs->vc5_hdmi_enable_4096by2160 &&
2035 mode->hdisplay > 3840 && mode->vdisplay >= 2160 &&
2036 drm_mode_vrefresh(mode) >= 50)
2037 return MODE_CLOCK_HIGH;
2039 if (info->max_tmds_clock && clock > (info->max_tmds_clock * 1000))
2040 return MODE_CLOCK_HIGH;
2045 static unsigned long long
2046 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
2048 enum vc4_hdmi_output_format fmt)
2050 unsigned long long clock = mode->clock * 1000ULL;
2052 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
2055 if (fmt == VC4_HDMI_OUTPUT_YUV422)
2058 clock = clock * bpc;
2065 vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi *vc4_hdmi,
2066 struct vc4_hdmi_connector_state *vc4_state,
2067 const struct drm_display_mode *mode,
2068 unsigned int bpc, unsigned int fmt)
2070 unsigned long long clock;
2072 clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
2073 if (vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode, clock) != MODE_OK)
2076 vc4_state->tmds_char_rate = clock;
2082 vc4_hdmi_encoder_compute_format(const struct vc4_hdmi *vc4_hdmi,
2083 struct vc4_hdmi_connector_state *vc4_state,
2084 const struct drm_display_mode *mode,
2087 struct drm_device *dev = vc4_hdmi->connector.dev;
2088 const struct drm_connector *connector = &vc4_hdmi->connector;
2089 const struct drm_display_info *info = &connector->display_info;
2090 unsigned int format;
2092 drm_dbg(dev, "Trying with an RGB output\n");
2094 format = VC4_HDMI_OUTPUT_RGB;
2095 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
2098 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
2101 vc4_state->output_format = format;
2106 drm_dbg(dev, "Failed, Trying with an YUV422 output\n");
2108 format = VC4_HDMI_OUTPUT_YUV422;
2109 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
2112 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
2115 vc4_state->output_format = format;
2120 drm_dbg(dev, "Failed. No Format Supported for that bpc count.\n");
2126 vc4_hdmi_encoder_compute_config(const struct vc4_hdmi *vc4_hdmi,
2127 struct vc4_hdmi_connector_state *vc4_state,
2128 const struct drm_display_mode *mode)
2130 struct drm_device *dev = vc4_hdmi->connector.dev;
2131 struct drm_connector_state *conn_state = &vc4_state->base;
2132 unsigned int max_bpc = clamp_t(unsigned int, conn_state->max_bpc, 8, 12);
2136 for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
2137 drm_dbg(dev, "Trying with a %d bpc output\n", bpc);
2139 ret = vc4_hdmi_encoder_compute_format(vc4_hdmi, vc4_state,
2144 vc4_state->output_bpc = bpc;
2147 "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
2148 mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
2149 vc4_state->output_bpc,
2150 vc4_hdmi_output_fmt_str(vc4_state->output_format),
2151 vc4_state->tmds_char_rate);
2159 #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
2160 #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
2162 static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
2163 struct drm_crtc_state *crtc_state,
2164 struct drm_connector_state *conn_state)
2166 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2167 struct drm_connector *connector = &vc4_hdmi->connector;
2168 struct drm_connector_state *old_conn_state =
2169 drm_atomic_get_old_connector_state(conn_state->state, connector);
2170 struct vc4_hdmi_connector_state *old_vc4_state =
2171 conn_state_to_vc4_hdmi_conn_state(old_conn_state);
2172 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
2173 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
2174 unsigned long long tmds_char_rate = mode->clock * 1000;
2175 unsigned long long tmds_bit_rate;
2178 if (vc4_hdmi->variant->unsupported_odd_h_timings) {
2179 if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
2180 /* Only try to fixup DBLCLK modes to get 480i and 576i
2182 * A generic solution for all modes with odd horizontal
2183 * timing values seems impossible based on trying to
2184 * solve it for 1366x768 monitors.
2186 if ((mode->hsync_start - mode->hdisplay) & 1)
2187 mode->hsync_start--;
2188 if ((mode->hsync_end - mode->hsync_start) & 1)
2192 /* Now check whether we still have odd values remaining */
2193 if ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
2194 (mode->hsync_end % 2) || (mode->htotal % 2))
2199 * The 1440p@60 pixel rate is in the same range than the first
2200 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
2201 * bandwidth). Slightly lower the frequency to bring it out of
2204 tmds_bit_rate = tmds_char_rate * 10;
2205 if (vc4_hdmi->disable_wifi_frequencies &&
2206 (tmds_bit_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
2207 tmds_bit_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
2208 mode->clock = 238560;
2209 tmds_char_rate = mode->clock * 1000;
2212 ret = vc4_hdmi_encoder_compute_config(vc4_hdmi, vc4_state, mode);
2216 /* vc4_hdmi_encoder_compute_config may have changed output_bpc and/or output_format */
2217 if (vc4_state->output_bpc != old_vc4_state->output_bpc ||
2218 vc4_state->output_format != old_vc4_state->output_format)
2219 crtc_state->mode_changed = true;
2224 static enum drm_mode_status
2225 vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
2226 const struct drm_display_mode *mode)
2228 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2230 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
2231 !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
2232 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
2233 (mode->hsync_end % 2) || (mode->htotal % 2)))
2234 return MODE_H_ILLEGAL;
2236 return vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode, mode->clock * 1000);
2239 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
2240 .atomic_check = vc4_hdmi_encoder_atomic_check,
2241 .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
2242 .mode_valid = vc4_hdmi_encoder_mode_valid,
2245 static int vc4_hdmi_late_register(struct drm_encoder *encoder)
2247 struct drm_device *drm = encoder->dev;
2248 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2249 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
2251 drm_debugfs_add_file(drm, variant->debugfs_name,
2252 vc4_hdmi_debugfs_regs, vc4_hdmi);
2257 static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
2258 .late_register = vc4_hdmi_late_register,
2261 static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
2264 u32 channel_map = 0;
2266 for (i = 0; i < 8; i++) {
2267 if (channel_mask & BIT(i))
2268 channel_map |= i << (3 * i);
2273 static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
2276 u32 channel_map = 0;
2278 for (i = 0; i < 8; i++) {
2279 if (channel_mask & BIT(i))
2280 channel_map |= i << (4 * i);
2285 static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi)
2287 struct drm_device *drm = vc4_hdmi->connector.dev;
2288 unsigned long flags;
2292 if (!drm_dev_enter(drm, &idx))
2295 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2296 hotplug = HDMI_READ(HDMI_HOTPLUG);
2297 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2301 return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED);
2304 /* HDMI audio codec callbacks */
2305 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
2306 unsigned int samplerate)
2308 struct drm_device *drm = vc4_hdmi->connector.dev;
2310 unsigned long flags;
2314 if (!drm_dev_enter(drm, &idx))
2317 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
2318 rational_best_approximation(hsm_clock, samplerate,
2319 VC4_HD_MAI_SMP_N_MASK >>
2320 VC4_HD_MAI_SMP_N_SHIFT,
2321 (VC4_HD_MAI_SMP_M_MASK >>
2322 VC4_HD_MAI_SMP_M_SHIFT) + 1,
2325 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2326 HDMI_WRITE(HDMI_MAI_SMP,
2327 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
2328 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
2329 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2334 static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
2336 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
2340 lockdep_assert_held(&vc4_hdmi->mutex);
2341 lockdep_assert_held(&vc4_hdmi->hw_lock);
2343 n = 128 * samplerate / 1000;
2344 tmp = (u64)(mode->clock * 1000) * n;
2345 do_div(tmp, 128 * samplerate);
2348 HDMI_WRITE(HDMI_CRP_CFG,
2349 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
2350 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
2353 * We could get slightly more accurate clocks in some cases by
2354 * providing a CTS_1 value. The two CTS values are alternated
2355 * between based on the period fields
2357 HDMI_WRITE(HDMI_CTS_0, cts);
2358 HDMI_WRITE(HDMI_CTS_1, cts);
2361 static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
2363 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
2365 return snd_soc_card_get_drvdata(card);
2368 static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
2370 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
2372 lockdep_assert_held(&vc4_hdmi->mutex);
2375 * If the encoder is currently in DVI mode, treat the codec DAI
2378 if (!display->is_hdmi)
2384 static int vc4_hdmi_audio_startup(struct device *dev, void *data)
2386 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2387 struct drm_device *drm = vc4_hdmi->connector.dev;
2388 unsigned long flags;
2392 mutex_lock(&vc4_hdmi->mutex);
2394 if (!drm_dev_enter(drm, &idx)) {
2399 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2404 vc4_hdmi->audio.streaming = true;
2406 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2407 HDMI_WRITE(HDMI_MAI_CTL,
2408 VC4_HD_MAI_CTL_RESET |
2409 VC4_HD_MAI_CTL_FLUSH |
2410 VC4_HD_MAI_CTL_DLATE |
2411 VC4_HD_MAI_CTL_ERRORE |
2412 VC4_HD_MAI_CTL_ERRORF);
2413 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2415 if (vc4_hdmi->variant->phy_rng_enable)
2416 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
2421 mutex_unlock(&vc4_hdmi->mutex);
2426 static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
2428 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2429 struct device *dev = &vc4_hdmi->pdev->dev;
2430 unsigned long flags;
2433 lockdep_assert_held(&vc4_hdmi->mutex);
2435 vc4_hdmi->audio.streaming = false;
2436 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
2438 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
2440 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2442 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
2443 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
2444 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
2446 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2449 static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
2451 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2452 struct drm_device *drm = vc4_hdmi->connector.dev;
2453 unsigned long flags;
2456 mutex_lock(&vc4_hdmi->mutex);
2458 if (!drm_dev_enter(drm, &idx))
2461 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2463 HDMI_WRITE(HDMI_MAI_CTL,
2464 VC4_HD_MAI_CTL_DLATE |
2465 VC4_HD_MAI_CTL_ERRORE |
2466 VC4_HD_MAI_CTL_ERRORF);
2468 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2470 if (vc4_hdmi->variant->phy_rng_disable)
2471 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
2473 vc4_hdmi->audio.streaming = false;
2474 vc4_hdmi_audio_reset(vc4_hdmi);
2479 mutex_unlock(&vc4_hdmi->mutex);
2482 static int sample_rate_to_mai_fmt(int samplerate)
2484 switch (samplerate) {
2486 return VC4_HDMI_MAI_SAMPLE_RATE_8000;
2488 return VC4_HDMI_MAI_SAMPLE_RATE_11025;
2490 return VC4_HDMI_MAI_SAMPLE_RATE_12000;
2492 return VC4_HDMI_MAI_SAMPLE_RATE_16000;
2494 return VC4_HDMI_MAI_SAMPLE_RATE_22050;
2496 return VC4_HDMI_MAI_SAMPLE_RATE_24000;
2498 return VC4_HDMI_MAI_SAMPLE_RATE_32000;
2500 return VC4_HDMI_MAI_SAMPLE_RATE_44100;
2502 return VC4_HDMI_MAI_SAMPLE_RATE_48000;
2504 return VC4_HDMI_MAI_SAMPLE_RATE_64000;
2506 return VC4_HDMI_MAI_SAMPLE_RATE_88200;
2508 return VC4_HDMI_MAI_SAMPLE_RATE_96000;
2510 return VC4_HDMI_MAI_SAMPLE_RATE_128000;
2512 return VC4_HDMI_MAI_SAMPLE_RATE_176400;
2514 return VC4_HDMI_MAI_SAMPLE_RATE_192000;
2516 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
2520 /* HDMI audio codec callbacks */
2521 static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
2522 struct hdmi_codec_daifmt *daifmt,
2523 struct hdmi_codec_params *params)
2525 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2526 struct drm_device *drm = vc4_hdmi->connector.dev;
2527 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2528 unsigned int sample_rate = params->sample_rate;
2529 unsigned int channels = params->channels;
2530 unsigned long flags;
2531 u32 audio_packet_config, channel_mask;
2533 u32 mai_audio_format;
2534 u32 mai_sample_rate;
2538 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
2539 sample_rate, params->sample_width, channels);
2541 mutex_lock(&vc4_hdmi->mutex);
2543 if (!drm_dev_enter(drm, &idx)) {
2548 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2553 vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
2555 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2556 HDMI_WRITE(HDMI_MAI_CTL,
2557 VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
2558 VC4_HD_MAI_CTL_WHOLSMP |
2559 VC4_HD_MAI_CTL_CHALIGN |
2560 VC4_HD_MAI_CTL_ENABLE);
2562 mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
2563 if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
2564 params->channels == 8)
2565 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
2567 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
2568 HDMI_WRITE(HDMI_MAI_FMT,
2569 VC4_SET_FIELD(mai_sample_rate,
2570 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
2571 VC4_SET_FIELD(mai_audio_format,
2572 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));
2574 /* The B frame identifier should match the value used by alsa-lib (8) */
2575 audio_packet_config =
2576 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
2577 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
2578 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
2580 channel_mask = GENMASK(channels - 1, 0);
2581 audio_packet_config |= VC4_SET_FIELD(channel_mask,
2582 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
2584 /* Set the MAI threshold */
2585 HDMI_WRITE(HDMI_MAI_THR,
2586 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICHIGH) |
2587 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICLOW) |
2588 VC4_SET_FIELD(0x06, VC4_HD_MAI_THR_DREQHIGH) |
2589 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_DREQLOW));
2591 HDMI_WRITE(HDMI_MAI_CONFIG,
2592 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
2593 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
2594 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
2596 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
2597 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
2598 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
2600 vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
2602 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2604 memcpy(&vc4_hdmi->audio.infoframe, ¶ms->cea, sizeof(params->cea));
2605 vc4_hdmi_set_audio_infoframe(encoder);
2610 mutex_unlock(&vc4_hdmi->mutex);
2615 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
2616 .name = "vc4-hdmi-cpu-dai-component",
2617 .legacy_dai_naming = 1,
2620 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
2622 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
2624 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
2629 static const struct snd_soc_dai_ops vc4_snd_dai_ops = {
2630 .probe = vc4_hdmi_audio_cpu_dai_probe,
2633 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
2634 .name = "vc4-hdmi-cpu-dai",
2635 .ops = &vc4_snd_dai_ops,
2637 .stream_name = "Playback",
2640 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
2641 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
2642 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
2643 SNDRV_PCM_RATE_192000,
2644 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
2648 static const struct snd_dmaengine_pcm_config pcm_conf = {
2649 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
2650 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
2653 static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
2654 uint8_t *buf, size_t len)
2656 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2657 struct drm_connector *connector = &vc4_hdmi->connector;
2659 mutex_lock(&vc4_hdmi->mutex);
2660 memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
2661 mutex_unlock(&vc4_hdmi->mutex);
2666 static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
2667 .get_eld = vc4_hdmi_audio_get_eld,
2668 .prepare = vc4_hdmi_audio_prepare,
2669 .audio_shutdown = vc4_hdmi_audio_shutdown,
2670 .audio_startup = vc4_hdmi_audio_startup,
2673 static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
2674 .ops = &vc4_hdmi_codec_ops,
2675 .max_i2s_channels = 8,
2679 static void vc4_hdmi_audio_codec_release(void *ptr)
2681 struct vc4_hdmi *vc4_hdmi = ptr;
2683 platform_device_unregister(vc4_hdmi->audio.codec_pdev);
2684 vc4_hdmi->audio.codec_pdev = NULL;
2687 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
2689 const struct vc4_hdmi_register *mai_data =
2690 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
2691 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
2692 struct snd_soc_card *card = &vc4_hdmi->audio.card;
2693 struct device *dev = &vc4_hdmi->pdev->dev;
2694 struct platform_device *codec_pdev;
2700 * ASoC makes it a bit hard to retrieve a pointer to the
2701 * vc4_hdmi structure. Registering the card will overwrite our
2702 * device drvdata with a pointer to the snd_soc_card structure,
2703 * which can then be used to retrieve whatever drvdata we want
2706 * However, that doesn't fly in the case where we wouldn't
2707 * register an ASoC card (because of an old DT that is missing
2708 * the dmas properties for example), then the card isn't
2709 * registered and the device drvdata wouldn't be set.
2711 * We can deal with both cases by making sure a snd_soc_card
2712 * pointer and a vc4_hdmi structure are pointing to the same
2713 * memory address, so we can treat them indistinctly without any
2716 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
2717 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
2719 if (!of_find_property(dev->of_node, "dmas", &len) || !len) {
2721 "'dmas' DT property is missing or empty, no HDMI audio\n");
2725 if (mai_data->reg != VC4_HD) {
2726 WARN_ONCE(true, "MAI isn't in the HD block\n");
2731 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
2732 * the bus address specified in the DT, because the physical address
2733 * (the one returned by platform_get_resource()) is not appropriate
2734 * for DMA transfers.
2735 * This VC/MMU should probably be exposed to avoid this kind of hacks.
2737 index = of_property_match_string(dev->of_node, "reg-names", "hd");
2738 /* Before BCM2711, we don't have a named register range */
2742 addr = of_get_address(dev->of_node, index, NULL, NULL);
2744 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
2745 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2746 vc4_hdmi->audio.dma_data.maxburst = 2;
2749 * NOTE: Strictly speaking, we should probably use a DRM-managed
2750 * registration there to avoid removing all the audio components
2751 * by the time the driver doesn't have any user anymore.
2753 * However, the ASoC core uses a number of devm_kzalloc calls
2754 * when registering, even when using non-device-managed
2755 * functions (such as in snd_soc_register_component()).
2757 * If we call snd_soc_unregister_component() in a DRM-managed
2758 * action, the device-managed actions have already been executed
2759 * and thus we would access memory that has been freed.
2761 * Using device-managed hooks here probably leaves us open to a
2762 * bunch of issues if userspace still has a handle on the ALSA
2763 * device when the device is removed. However, this is mitigated
2764 * by the use of drm_dev_enter()/drm_dev_exit() in the audio
2765 * path to prevent the access to the device resources if it
2766 * isn't there anymore.
2768 * Then, the vc4_hdmi structure is DRM-managed and thus only
2769 * freed whenever the last user has closed the DRM device file.
2770 * It should thus outlive ALSA in most situations.
2772 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
2774 dev_err(dev, "Could not register PCM component: %d\n", ret);
2778 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
2779 &vc4_hdmi_audio_cpu_dai_drv, 1);
2781 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
2785 codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
2786 PLATFORM_DEVID_AUTO,
2787 &vc4_hdmi_codec_pdata,
2788 sizeof(vc4_hdmi_codec_pdata));
2789 if (IS_ERR(codec_pdev)) {
2790 dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
2791 return PTR_ERR(codec_pdev);
2793 vc4_hdmi->audio.codec_pdev = codec_pdev;
2795 ret = devm_add_action_or_reset(dev, vc4_hdmi_audio_codec_release, vc4_hdmi);
2799 dai_link->cpus = &vc4_hdmi->audio.cpu;
2800 dai_link->codecs = &vc4_hdmi->audio.codec;
2801 dai_link->platforms = &vc4_hdmi->audio.platform;
2803 dai_link->num_cpus = 1;
2804 dai_link->num_codecs = 1;
2805 dai_link->num_platforms = 1;
2807 dai_link->name = "MAI";
2808 dai_link->stream_name = "MAI PCM";
2809 dai_link->codecs->dai_name = "i2s-hifi";
2810 dai_link->cpus->dai_name = dev_name(dev);
2811 dai_link->codecs->name = dev_name(&codec_pdev->dev);
2812 dai_link->platforms->name = dev_name(dev);
2814 card->dai_link = dai_link;
2815 card->num_links = 1;
2816 card->name = vc4_hdmi->variant->card_name;
2817 card->driver_name = "vc4-hdmi";
2819 card->owner = THIS_MODULE;
2822 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
2823 * stores a pointer to the snd card object in dev->driver_data. This
2824 * means we cannot use it for something else. The hdmi back-pointer is
2825 * now stored in card->drvdata and should be retrieved with
2826 * snd_soc_card_get_drvdata() if needed.
2828 snd_soc_card_set_drvdata(card, vc4_hdmi);
2829 ret = devm_snd_soc_register_card(dev, card);
2831 dev_err_probe(dev, ret, "Could not register sound card\n");
2837 static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
2839 struct vc4_hdmi *vc4_hdmi = priv;
2840 struct drm_connector *connector = &vc4_hdmi->connector;
2841 struct drm_device *dev = connector->dev;
2843 if (dev && dev->registered)
2844 drm_connector_helper_hpd_irq_event(connector);
2849 static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
2851 struct drm_connector *connector = &vc4_hdmi->connector;
2852 struct platform_device *pdev = vc4_hdmi->pdev;
2855 if (vc4_hdmi->variant->external_irq_controller) {
2856 unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
2857 unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
2859 ret = devm_request_threaded_irq(&pdev->dev, hpd_con,
2861 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2862 "vc4 hdmi hpd connected", vc4_hdmi);
2866 ret = devm_request_threaded_irq(&pdev->dev, hpd_rm,
2868 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2869 "vc4 hdmi hpd disconnected", vc4_hdmi);
2873 connector->polled = DRM_CONNECTOR_POLL_HPD;
2879 #ifdef CONFIG_DRM_VC4_HDMI_CEC
2880 static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
2882 struct vc4_hdmi *vc4_hdmi = priv;
2884 if (vc4_hdmi->cec_rx_msg.len)
2885 cec_received_msg(vc4_hdmi->cec_adap,
2886 &vc4_hdmi->cec_rx_msg);
2891 static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
2893 struct vc4_hdmi *vc4_hdmi = priv;
2895 if (vc4_hdmi->cec_tx_ok) {
2896 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
2900 * This CEC implementation makes 1 retry, so if we
2901 * get a NACK, then that means it made 2 attempts.
2903 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
2909 static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
2911 struct vc4_hdmi *vc4_hdmi = priv;
2914 if (vc4_hdmi->cec_irq_was_rx)
2915 ret = vc4_cec_irq_handler_rx_thread(irq, priv);
2917 ret = vc4_cec_irq_handler_tx_thread(irq, priv);
2922 static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
2924 struct drm_device *dev = vc4_hdmi->connector.dev;
2925 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
2928 lockdep_assert_held(&vc4_hdmi->hw_lock);
2930 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
2931 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
2933 if (msg->len > 16) {
2934 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
2938 for (i = 0; i < msg->len; i += 4) {
2939 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
2941 msg->msg[i] = val & 0xff;
2942 msg->msg[i + 1] = (val >> 8) & 0xff;
2943 msg->msg[i + 2] = (val >> 16) & 0xff;
2944 msg->msg[i + 3] = (val >> 24) & 0xff;
2948 static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2953 * We don't need to protect the register access using
2954 * drm_dev_enter() there because the interrupt handler lifetime
2955 * is tied to the device itself, and not to the DRM device.
2957 * So when the device will be gone, one of the first thing we
2958 * will be doing will be to unregister the interrupt handler,
2959 * and then unregister the DRM device. drm_dev_enter() would
2960 * thus always succeed if we are here.
2963 lockdep_assert_held(&vc4_hdmi->hw_lock);
2965 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
2966 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
2967 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
2968 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
2970 return IRQ_WAKE_THREAD;
2973 static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
2975 struct vc4_hdmi *vc4_hdmi = priv;
2978 spin_lock(&vc4_hdmi->hw_lock);
2979 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
2980 spin_unlock(&vc4_hdmi->hw_lock);
2985 static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2989 lockdep_assert_held(&vc4_hdmi->hw_lock);
2992 * We don't need to protect the register access using
2993 * drm_dev_enter() there because the interrupt handler lifetime
2994 * is tied to the device itself, and not to the DRM device.
2996 * So when the device will be gone, one of the first thing we
2997 * will be doing will be to unregister the interrupt handler,
2998 * and then unregister the DRM device. drm_dev_enter() would
2999 * thus always succeed if we are here.
3002 vc4_hdmi->cec_rx_msg.len = 0;
3003 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
3004 vc4_cec_read_msg(vc4_hdmi, cntrl1);
3005 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
3006 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
3007 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
3009 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
3011 return IRQ_WAKE_THREAD;
3014 static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
3016 struct vc4_hdmi *vc4_hdmi = priv;
3019 spin_lock(&vc4_hdmi->hw_lock);
3020 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
3021 spin_unlock(&vc4_hdmi->hw_lock);
3026 static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
3028 struct vc4_hdmi *vc4_hdmi = priv;
3029 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
3034 * We don't need to protect the register access using
3035 * drm_dev_enter() there because the interrupt handler lifetime
3036 * is tied to the device itself, and not to the DRM device.
3038 * So when the device will be gone, one of the first thing we
3039 * will be doing will be to unregister the interrupt handler,
3040 * and then unregister the DRM device. drm_dev_enter() would
3041 * thus always succeed if we are here.
3044 if (!(stat & VC4_HDMI_CPU_CEC))
3047 spin_lock(&vc4_hdmi->hw_lock);
3048 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
3049 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
3050 if (vc4_hdmi->cec_irq_was_rx)
3051 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
3053 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
3055 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
3056 spin_unlock(&vc4_hdmi->hw_lock);
3061 static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
3063 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3064 struct drm_device *drm = vc4_hdmi->connector.dev;
3065 /* clock period in microseconds */
3066 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
3067 unsigned long flags;
3072 if (!drm_dev_enter(drm, &idx))
3074 * We can't return an error code, because the CEC
3075 * framework will emit WARN_ON messages at unbind
3080 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
3086 mutex_lock(&vc4_hdmi->mutex);
3088 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3090 val = HDMI_READ(HDMI_CEC_CNTRL_5);
3091 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
3092 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
3093 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
3094 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
3095 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
3097 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
3098 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
3099 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
3100 HDMI_WRITE(HDMI_CEC_CNTRL_2,
3101 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
3102 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
3103 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
3104 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
3105 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
3106 HDMI_WRITE(HDMI_CEC_CNTRL_3,
3107 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
3108 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
3109 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
3110 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
3111 HDMI_WRITE(HDMI_CEC_CNTRL_4,
3112 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
3113 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
3114 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
3115 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
3117 if (!vc4_hdmi->variant->external_irq_controller)
3118 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
3120 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3122 mutex_unlock(&vc4_hdmi->mutex);
3128 static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
3130 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3131 struct drm_device *drm = vc4_hdmi->connector.dev;
3132 unsigned long flags;
3135 if (!drm_dev_enter(drm, &idx))
3137 * We can't return an error code, because the CEC
3138 * framework will emit WARN_ON messages at unbind
3143 mutex_lock(&vc4_hdmi->mutex);
3145 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3147 if (!vc4_hdmi->variant->external_irq_controller)
3148 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
3150 HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
3151 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
3153 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3155 mutex_unlock(&vc4_hdmi->mutex);
3157 pm_runtime_put(&vc4_hdmi->pdev->dev);
3164 static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
3167 return vc4_hdmi_cec_enable(adap);
3169 return vc4_hdmi_cec_disable(adap);
3172 static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
3174 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3175 struct drm_device *drm = vc4_hdmi->connector.dev;
3176 unsigned long flags;
3179 if (!drm_dev_enter(drm, &idx))
3181 * We can't return an error code, because the CEC
3182 * framework will emit WARN_ON messages at unbind
3187 mutex_lock(&vc4_hdmi->mutex);
3188 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3189 HDMI_WRITE(HDMI_CEC_CNTRL_1,
3190 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
3191 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
3192 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3193 mutex_unlock(&vc4_hdmi->mutex);
3200 static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
3201 u32 signal_free_time, struct cec_msg *msg)
3203 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3204 struct drm_device *dev = vc4_hdmi->connector.dev;
3205 unsigned long flags;
3210 if (!drm_dev_enter(dev, &idx))
3213 if (msg->len > 16) {
3214 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
3219 mutex_lock(&vc4_hdmi->mutex);
3221 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3223 for (i = 0; i < msg->len; i += 4)
3224 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
3226 (msg->msg[i + 1] << 8) |
3227 (msg->msg[i + 2] << 16) |
3228 (msg->msg[i + 3] << 24));
3230 val = HDMI_READ(HDMI_CEC_CNTRL_1);
3231 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
3232 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
3233 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
3234 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
3235 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
3237 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
3239 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3240 mutex_unlock(&vc4_hdmi->mutex);
3246 static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
3247 .adap_enable = vc4_hdmi_cec_adap_enable,
3248 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
3249 .adap_transmit = vc4_hdmi_cec_adap_transmit,
3252 static void vc4_hdmi_cec_release(void *ptr)
3254 struct vc4_hdmi *vc4_hdmi = ptr;
3256 cec_unregister_adapter(vc4_hdmi->cec_adap);
3257 vc4_hdmi->cec_adap = NULL;
3260 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
3262 struct cec_connector_info conn_info;
3263 struct platform_device *pdev = vc4_hdmi->pdev;
3264 struct device *dev = &pdev->dev;
3267 if (!of_property_present(dev->of_node, "interrupts")) {
3268 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
3272 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
3274 vc4_hdmi->variant->card_name,
3276 CEC_CAP_CONNECTOR_INFO, 1);
3277 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
3281 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
3282 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
3284 if (vc4_hdmi->variant->external_irq_controller) {
3285 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-rx"),
3286 vc4_cec_irq_handler_rx_bare,
3287 vc4_cec_irq_handler_rx_thread, 0,
3288 "vc4 hdmi cec rx", vc4_hdmi);
3290 goto err_delete_cec_adap;
3292 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-tx"),
3293 vc4_cec_irq_handler_tx_bare,
3294 vc4_cec_irq_handler_tx_thread, 0,
3295 "vc4 hdmi cec tx", vc4_hdmi);
3297 goto err_delete_cec_adap;
3299 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
3300 vc4_cec_irq_handler,
3301 vc4_cec_irq_handler_thread, 0,
3302 "vc4 hdmi cec", vc4_hdmi);
3304 goto err_delete_cec_adap;
3307 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
3309 goto err_delete_cec_adap;
3312 * NOTE: Strictly speaking, we should probably use a DRM-managed
3313 * registration there to avoid removing the CEC adapter by the
3314 * time the DRM driver doesn't have any user anymore.
3316 * However, the CEC framework already cleans up the CEC adapter
3317 * only when the last user has closed its file descriptor, so we
3318 * don't need to handle it in DRM.
3320 * By the time the device-managed hook is executed, we will give
3321 * up our reference to the CEC adapter and therefore don't
3322 * really care when it's actually freed.
3324 * There's still a problematic sequence: if we unregister our
3325 * CEC adapter, but the userspace keeps a handle on the CEC
3326 * adapter but not the DRM device for some reason. In such a
3327 * case, our vc4_hdmi structure will be freed, but the
3328 * cec_adapter structure will have a dangling pointer to what
3329 * used to be our HDMI controller. If we get a CEC call at that
3330 * moment, we could end up with a use-after-free. Fortunately,
3331 * the CEC framework already handles this too, by calling
3332 * cec_is_registered() in cec_ioctl() and cec_poll().
3334 ret = devm_add_action_or_reset(dev, vc4_hdmi_cec_release, vc4_hdmi);
3340 err_delete_cec_adap:
3341 cec_delete_adapter(vc4_hdmi->cec_adap);
3346 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
3352 static void vc4_hdmi_free_regset(struct drm_device *drm, void *ptr)
3354 struct debugfs_reg32 *regs = ptr;
3359 static int vc4_hdmi_build_regset(struct drm_device *drm,
3360 struct vc4_hdmi *vc4_hdmi,
3361 struct debugfs_regset32 *regset,
3362 enum vc4_hdmi_regs reg)
3364 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
3365 struct debugfs_reg32 *regs, *new_regs;
3366 unsigned int count = 0;
3370 regs = kcalloc(variant->num_registers, sizeof(*regs),
3375 for (i = 0; i < variant->num_registers; i++) {
3376 const struct vc4_hdmi_register *field = &variant->registers[i];
3378 if (field->reg != reg)
3381 regs[count].name = field->name;
3382 regs[count].offset = field->offset;
3386 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
3390 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
3391 regset->regs = new_regs;
3392 regset->nregs = count;
3394 ret = drmm_add_action_or_reset(drm, vc4_hdmi_free_regset, new_regs);
3401 static int vc4_hdmi_init_resources(struct drm_device *drm,
3402 struct vc4_hdmi *vc4_hdmi)
3404 struct platform_device *pdev = vc4_hdmi->pdev;
3405 struct device *dev = &pdev->dev;
3408 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
3409 if (IS_ERR(vc4_hdmi->hdmicore_regs))
3410 return PTR_ERR(vc4_hdmi->hdmicore_regs);
3412 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
3413 if (IS_ERR(vc4_hdmi->hd_regs))
3414 return PTR_ERR(vc4_hdmi->hd_regs);
3416 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3420 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3424 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
3425 if (IS_ERR(vc4_hdmi->pixel_clock)) {
3426 ret = PTR_ERR(vc4_hdmi->pixel_clock);
3427 if (ret != -EPROBE_DEFER)
3428 DRM_ERROR("Failed to get pixel clock\n");
3432 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3433 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3434 DRM_ERROR("Failed to get HDMI state machine clock\n");
3435 return PTR_ERR(vc4_hdmi->hsm_clock);
3437 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
3438 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
3443 static int vc5_hdmi_init_resources(struct drm_device *drm,
3444 struct vc4_hdmi *vc4_hdmi)
3446 struct platform_device *pdev = vc4_hdmi->pdev;
3447 struct device *dev = &pdev->dev;
3448 struct resource *res;
3451 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
3455 vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
3456 resource_size(res));
3457 if (!vc4_hdmi->hdmicore_regs)
3460 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
3464 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
3465 if (!vc4_hdmi->hd_regs)
3468 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
3472 vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
3473 if (!vc4_hdmi->cec_regs)
3476 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
3480 vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
3481 if (!vc4_hdmi->csc_regs)
3484 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
3488 vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
3489 if (!vc4_hdmi->dvp_regs)
3492 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
3496 vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
3497 if (!vc4_hdmi->phy_regs)
3500 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
3504 vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
3505 if (!vc4_hdmi->ram_regs)
3508 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
3512 vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
3513 if (!vc4_hdmi->rm_regs)
3516 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3517 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3518 DRM_ERROR("Failed to get HDMI state machine clock\n");
3519 return PTR_ERR(vc4_hdmi->hsm_clock);
3522 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
3523 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
3524 DRM_ERROR("Failed to get pixel bvb clock\n");
3525 return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
3528 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
3529 if (IS_ERR(vc4_hdmi->audio_clock)) {
3530 DRM_ERROR("Failed to get audio clock\n");
3531 return PTR_ERR(vc4_hdmi->audio_clock);
3534 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
3535 if (IS_ERR(vc4_hdmi->cec_clock)) {
3536 DRM_ERROR("Failed to get CEC clock\n");
3537 return PTR_ERR(vc4_hdmi->cec_clock);
3540 vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
3541 if (IS_ERR(vc4_hdmi->reset)) {
3542 DRM_ERROR("Failed to get HDMI reset line\n");
3543 return PTR_ERR(vc4_hdmi->reset);
3546 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3550 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3554 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->cec_regset, VC5_CEC);
3558 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->csc_regset, VC5_CSC);
3562 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->dvp_regset, VC5_DVP);
3566 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->phy_regset, VC5_PHY);
3570 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->ram_regset, VC5_RAM);
3574 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->rm_regset, VC5_RM);
3581 static int vc4_hdmi_runtime_suspend(struct device *dev)
3583 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3585 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3590 static int vc4_hdmi_runtime_resume(struct device *dev)
3592 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3593 unsigned long __maybe_unused flags;
3594 u32 __maybe_unused value;
3598 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
3603 * Whenever the RaspberryPi boots without an HDMI monitor
3604 * plugged in, the firmware won't have initialized the HSM clock
3605 * rate and it will be reported as 0.
3607 * If we try to access a register of the controller in such a
3608 * case, it will lead to a silent CPU stall. Let's make sure we
3609 * prevent such a case.
3611 rate = clk_get_rate(vc4_hdmi->hsm_clock);
3614 goto err_disable_clk;
3617 if (vc4_hdmi->variant->reset)
3618 vc4_hdmi->variant->reset(vc4_hdmi);
3620 #ifdef CONFIG_DRM_VC4_HDMI_CEC
3621 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3622 value = HDMI_READ(HDMI_CEC_CNTRL_1);
3623 /* Set the logical address to Unregistered */
3624 value |= VC4_HDMI_CEC_ADDR_MASK;
3625 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
3626 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3628 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
3630 if (!vc4_hdmi->variant->external_irq_controller) {
3631 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3632 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
3633 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3640 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3644 static void vc4_hdmi_put_ddc_device(void *ptr)
3646 struct vc4_hdmi *vc4_hdmi = ptr;
3648 put_device(&vc4_hdmi->ddc->dev);
3651 static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
3653 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
3654 struct platform_device *pdev = to_platform_device(dev);
3655 struct drm_device *drm = dev_get_drvdata(master);
3656 struct vc4_hdmi *vc4_hdmi;
3657 struct drm_encoder *encoder;
3658 struct device_node *ddc_node;
3661 vc4_hdmi = drmm_kzalloc(drm, sizeof(*vc4_hdmi), GFP_KERNEL);
3665 ret = drmm_mutex_init(drm, &vc4_hdmi->mutex);
3669 spin_lock_init(&vc4_hdmi->hw_lock);
3670 INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
3672 dev_set_drvdata(dev, vc4_hdmi);
3673 encoder = &vc4_hdmi->encoder.base;
3674 vc4_hdmi->encoder.type = variant->encoder_type;
3675 vc4_hdmi->encoder.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
3676 vc4_hdmi->encoder.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
3677 vc4_hdmi->encoder.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
3678 vc4_hdmi->encoder.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
3679 vc4_hdmi->encoder.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
3680 vc4_hdmi->pdev = pdev;
3681 vc4_hdmi->variant = variant;
3684 * Since we don't know the state of the controller and its
3685 * display (if any), let's assume it's always enabled.
3686 * vc4_hdmi_disable_scrambling() will thus run at boot, make
3687 * sure it's disabled, and avoid any inconsistency.
3689 if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK)
3690 vc4_hdmi->scdc_enabled = true;
3692 ret = variant->init_resources(drm, vc4_hdmi);
3696 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
3698 DRM_ERROR("Failed to find ddc node in device tree\n");
3702 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
3703 of_node_put(ddc_node);
3704 if (!vc4_hdmi->ddc) {
3705 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
3706 return -EPROBE_DEFER;
3709 ret = devm_add_action_or_reset(dev, vc4_hdmi_put_ddc_device, vc4_hdmi);
3713 /* Only use the GPIO HPD pin if present in the DT, otherwise
3714 * we'll use the HDMI core's register.
3716 vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
3717 if (IS_ERR(vc4_hdmi->hpd_gpio)) {
3718 return PTR_ERR(vc4_hdmi->hpd_gpio);
3721 vc4_hdmi->disable_wifi_frequencies =
3722 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
3724 ret = devm_pm_runtime_enable(dev);
3729 * We need to have the device powered up at this point to call
3730 * our reset hook and for the CEC init.
3732 ret = pm_runtime_resume_and_get(dev);
3736 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
3737 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
3738 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
3739 clk_prepare_enable(vc4_hdmi->pixel_clock);
3740 clk_prepare_enable(vc4_hdmi->hsm_clock);
3741 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
3744 ret = drmm_encoder_init(drm, encoder,
3745 &vc4_hdmi_encoder_funcs,
3746 DRM_MODE_ENCODER_TMDS,
3749 goto err_put_runtime_pm;
3751 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
3753 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
3755 goto err_put_runtime_pm;
3757 ret = vc4_hdmi_hotplug_init(vc4_hdmi);
3759 goto err_put_runtime_pm;
3761 ret = vc4_hdmi_cec_init(vc4_hdmi);
3763 goto err_put_runtime_pm;
3765 ret = vc4_hdmi_audio_init(vc4_hdmi);
3767 goto err_put_runtime_pm;
3769 pm_runtime_put_sync(dev);
3774 pm_runtime_put_sync(dev);
3779 static const struct component_ops vc4_hdmi_ops = {
3780 .bind = vc4_hdmi_bind,
3783 static int vc4_hdmi_dev_probe(struct platform_device *pdev)
3785 return component_add(&pdev->dev, &vc4_hdmi_ops);
3788 static void vc4_hdmi_dev_remove(struct platform_device *pdev)
3790 component_del(&pdev->dev, &vc4_hdmi_ops);
3793 static const struct vc4_hdmi_variant bcm2835_variant = {
3794 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3795 .debugfs_name = "hdmi_regs",
3796 .card_name = "vc4-hdmi",
3797 .max_pixel_clock = 162000000,
3798 .registers = vc4_hdmi_fields,
3799 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
3801 .init_resources = vc4_hdmi_init_resources,
3802 .csc_setup = vc4_hdmi_csc_setup,
3803 .reset = vc4_hdmi_reset,
3804 .set_timings = vc4_hdmi_set_timings,
3805 .phy_init = vc4_hdmi_phy_init,
3806 .phy_disable = vc4_hdmi_phy_disable,
3807 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
3808 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
3809 .channel_map = vc4_hdmi_channel_map,
3810 .supports_hdr = false,
3813 static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
3814 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3815 .debugfs_name = "hdmi0_regs",
3816 .card_name = "vc4-hdmi-0",
3817 .max_pixel_clock = 600000000,
3818 .registers = vc5_hdmi_hdmi0_fields,
3819 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
3820 .phy_lane_mapping = {
3826 .unsupported_odd_h_timings = true,
3827 .external_irq_controller = true,
3829 .init_resources = vc5_hdmi_init_resources,
3830 .csc_setup = vc5_hdmi_csc_setup,
3831 .reset = vc5_hdmi_reset,
3832 .set_timings = vc5_hdmi_set_timings,
3833 .phy_init = vc5_hdmi_phy_init,
3834 .phy_disable = vc5_hdmi_phy_disable,
3835 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3836 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3837 .channel_map = vc5_hdmi_channel_map,
3838 .supports_hdr = true,
3839 .hp_detect = vc5_hdmi_hp_detect,
3842 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
3843 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
3844 .debugfs_name = "hdmi1_regs",
3845 .card_name = "vc4-hdmi-1",
3846 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
3847 .registers = vc5_hdmi_hdmi1_fields,
3848 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
3849 .phy_lane_mapping = {
3855 .unsupported_odd_h_timings = true,
3856 .external_irq_controller = true,
3858 .init_resources = vc5_hdmi_init_resources,
3859 .csc_setup = vc5_hdmi_csc_setup,
3860 .reset = vc5_hdmi_reset,
3861 .set_timings = vc5_hdmi_set_timings,
3862 .phy_init = vc5_hdmi_phy_init,
3863 .phy_disable = vc5_hdmi_phy_disable,
3864 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3865 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3866 .channel_map = vc5_hdmi_channel_map,
3867 .supports_hdr = true,
3868 .hp_detect = vc5_hdmi_hp_detect,
3871 static const struct of_device_id vc4_hdmi_dt_match[] = {
3872 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
3873 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
3874 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
3878 static const struct dev_pm_ops vc4_hdmi_pm_ops = {
3879 SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
3880 vc4_hdmi_runtime_resume,
3884 struct platform_driver vc4_hdmi_driver = {
3885 .probe = vc4_hdmi_dev_probe,
3886 .remove_new = vc4_hdmi_dev_remove,
3889 .of_match_table = vc4_hdmi_dt_match,
3890 .pm = &vc4_hdmi_pm_ops,