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
6 * Author: Rob Clark <robdclark@gmail.com>
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_probe_helper.h>
39 #include <drm/drm_simple_kms_helper.h>
40 #include <linux/clk.h>
41 #include <linux/component.h>
42 #include <linux/gpio/consumer.h>
43 #include <linux/i2c.h>
44 #include <linux/module.h>
45 #include <linux/moduleparam.h>
47 #include <linux/of_address.h>
48 #include <linux/pm_runtime.h>
49 #include <linux/rational.h>
50 #include <linux/reset.h>
51 #include <sound/dmaengine_pcm.h>
52 #include <sound/hdmi-codec.h>
53 #include <sound/pcm_drm_eld.h>
54 #include <sound/pcm_params.h>
55 #include <sound/soc.h>
56 #include "media/cec.h"
59 #include "vc4_hdmi_regs.h"
62 #define VC5_HDMI_HORZA_HFP_SHIFT 16
63 #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
64 #define VC5_HDMI_HORZA_VPOS BIT(15)
65 #define VC5_HDMI_HORZA_HPOS BIT(14)
66 #define VC5_HDMI_HORZA_HAP_SHIFT 0
67 #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
69 #define VC5_HDMI_HORZB_HBP_SHIFT 16
70 #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
71 #define VC5_HDMI_HORZB_HSP_SHIFT 0
72 #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
74 #define VC5_HDMI_VERTA_VSP_SHIFT 24
75 #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
76 #define VC5_HDMI_VERTA_VFP_SHIFT 16
77 #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
78 #define VC5_HDMI_VERTA_VAL_SHIFT 0
79 #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
81 #define VC5_HDMI_VERTB_VSPO_SHIFT 16
82 #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
84 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
85 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
86 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
87 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
89 #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
91 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
92 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
94 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
95 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
97 #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
99 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
100 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
102 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK VC4_MASK(7, 0)
103 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_SET_AVMUTE BIT(0)
104 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE BIT(4)
106 # define VC4_HD_M_SW_RST BIT(2)
107 # define VC4_HD_M_ENABLE BIT(0)
109 #define HSM_MIN_CLOCK_FREQ 120000000
110 #define CEC_CLOCK_FREQ 40000
112 #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
114 /* bit field to force hotplug detection. bit0 = HDMI0 */
115 static int force_hotplug = 0;
116 module_param(force_hotplug, int, 0644);
118 static const char * const output_format_str[] = {
119 [VC4_HDMI_OUTPUT_RGB] = "RGB",
120 [VC4_HDMI_OUTPUT_YUV420] = "YUV 4:2:0",
121 [VC4_HDMI_OUTPUT_YUV422] = "YUV 4:2:2",
122 [VC4_HDMI_OUTPUT_YUV444] = "YUV 4:4:4",
125 static const char *vc4_hdmi_output_fmt_str(enum vc4_hdmi_output_format fmt)
127 if (fmt >= ARRAY_SIZE(output_format_str))
130 return output_format_str[fmt];
133 static unsigned long long
134 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
135 unsigned int bpc, enum vc4_hdmi_output_format fmt);
137 static bool vc4_hdmi_supports_scrambling(struct vc4_hdmi *vc4_hdmi)
139 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
141 lockdep_assert_held(&vc4_hdmi->mutex);
143 if (!display->is_hdmi)
146 if (!display->hdmi.scdc.supported ||
147 !display->hdmi.scdc.scrambling.supported)
153 static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
155 enum vc4_hdmi_output_format fmt)
157 unsigned long long clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
159 return clock > HDMI_14_MAX_TMDS_CLK;
162 static bool vc4_hdmi_is_full_range(struct vc4_hdmi *vc4_hdmi,
163 struct vc4_hdmi_connector_state *vc4_state)
165 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
166 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
168 if (vc4_state->broadcast_rgb == VC4_HDMI_BROADCAST_RGB_LIMITED)
170 else if (vc4_state->broadcast_rgb == VC4_HDMI_BROADCAST_RGB_FULL)
173 return !display->is_hdmi ||
174 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
177 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
179 struct drm_debugfs_entry *entry = m->private;
180 struct vc4_hdmi *vc4_hdmi = entry->file.data;
181 struct drm_device *drm = vc4_hdmi->connector.dev;
182 struct drm_printer p = drm_seq_file_printer(m);
185 if (!drm_dev_enter(drm, &idx))
188 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
190 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
191 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
192 drm_print_regset32(&p, &vc4_hdmi->cec_regset);
193 drm_print_regset32(&p, &vc4_hdmi->csc_regset);
194 drm_print_regset32(&p, &vc4_hdmi->dvp_regset);
195 drm_print_regset32(&p, &vc4_hdmi->phy_regset);
196 drm_print_regset32(&p, &vc4_hdmi->ram_regset);
197 drm_print_regset32(&p, &vc4_hdmi->rm_regset);
199 pm_runtime_put(&vc4_hdmi->pdev->dev);
206 static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
208 struct drm_device *drm = vc4_hdmi->connector.dev;
213 * We can be called by our bind callback, when the
214 * connector->dev pointer might not be initialised yet.
216 if (drm && !drm_dev_enter(drm, &idx))
219 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
221 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
223 HDMI_WRITE(HDMI_M_CTL, 0);
225 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
227 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
228 VC4_HDMI_SW_RESET_HDMI |
229 VC4_HDMI_SW_RESET_FORMAT_DETECT);
231 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
233 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
239 static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
241 struct drm_device *drm = vc4_hdmi->connector.dev;
246 * We can be called by our bind callback, when the
247 * connector->dev pointer might not be initialised yet.
249 if (drm && !drm_dev_enter(drm, &idx))
252 reset_control_reset(vc4_hdmi->reset);
254 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
256 HDMI_WRITE(HDMI_DVP_CTL, 0);
258 HDMI_WRITE(HDMI_CLOCK_STOP,
259 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
261 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
267 #ifdef CONFIG_DRM_VC4_HDMI_CEC
268 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
270 struct drm_device *drm = vc4_hdmi->connector.dev;
271 unsigned long cec_rate;
278 * This function is called by our runtime_resume implementation
279 * and thus at bind time, when we haven't registered our
280 * connector yet and thus don't have a pointer to the DRM
283 if (drm && !drm_dev_enter(drm, &idx))
286 cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
288 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
290 value = HDMI_READ(HDMI_CEC_CNTRL_1);
291 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
294 * Set the clock divider: the hsm_clock rate and this divider
295 * setting will give a 40 kHz CEC clock.
297 clk_cnt = cec_rate / CEC_CLOCK_FREQ;
298 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
299 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
301 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
307 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
310 static int reset_pipe(struct drm_crtc *crtc,
311 struct drm_modeset_acquire_ctx *ctx)
313 struct drm_atomic_state *state;
314 struct drm_crtc_state *crtc_state;
317 state = drm_atomic_state_alloc(crtc->dev);
321 state->acquire_ctx = ctx;
323 crtc_state = drm_atomic_get_crtc_state(state, crtc);
324 if (IS_ERR(crtc_state)) {
325 ret = PTR_ERR(crtc_state);
329 crtc_state->connectors_changed = true;
331 ret = drm_atomic_commit(state);
333 drm_atomic_state_put(state);
338 static int vc4_hdmi_reset_link(struct drm_connector *connector,
339 struct drm_modeset_acquire_ctx *ctx)
341 struct drm_device *drm;
342 struct vc4_hdmi *vc4_hdmi;
343 struct drm_connector_state *conn_state;
344 struct drm_crtc_state *crtc_state;
345 struct drm_crtc *crtc;
346 bool scrambling_needed;
353 drm = connector->dev;
354 ret = drm_modeset_lock(&drm->mode_config.connection_mutex, ctx);
358 conn_state = connector->state;
359 crtc = conn_state->crtc;
363 ret = drm_modeset_lock(&crtc->mutex, ctx);
367 crtc_state = crtc->state;
368 if (!crtc_state->active)
371 vc4_hdmi = connector_to_vc4_hdmi(connector);
372 mutex_lock(&vc4_hdmi->mutex);
374 if (!vc4_hdmi_supports_scrambling(vc4_hdmi)) {
375 mutex_unlock(&vc4_hdmi->mutex);
379 scrambling_needed = vc4_hdmi_mode_needs_scrambling(&vc4_hdmi->saved_adjusted_mode,
380 vc4_hdmi->output_bpc,
381 vc4_hdmi->output_format);
382 if (!scrambling_needed) {
383 mutex_unlock(&vc4_hdmi->mutex);
387 if (conn_state->commit &&
388 !try_wait_for_completion(&conn_state->commit->hw_done)) {
389 mutex_unlock(&vc4_hdmi->mutex);
393 ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
395 drm_err(drm, "Failed to read TMDS config: %d\n", ret);
396 mutex_unlock(&vc4_hdmi->mutex);
400 if (!!(config & SCDC_SCRAMBLING_ENABLE) == scrambling_needed) {
401 mutex_unlock(&vc4_hdmi->mutex);
405 mutex_unlock(&vc4_hdmi->mutex);
408 * HDMI 2.0 says that one should not send scrambled data
409 * prior to configuring the sink scrambling, and that
410 * TMDS clock/data transmission should be suspended when
411 * changing the TMDS clock rate in the sink. So let's
412 * just do a full modeset here, even though some sinks
413 * would be perfectly happy if were to just reconfigure
414 * the SCDC settings on the fly.
416 return reset_pipe(crtc, ctx);
419 static void vc4_hdmi_handle_hotplug(struct vc4_hdmi *vc4_hdmi,
420 struct drm_modeset_acquire_ctx *ctx,
421 enum drm_connector_status status)
423 struct drm_connector *connector = &vc4_hdmi->connector;
428 * NOTE: This function should really be called with
429 * vc4_hdmi->mutex held, but doing so results in reentrancy
430 * issues since cec_s_phys_addr_from_edid might call
431 * .adap_enable, which leads to that funtion being called with
434 * A similar situation occurs with vc4_hdmi_reset_link() that
435 * will call into our KMS hooks if the scrambling was enabled.
437 * Concurrency isn't an issue at the moment since we don't share
438 * any state with any of the other frameworks so we can ignore
442 if (status == connector_status_disconnected) {
443 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
447 edid = drm_get_edid(connector, vc4_hdmi->ddc);
451 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
455 ret = vc4_hdmi_reset_link(connector, ctx);
456 if (ret == -EDEADLK) {
457 drm_modeset_backoff(ctx);
465 static int vc4_hdmi_connector_detect_ctx(struct drm_connector *connector,
466 struct drm_modeset_acquire_ctx *ctx,
469 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
470 enum drm_connector_status status = connector_status_disconnected;
473 * NOTE: This function should really take vc4_hdmi->mutex, but
474 * doing so results in reentrancy issues since
475 * vc4_hdmi_handle_hotplug() can call into other functions that
476 * would take the mutex while it's held here.
478 * Concurrency isn't an issue at the moment since we don't share
479 * any state with any of the other frameworks so we can ignore
483 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
485 if (force_hotplug & BIT(vc4_hdmi->encoder.type - VC4_ENCODER_TYPE_HDMI0))
486 status = connector_status_connected;
487 else if (vc4_hdmi->hpd_gpio) {
488 if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
489 status = connector_status_connected;
491 if (vc4_hdmi->variant->hp_detect &&
492 vc4_hdmi->variant->hp_detect(vc4_hdmi))
493 status = connector_status_connected;
496 vc4_hdmi_handle_hotplug(vc4_hdmi, ctx, status);
497 pm_runtime_put(&vc4_hdmi->pdev->dev);
502 static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
504 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
505 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
510 * NOTE: This function should really take vc4_hdmi->mutex, but
511 * doing so results in reentrancy issues since
512 * cec_s_phys_addr_from_edid might call .adap_enable, which
513 * leads to that funtion being called with our mutex held.
515 * Concurrency isn't an issue at the moment since we don't share
516 * any state with any of the other frameworks so we can ignore
520 edid = drm_get_edid(connector, vc4_hdmi->ddc);
521 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
525 drm_connector_update_edid_property(connector, edid);
526 ret = drm_add_edid_modes(connector, edid);
529 if (!vc4->hvs->vc5_hdmi_enable_hdmi_20) {
530 struct drm_device *drm = connector->dev;
531 const struct drm_display_mode *mode;
533 list_for_each_entry(mode, &connector->probed_modes, head) {
534 if (vc4_hdmi_mode_needs_scrambling(mode, 8, VC4_HDMI_OUTPUT_RGB)) {
535 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
536 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
544 static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
545 struct drm_atomic_state *state)
547 struct drm_connector_state *old_state =
548 drm_atomic_get_old_connector_state(state, connector);
549 struct vc4_hdmi_connector_state *old_vc4_state =
550 conn_state_to_vc4_hdmi_conn_state(old_state);
551 struct drm_connector_state *new_state =
552 drm_atomic_get_new_connector_state(state, connector);
553 struct vc4_hdmi_connector_state *new_vc4_state =
554 conn_state_to_vc4_hdmi_conn_state(new_state);
555 struct drm_crtc *crtc = new_state->crtc;
560 if (old_state->tv.margins.left != new_state->tv.margins.left ||
561 old_state->tv.margins.right != new_state->tv.margins.right ||
562 old_state->tv.margins.top != new_state->tv.margins.top ||
563 old_state->tv.margins.bottom != new_state->tv.margins.bottom) {
564 struct drm_crtc_state *crtc_state;
567 crtc_state = drm_atomic_get_crtc_state(state, crtc);
568 if (IS_ERR(crtc_state))
569 return PTR_ERR(crtc_state);
572 * Strictly speaking, we should be calling
573 * drm_atomic_helper_check_planes() after our call to
574 * drm_atomic_add_affected_planes(). However, the
575 * connector atomic_check is called as part of
576 * drm_atomic_helper_check_modeset() that already
577 * happens before a call to
578 * drm_atomic_helper_check_planes() in
579 * drm_atomic_helper_check().
581 ret = drm_atomic_add_affected_planes(state, crtc);
586 if (old_state->colorspace != new_state->colorspace ||
587 old_vc4_state->broadcast_rgb != new_vc4_state->broadcast_rgb ||
588 !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
589 struct drm_crtc_state *crtc_state;
591 crtc_state = drm_atomic_get_crtc_state(state, crtc);
592 if (IS_ERR(crtc_state))
593 return PTR_ERR(crtc_state);
595 crtc_state->mode_changed = true;
601 static int vc4_hdmi_connector_get_property(struct drm_connector *connector,
602 const struct drm_connector_state *state,
603 struct drm_property *property,
606 struct drm_device *drm = connector->dev;
607 struct vc4_hdmi *vc4_hdmi =
608 connector_to_vc4_hdmi(connector);
609 const struct vc4_hdmi_connector_state *vc4_conn_state =
610 conn_state_to_vc4_hdmi_conn_state(state);
612 if (property == vc4_hdmi->broadcast_rgb_property) {
613 *val = vc4_conn_state->broadcast_rgb;
615 drm_dbg(drm, "Unknown property [PROP:%d:%s]\n",
616 property->base.id, property->name);
623 static int vc4_hdmi_connector_set_property(struct drm_connector *connector,
624 struct drm_connector_state *state,
625 struct drm_property *property,
628 struct drm_device *drm = connector->dev;
629 struct vc4_hdmi *vc4_hdmi =
630 connector_to_vc4_hdmi(connector);
631 struct vc4_hdmi_connector_state *vc4_conn_state =
632 conn_state_to_vc4_hdmi_conn_state(state);
634 if (property == vc4_hdmi->broadcast_rgb_property) {
635 vc4_conn_state->broadcast_rgb = val;
639 drm_dbg(drm, "Unknown property [PROP:%d:%s]\n",
640 property->base.id, property->name);
644 static void vc4_hdmi_connector_reset(struct drm_connector *connector)
646 struct vc4_hdmi_connector_state *old_state =
647 conn_state_to_vc4_hdmi_conn_state(connector->state);
648 struct vc4_hdmi_connector_state *new_state =
649 kzalloc(sizeof(*new_state), GFP_KERNEL);
651 if (connector->state)
652 __drm_atomic_helper_connector_destroy_state(connector->state);
655 __drm_atomic_helper_connector_reset(connector, &new_state->base);
660 new_state->base.max_bpc = 8;
661 new_state->base.max_requested_bpc = 8;
662 new_state->output_format = VC4_HDMI_OUTPUT_RGB;
663 new_state->broadcast_rgb = VC4_HDMI_BROADCAST_RGB_AUTO;
664 drm_atomic_helper_connector_tv_margins_reset(connector);
667 static struct drm_connector_state *
668 vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
670 struct drm_connector_state *conn_state = connector->state;
671 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
672 struct vc4_hdmi_connector_state *new_state;
674 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
678 new_state->tmds_char_rate = vc4_state->tmds_char_rate;
679 new_state->output_bpc = vc4_state->output_bpc;
680 new_state->output_format = vc4_state->output_format;
681 new_state->broadcast_rgb = vc4_state->broadcast_rgb;
682 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
684 return &new_state->base;
687 static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
688 .fill_modes = drm_helper_probe_single_connector_modes,
689 .reset = vc4_hdmi_connector_reset,
690 .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
691 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
692 .atomic_get_property = vc4_hdmi_connector_get_property,
693 .atomic_set_property = vc4_hdmi_connector_set_property,
696 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
697 .detect_ctx = vc4_hdmi_connector_detect_ctx,
698 .get_modes = vc4_hdmi_connector_get_modes,
699 .atomic_check = vc4_hdmi_connector_atomic_check,
702 static const struct drm_prop_enum_list broadcast_rgb_names[] = {
703 { VC4_HDMI_BROADCAST_RGB_AUTO, "Automatic" },
704 { VC4_HDMI_BROADCAST_RGB_FULL, "Full" },
705 { VC4_HDMI_BROADCAST_RGB_LIMITED, "Limited 16:235" },
709 vc4_hdmi_attach_broadcast_rgb_property(struct drm_device *dev,
710 struct vc4_hdmi *vc4_hdmi)
712 struct drm_property *prop = vc4_hdmi->broadcast_rgb_property;
715 prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
718 ARRAY_SIZE(broadcast_rgb_names));
722 vc4_hdmi->broadcast_rgb_property = prop;
725 drm_object_attach_property(&vc4_hdmi->connector.base, prop,
726 VC4_HDMI_BROADCAST_RGB_AUTO);
729 static int vc4_hdmi_connector_init(struct drm_device *dev,
730 struct vc4_hdmi *vc4_hdmi)
732 struct drm_connector *connector = &vc4_hdmi->connector;
733 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
736 ret = drmm_connector_init(dev, connector,
737 &vc4_hdmi_connector_funcs,
738 DRM_MODE_CONNECTOR_HDMIA,
743 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
746 * Some of the properties below require access to state, like bpc.
747 * Allocate some default initial connector state with our reset helper.
749 if (connector->funcs->reset)
750 connector->funcs->reset(connector);
752 /* Create and attach TV margin props to this connector. */
753 ret = drm_mode_create_tv_margin_properties(dev);
757 ret = drm_mode_create_hdmi_colorspace_property(connector, 0);
761 drm_connector_attach_colorspace_property(connector);
762 drm_connector_attach_tv_margin_properties(connector);
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_max_bpc_property(connector, 8, 12);
773 drm_connector_attach_hdr_output_metadata_property(connector);
775 drm_connector_attach_max_bpc_property(connector, 8, 8);
778 vc4_hdmi_attach_broadcast_rgb_property(dev, vc4_hdmi);
780 drm_connector_attach_encoder(connector, encoder);
785 static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
786 enum hdmi_infoframe_type type,
789 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
790 struct drm_device *drm = vc4_hdmi->connector.dev;
791 u32 packet_id = type - 0x80;
796 if (!drm_dev_enter(drm, &idx))
799 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
800 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
801 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
802 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
805 ret = wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
806 BIT(packet_id)), 100);
813 static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
814 union hdmi_infoframe *frame)
816 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
817 struct drm_device *drm = vc4_hdmi->connector.dev;
818 u32 packet_id = frame->any.type - 0x80;
819 const struct vc4_hdmi_register *ram_packet_start =
820 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
821 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
822 u32 packet_reg_next = ram_packet_start->offset +
823 VC4_HDMI_PACKET_STRIDE * (packet_id + 1);
824 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
825 ram_packet_start->reg);
826 uint8_t buffer[VC4_HDMI_PACKET_STRIDE] = {};
832 if (!drm_dev_enter(drm, &idx))
835 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
836 VC4_HDMI_RAM_PACKET_ENABLE),
837 "Packet RAM has to be on to store the packet.");
839 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
843 ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
845 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
849 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
851 for (i = 0; i < len; i += 7) {
852 writel(buffer[i + 0] << 0 |
858 writel(buffer[i + 3] << 0 |
860 buffer[i + 5] << 16 |
867 * clear remainder of packet ram as it's included in the
868 * infoframe and triggers a checksum error on hdmi analyser
870 for (; packet_reg < packet_reg_next; packet_reg += 4)
871 writel(0, base + packet_reg);
873 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
874 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
876 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
878 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
879 BIT(packet_id)), 100);
881 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
887 static void vc4_hdmi_avi_infoframe_colorspace(struct hdmi_avi_infoframe *frame,
888 enum vc4_hdmi_output_format fmt)
891 case VC4_HDMI_OUTPUT_RGB:
892 frame->colorspace = HDMI_COLORSPACE_RGB;
895 case VC4_HDMI_OUTPUT_YUV420:
896 frame->colorspace = HDMI_COLORSPACE_YUV420;
899 case VC4_HDMI_OUTPUT_YUV422:
900 frame->colorspace = HDMI_COLORSPACE_YUV422;
903 case VC4_HDMI_OUTPUT_YUV444:
904 frame->colorspace = HDMI_COLORSPACE_YUV444;
912 static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
914 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
915 struct drm_connector *connector = &vc4_hdmi->connector;
916 struct drm_connector_state *cstate = connector->state;
917 struct vc4_hdmi_connector_state *vc4_state =
918 conn_state_to_vc4_hdmi_conn_state(cstate);
919 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
920 union hdmi_infoframe frame;
923 lockdep_assert_held(&vc4_hdmi->mutex);
925 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
928 DRM_ERROR("couldn't fill AVI infoframe\n");
932 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
934 vc4_hdmi_is_full_range(vc4_hdmi, vc4_state) ?
935 HDMI_QUANTIZATION_RANGE_FULL :
936 HDMI_QUANTIZATION_RANGE_LIMITED);
937 drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
938 vc4_hdmi_avi_infoframe_colorspace(&frame.avi, vc4_state->output_format);
939 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
941 vc4_hdmi_write_infoframe(encoder, &frame);
944 static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
946 union hdmi_infoframe frame;
949 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
951 DRM_ERROR("couldn't fill SPD infoframe\n");
955 frame.spd.sdi = HDMI_SPD_SDI_PC;
957 vc4_hdmi_write_infoframe(encoder, &frame);
960 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
962 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
963 struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
964 union hdmi_infoframe frame;
966 memcpy(&frame.audio, audio, sizeof(*audio));
968 if (vc4_hdmi->packet_ram_enabled)
969 vc4_hdmi_write_infoframe(encoder, &frame);
972 static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
974 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
975 struct drm_connector *connector = &vc4_hdmi->connector;
976 struct drm_connector_state *conn_state = connector->state;
977 union hdmi_infoframe frame;
979 lockdep_assert_held(&vc4_hdmi->mutex);
981 if (!vc4_hdmi->variant->supports_hdr)
984 if (!conn_state->hdr_output_metadata)
987 if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
990 vc4_hdmi_write_infoframe(encoder, &frame);
993 static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
995 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
997 lockdep_assert_held(&vc4_hdmi->mutex);
999 vc4_hdmi_set_avi_infoframe(encoder);
1000 vc4_hdmi_set_spd_infoframe(encoder);
1002 * If audio was streaming, then we need to reenabled the audio
1003 * infoframe here during encoder_enable.
1005 if (vc4_hdmi->audio.streaming)
1006 vc4_hdmi_set_audio_infoframe(encoder);
1008 vc4_hdmi_set_hdr_infoframe(encoder);
1011 #define SCRAMBLING_POLLING_DELAY_MS 1000
1013 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
1015 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1016 struct drm_connector *connector = &vc4_hdmi->connector;
1017 struct drm_device *drm = connector->dev;
1018 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1019 unsigned long flags;
1022 lockdep_assert_held(&vc4_hdmi->mutex);
1024 if (!vc4_hdmi_supports_scrambling(vc4_hdmi))
1027 if (!vc4_hdmi_mode_needs_scrambling(mode,
1028 vc4_hdmi->output_bpc,
1029 vc4_hdmi->output_format))
1032 if (!drm_dev_enter(drm, &idx))
1035 drm_scdc_set_high_tmds_clock_ratio(connector, true);
1036 drm_scdc_set_scrambling(connector, true);
1038 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1039 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
1040 VC5_HDMI_SCRAMBLER_CTL_ENABLE);
1041 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1045 vc4_hdmi->scdc_enabled = true;
1047 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
1048 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
1051 static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
1053 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1054 struct drm_connector *connector = &vc4_hdmi->connector;
1055 struct drm_device *drm = connector->dev;
1056 unsigned long flags;
1059 lockdep_assert_held(&vc4_hdmi->mutex);
1061 if (!vc4_hdmi->scdc_enabled)
1064 vc4_hdmi->scdc_enabled = false;
1066 if (delayed_work_pending(&vc4_hdmi->scrambling_work))
1067 cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
1069 if (!drm_dev_enter(drm, &idx))
1072 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1073 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
1074 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
1075 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1077 drm_scdc_set_scrambling(connector, false);
1078 drm_scdc_set_high_tmds_clock_ratio(connector, false);
1083 static void vc4_hdmi_scrambling_wq(struct work_struct *work)
1085 struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
1088 struct drm_connector *connector = &vc4_hdmi->connector;
1090 if (drm_scdc_get_scrambling_status(connector))
1093 drm_scdc_set_high_tmds_clock_ratio(connector, true);
1094 drm_scdc_set_scrambling(connector, true);
1096 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
1097 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
1100 static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
1101 struct drm_atomic_state *state)
1103 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1104 struct drm_device *drm = vc4_hdmi->connector.dev;
1105 struct vc4_dev *vc4 = to_vc4_dev(drm);
1106 unsigned long flags;
1109 mutex_lock(&vc4_hdmi->mutex);
1111 vc4_hdmi->packet_ram_enabled = false;
1113 if (!drm_dev_enter(drm, &idx))
1116 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1118 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
1120 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
1122 if (vc4->gen >= VC4_GEN_6)
1123 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) |
1124 VC4_HD_VID_CTL_BLANKPIX);
1126 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1131 * TODO: This should work on BCM2712, but doesn't for some
1132 * reason and result in a system lockup.
1134 if (vc4->gen < VC4_GEN_6) {
1135 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1136 HDMI_WRITE(HDMI_VID_CTL,
1137 HDMI_READ(HDMI_VID_CTL) &
1138 ~VC4_HD_VID_CTL_ENABLE);
1139 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1142 vc4_hdmi_disable_scrambling(encoder);
1147 mutex_unlock(&vc4_hdmi->mutex);
1150 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
1151 struct drm_atomic_state *state)
1153 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1154 struct drm_device *drm = vc4_hdmi->connector.dev;
1155 unsigned long flags;
1159 mutex_lock(&vc4_hdmi->mutex);
1161 if (!drm_dev_enter(drm, &idx))
1164 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1165 HDMI_WRITE(HDMI_VID_CTL,
1166 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
1167 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1169 if (vc4_hdmi->variant->phy_disable)
1170 vc4_hdmi->variant->phy_disable(vc4_hdmi);
1172 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
1173 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1175 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
1177 DRM_ERROR("Failed to release power domain: %d\n", ret);
1182 mutex_unlock(&vc4_hdmi->mutex);
1185 static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1186 struct drm_connector_state *state,
1187 const struct drm_display_mode *mode)
1189 struct vc4_hdmi_connector_state *vc4_state =
1190 conn_state_to_vc4_hdmi_conn_state(state);
1191 struct drm_device *drm = vc4_hdmi->connector.dev;
1192 unsigned long flags;
1196 if (!drm_dev_enter(drm, &idx))
1199 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1201 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
1202 VC4_HD_CSC_CTL_ORDER);
1204 if (!vc4_hdmi_is_full_range(vc4_hdmi, vc4_state)) {
1205 /* CEA VICs other than #1 requre limited range RGB
1206 * output unless overridden by an AVI infoframe.
1207 * Apply a colorspace conversion to squash 0-255 down
1208 * to 16-235. The matrix here is:
1215 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
1216 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
1217 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1218 VC4_HD_CSC_CTL_MODE);
1220 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
1221 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
1222 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
1223 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
1224 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
1225 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
1228 /* The RGB order applies even when CSC is disabled. */
1229 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1231 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1237 * Matrices for (internal) RGB to RGB output.
1239 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1241 static const u16 vc5_hdmi_csc_full_rgb_to_rgb[2][3][4] = {
1244 * Full range - unity
1250 { 0x2000, 0x0000, 0x0000, 0x0000 },
1251 { 0x0000, 0x2000, 0x0000, 0x0000 },
1252 { 0x0000, 0x0000, 0x2000, 0x0000 },
1258 * CEA VICs other than #1 require limited range RGB
1259 * output unless overridden by an AVI infoframe. Apply a
1260 * colorspace conversion to squash 0-255 down to 16-235.
1261 * The matrix here is:
1267 { 0x1b80, 0x0000, 0x0000, 0x0400 },
1268 { 0x0000, 0x1b80, 0x0000, 0x0400 },
1269 { 0x0000, 0x0000, 0x1b80, 0x0400 },
1274 * Conversion between Full Range RGB and YUV using the BT.601 Colorspace
1276 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1278 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt601[2][3][4] = {
1283 * [ 0.299000 0.587000 0.114000 0 ]
1284 * [ -0.168736 -0.331264 0.500000 128 ]
1285 * [ 0.500000 -0.418688 -0.081312 128 ]
1287 { 0x0991, 0x12c9, 0x03a6, 0x0000 },
1288 { 0xfa9b, 0xf567, 0x1000, 0x2000 },
1289 { 0x1000, 0xf29b, 0xfd67, 0x2000 },
1294 * [ 0.255785 0.502160 0.097523 16 ]
1295 * [ -0.147644 -0.289856 0.437500 128 ]
1296 * [ 0.437500 -0.366352 -0.071148 128 ]
1298 { 0x082f, 0x1012, 0x031f, 0x0400 },
1299 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
1300 { 0x0e00, 0xf448, 0xfdba, 0x2000 },
1305 * Conversion between Full Range RGB and YUV using the BT.709 Colorspace
1307 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1309 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt709[2][3][4] = {
1314 * [ 0.212600 0.715200 0.072200 0 ]
1315 * [ -0.114572 -0.385428 0.500000 128 ]
1316 * [ 0.500000 -0.454153 -0.045847 128 ]
1318 { 0x06ce, 0x16e3, 0x024f, 0x0000 },
1319 { 0xfc56, 0xf3ac, 0x1000, 0x2000 },
1320 { 0x1000, 0xf179, 0xfe89, 0x2000 },
1326 * [ 0.181906 0.611804 0.061758 16 ]
1327 * [ -0.100268 -0.337232 0.437500 128 ]
1328 * [ 0.437500 -0.397386 -0.040114 128 ]
1330 { 0x05d2, 0x1394, 0x01fa, 0x0400 },
1331 { 0xfccc, 0xf536, 0x0e00, 0x2000 },
1332 { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
1337 * Conversion between Full Range RGB and YUV using the BT.2020 Colorspace
1339 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1341 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt2020[2][3][4] = {
1346 * [ 0.262700 0.678000 0.059300 0 ]
1347 * [ -0.139630 -0.360370 0.500000 128 ]
1348 * [ 0.500000 -0.459786 -0.040214 128 ]
1350 { 0x0868, 0x15b2, 0x01e6, 0x0000 },
1351 { 0xfb89, 0xf479, 0x1000, 0x2000 },
1352 { 0x1000, 0xf14a, 0xfeb8, 0x2000 },
1357 * [ 0.224732 0.580008 0.050729 16 ]
1358 * [ -0.122176 -0.315324 0.437500 128 ]
1359 * [ 0.437500 -0.402312 -0.035188 128 ]
1361 { 0x082f, 0x1012, 0x031f, 0x0400 },
1362 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
1363 { 0x0e00, 0xf448, 0xfdba, 0x2000 },
1367 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
1368 const u16 coeffs[3][4])
1370 lockdep_assert_held(&vc4_hdmi->hw_lock);
1372 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
1373 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
1374 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
1375 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
1376 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
1377 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
1380 static void vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi *vc4_hdmi,
1381 const u16 coeffs[3][4])
1383 lockdep_assert_held(&vc4_hdmi->hw_lock);
1385 /* YUV444 needs the CSC matrices using the channels in a different order */
1386 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[1][1] << 16) | coeffs[1][0]);
1387 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[1][3] << 16) | coeffs[1][2]);
1388 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[2][1] << 16) | coeffs[2][0]);
1389 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[2][3] << 16) | coeffs[2][2]);
1390 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[0][1] << 16) | coeffs[0][0]);
1391 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[0][3] << 16) | coeffs[0][2]);
1395 (*vc5_hdmi_find_yuv_csc_coeffs(struct vc4_hdmi *vc4_hdmi, u32 colorspace, bool limited))[4]
1397 switch (colorspace) {
1398 case DRM_MODE_COLORIMETRY_SMPTE_170M_YCC:
1399 case DRM_MODE_COLORIMETRY_XVYCC_601:
1400 case DRM_MODE_COLORIMETRY_SYCC_601:
1401 case DRM_MODE_COLORIMETRY_OPYCC_601:
1402 case DRM_MODE_COLORIMETRY_BT601_YCC:
1403 return vc5_hdmi_csc_full_rgb_to_yuv_bt601[limited];
1406 case DRM_MODE_COLORIMETRY_NO_DATA:
1407 case DRM_MODE_COLORIMETRY_BT709_YCC:
1408 case DRM_MODE_COLORIMETRY_XVYCC_709:
1409 case DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED:
1410 case DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT:
1411 return vc5_hdmi_csc_full_rgb_to_yuv_bt709[limited];
1413 case DRM_MODE_COLORIMETRY_BT2020_CYCC:
1414 case DRM_MODE_COLORIMETRY_BT2020_YCC:
1415 case DRM_MODE_COLORIMETRY_BT2020_RGB:
1416 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
1417 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
1418 return vc5_hdmi_csc_full_rgb_to_yuv_bt2020[limited];
1422 static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1423 struct drm_connector_state *state,
1424 const struct drm_display_mode *mode)
1426 struct drm_device *drm = vc4_hdmi->connector.dev;
1427 struct vc4_hdmi_connector_state *vc4_state =
1428 conn_state_to_vc4_hdmi_conn_state(state);
1429 unsigned int lim_range = vc4_hdmi_is_full_range(vc4_hdmi, vc4_state) ? 0 : 1;
1430 unsigned long flags;
1431 const u16 (*csc)[4];
1433 u32 if_xbar = 0x543210;
1434 u32 csc_chan_ctl = 0;
1435 u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1436 VC5_MT_CP_CSC_CTL_MODE);
1439 if (!drm_dev_enter(drm, &idx))
1442 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1444 switch (vc4_state->output_format) {
1445 case VC4_HDMI_OUTPUT_YUV444:
1446 csc = vc5_hdmi_find_yuv_csc_coeffs(vc4_hdmi, state->colorspace, !!lim_range);
1448 vc5_hdmi_set_csc_coeffs_swap(vc4_hdmi, csc);
1451 case VC4_HDMI_OUTPUT_YUV422:
1452 csc = vc5_hdmi_find_yuv_csc_coeffs(vc4_hdmi, state->colorspace, !!lim_range);
1454 csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
1455 VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
1456 VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
1457 VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
1459 csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
1460 VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
1462 if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
1463 VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);
1465 vc5_hdmi_set_csc_coeffs(vc4_hdmi, csc);
1468 case VC4_HDMI_OUTPUT_RGB:
1471 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_rgb[lim_range]);
1478 HDMI_WRITE(HDMI_VEC_INTERFACE_CFG, if_cfg);
1479 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, if_xbar);
1480 HDMI_WRITE(HDMI_CSC_CHANNEL_CTL, csc_chan_ctl);
1481 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1483 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1488 static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1489 struct drm_connector_state *state,
1490 const struct drm_display_mode *mode)
1492 struct drm_device *drm = vc4_hdmi->connector.dev;
1493 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1494 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1495 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1496 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1497 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1498 VC4_HDMI_VERTA_VSP) |
1499 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1500 VC4_HDMI_VERTA_VFP) |
1501 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
1502 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1503 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1505 VC4_HDMI_VERTB_VBP));
1506 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1507 VC4_SET_FIELD(mode->crtc_vtotal -
1508 mode->crtc_vsync_end,
1509 VC4_HDMI_VERTB_VBP));
1510 unsigned long flags;
1514 if (!drm_dev_enter(drm, &idx))
1517 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1519 HDMI_WRITE(HDMI_HORZA,
1520 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
1521 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
1522 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1523 VC4_HDMI_HORZA_HAP));
1525 HDMI_WRITE(HDMI_HORZB,
1526 VC4_SET_FIELD((mode->htotal -
1527 mode->hsync_end) * pixel_rep,
1528 VC4_HDMI_HORZB_HBP) |
1529 VC4_SET_FIELD((mode->hsync_end -
1530 mode->hsync_start) * pixel_rep,
1531 VC4_HDMI_HORZB_HSP) |
1532 VC4_SET_FIELD((mode->hsync_start -
1533 mode->hdisplay) * pixel_rep,
1534 VC4_HDMI_HORZB_HFP));
1536 HDMI_WRITE(HDMI_VERTA0, verta);
1537 HDMI_WRITE(HDMI_VERTA1, verta);
1539 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1540 HDMI_WRITE(HDMI_VERTB1, vertb);
1542 reg = HDMI_READ(HDMI_MISC_CONTROL);
1543 reg &= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1544 reg |= VC4_SET_FIELD(pixel_rep - 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP);
1545 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1547 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1552 static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1553 struct drm_connector_state *state,
1554 const struct drm_display_mode *mode)
1556 struct drm_device *drm = vc4_hdmi->connector.dev;
1557 const struct vc4_hdmi_connector_state *vc4_state =
1558 conn_state_to_vc4_hdmi_conn_state(state);
1559 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1560 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1561 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1562 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1563 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1564 VC5_HDMI_VERTA_VSP) |
1565 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1566 VC5_HDMI_VERTA_VFP) |
1567 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
1568 u32 vertb = (VC4_SET_FIELD(mode->htotal >> (2 - pixel_rep),
1569 VC5_HDMI_VERTB_VSPO) |
1570 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1572 VC4_HDMI_VERTB_VBP));
1573 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
1574 VC4_SET_FIELD(mode->crtc_vtotal -
1575 mode->crtc_vsync_end,
1576 VC4_HDMI_VERTB_VBP));
1577 unsigned long flags;
1582 if (!drm_dev_enter(drm, &idx))
1585 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1587 HDMI_WRITE(HDMI_HORZA,
1588 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
1589 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
1590 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1591 VC5_HDMI_HORZA_HAP) |
1592 VC4_SET_FIELD((mode->hsync_start -
1593 mode->hdisplay) * pixel_rep,
1594 VC5_HDMI_HORZA_HFP));
1596 HDMI_WRITE(HDMI_HORZB,
1597 VC4_SET_FIELD((mode->htotal -
1598 mode->hsync_end) * pixel_rep,
1599 VC5_HDMI_HORZB_HBP) |
1600 VC4_SET_FIELD((mode->hsync_end -
1601 mode->hsync_start) * pixel_rep,
1602 VC5_HDMI_HORZB_HSP));
1604 HDMI_WRITE(HDMI_VERTA0, verta);
1605 HDMI_WRITE(HDMI_VERTA1, verta);
1607 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1608 HDMI_WRITE(HDMI_VERTB1, vertb);
1610 switch (vc4_state->output_bpc) {
1624 * YCC422 is always 36-bit and not considered deep colour so
1625 * doesn't signal in GCP.
1627 if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
1631 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
1632 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
1633 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
1634 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
1635 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
1636 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
1638 reg = HDMI_READ(HDMI_GCP_WORD_1);
1639 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
1640 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
1641 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK;
1642 reg |= VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE;
1643 HDMI_WRITE(HDMI_GCP_WORD_1, reg);
1645 reg = HDMI_READ(HDMI_GCP_CONFIG);
1646 reg |= VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
1647 HDMI_WRITE(HDMI_GCP_CONFIG, reg);
1649 reg = HDMI_READ(HDMI_MISC_CONTROL);
1650 reg &= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1651 reg |= VC4_SET_FIELD(pixel_rep - 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP);
1652 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1654 HDMI_WRITE(HDMI_CLOCK_STOP, 0);
1656 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1661 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
1663 struct drm_device *drm = vc4_hdmi->connector.dev;
1664 unsigned long flags;
1669 if (!drm_dev_enter(drm, &idx))
1672 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1674 drift = HDMI_READ(HDMI_FIFO_CTL);
1675 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
1677 HDMI_WRITE(HDMI_FIFO_CTL,
1678 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1679 HDMI_WRITE(HDMI_FIFO_CTL,
1680 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1682 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1684 usleep_range(1000, 1100);
1686 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1688 HDMI_WRITE(HDMI_FIFO_CTL,
1689 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1690 HDMI_WRITE(HDMI_FIFO_CTL,
1691 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1693 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1695 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
1696 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
1697 WARN_ONCE(ret, "Timeout waiting for "
1698 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
1703 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
1704 struct drm_atomic_state *state)
1706 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1707 struct drm_device *drm = vc4_hdmi->connector.dev;
1708 struct drm_connector *connector = &vc4_hdmi->connector;
1709 struct drm_connector_state *conn_state =
1710 drm_atomic_get_new_connector_state(state, connector);
1711 struct vc4_hdmi_connector_state *vc4_conn_state =
1712 conn_state_to_vc4_hdmi_conn_state(conn_state);
1713 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1714 unsigned long tmds_char_rate = vc4_conn_state->tmds_char_rate;
1715 unsigned long bvb_rate, hsm_rate;
1716 unsigned long flags;
1720 mutex_lock(&vc4_hdmi->mutex);
1722 if (!drm_dev_enter(drm, &idx))
1725 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
1727 DRM_ERROR("Failed to retain power domain: %d\n", ret);
1732 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
1733 * be faster than pixel clock, infinitesimally faster, tested in
1734 * simulation. Otherwise, exact value is unimportant for HDMI
1735 * operation." This conflicts with bcm2835's vc4 documentation, which
1736 * states HSM's clock has to be at least 108% of the pixel clock.
1738 * Real life tests reveal that vc4's firmware statement holds up, and
1739 * users are able to use pixel clocks closer to HSM's, namely for
1740 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
1741 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
1744 * Additionally, the AXI clock needs to be at least 25% of
1745 * pixel clock, but HSM ends up being the limiting factor.
1747 hsm_rate = max_t(unsigned long,
1749 (tmds_char_rate / 100) * 101);
1750 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
1752 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1753 goto err_put_runtime_pm;
1756 ret = clk_set_rate(vc4_hdmi->pixel_clock, tmds_char_rate);
1758 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
1759 goto err_put_runtime_pm;
1762 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
1764 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
1765 goto err_put_runtime_pm;
1768 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
1770 if (tmds_char_rate > 297000000)
1771 bvb_rate = 300000000;
1772 else if (tmds_char_rate > 148500000)
1773 bvb_rate = 150000000;
1775 bvb_rate = 75000000;
1777 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
1779 DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
1780 goto err_disable_pixel_clock;
1783 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
1785 DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
1786 goto err_disable_pixel_clock;
1789 if (vc4_hdmi->variant->phy_init)
1790 vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
1792 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1794 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1795 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1796 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
1797 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
1799 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1801 if (vc4_hdmi->variant->set_timings)
1802 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
1806 mutex_unlock(&vc4_hdmi->mutex);
1810 err_disable_pixel_clock:
1811 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1813 pm_runtime_put(&vc4_hdmi->pdev->dev);
1817 mutex_unlock(&vc4_hdmi->mutex);
1821 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
1822 struct drm_atomic_state *state)
1824 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1825 struct drm_device *drm = vc4_hdmi->connector.dev;
1826 struct drm_connector *connector = &vc4_hdmi->connector;
1827 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1828 struct drm_connector_state *conn_state =
1829 drm_atomic_get_new_connector_state(state, connector);
1830 unsigned long flags;
1833 mutex_lock(&vc4_hdmi->mutex);
1835 if (!drm_dev_enter(drm, &idx))
1838 if (vc4_hdmi->variant->csc_setup)
1839 vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
1841 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1842 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
1843 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1848 mutex_unlock(&vc4_hdmi->mutex);
1851 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
1852 struct drm_atomic_state *state)
1854 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1855 struct drm_device *drm = vc4_hdmi->connector.dev;
1856 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1857 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
1858 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1859 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1860 unsigned long flags;
1864 mutex_lock(&vc4_hdmi->mutex);
1866 if (!drm_dev_enter(drm, &idx))
1869 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1871 HDMI_WRITE(HDMI_VID_CTL,
1872 HDMI_READ(HDMI_VID_CTL) |
1873 VC4_HD_VID_CTL_ENABLE |
1874 VC4_HD_VID_CTL_CLRRGB |
1875 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
1876 VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
1877 VC4_HD_VID_CTL_BLANK_INSERT_EN |
1878 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
1879 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
1881 HDMI_WRITE(HDMI_VID_CTL,
1882 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1884 if (display->is_hdmi) {
1885 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1886 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1887 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1889 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1891 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1892 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1893 WARN_ONCE(ret, "Timeout waiting for "
1894 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1896 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1897 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
1898 ~(VC4_HDMI_RAM_PACKET_ENABLE));
1899 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1900 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1901 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1903 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1905 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1906 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1907 WARN_ONCE(ret, "Timeout waiting for "
1908 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1911 if (display->is_hdmi) {
1912 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1914 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1915 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
1917 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1918 VC4_HDMI_RAM_PACKET_ENABLE);
1920 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1921 vc4_hdmi->packet_ram_enabled = true;
1923 vc4_hdmi_set_infoframes(encoder);
1926 vc4_hdmi_recenter_fifo(vc4_hdmi);
1927 vc4_hdmi_enable_scrambling(encoder);
1932 mutex_unlock(&vc4_hdmi->mutex);
1935 static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
1936 struct drm_crtc_state *crtc_state,
1937 struct drm_connector_state *conn_state)
1939 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1940 struct vc4_hdmi_connector_state *vc4_state =
1941 conn_state_to_vc4_hdmi_conn_state(conn_state);
1943 mutex_lock(&vc4_hdmi->mutex);
1944 drm_mode_copy(&vc4_hdmi->saved_adjusted_mode,
1945 &crtc_state->adjusted_mode);
1946 vc4_hdmi->output_bpc = vc4_state->output_bpc;
1947 vc4_hdmi->output_format = vc4_state->output_format;
1948 mutex_unlock(&vc4_hdmi->mutex);
1952 vc4_hdmi_sink_supports_format_bpc(const struct vc4_hdmi *vc4_hdmi,
1953 const struct drm_display_info *info,
1954 const struct drm_display_mode *mode,
1955 unsigned int format, unsigned int bpc)
1957 struct drm_device *dev = vc4_hdmi->connector.dev;
1958 u8 vic = drm_match_cea_mode(mode);
1960 if (vic == 1 && bpc != 8) {
1961 drm_dbg(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
1965 if (!info->is_hdmi &&
1966 (format != VC4_HDMI_OUTPUT_RGB || bpc != 8)) {
1967 drm_dbg(dev, "DVI Monitors require an RGB output at 8 bpc\n");
1972 case VC4_HDMI_OUTPUT_RGB:
1973 drm_dbg(dev, "RGB Format, checking the constraints.\n");
1975 if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
1976 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
1980 if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
1981 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
1985 drm_dbg(dev, "RGB format supported in that configuration.\n");
1989 case VC4_HDMI_OUTPUT_YUV422:
1990 drm_dbg(dev, "YUV422 format, checking the constraints.\n");
1992 if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR422)) {
1993 drm_dbg(dev, "Sink doesn't support YUV422.\n");
1998 drm_dbg(dev, "YUV422 only supports 12 bpc.\n");
2002 drm_dbg(dev, "YUV422 format supported in that configuration.\n");
2006 case VC4_HDMI_OUTPUT_YUV444:
2007 drm_dbg(dev, "YUV444 format, checking the constraints.\n");
2009 if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR444)) {
2010 drm_dbg(dev, "Sink doesn't support YUV444.\n");
2014 if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) {
2015 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
2019 if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) {
2020 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
2024 drm_dbg(dev, "YUV444 format supported in that configuration.\n");
2032 static enum drm_mode_status
2033 vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi *vc4_hdmi,
2034 const struct drm_display_mode *mode,
2035 unsigned long long clock)
2037 const struct drm_connector *connector = &vc4_hdmi->connector;
2038 const struct drm_display_info *info = &connector->display_info;
2039 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
2041 if (clock > vc4_hdmi->variant->max_pixel_clock)
2042 return MODE_CLOCK_HIGH;
2044 if (!vc4->hvs->vc5_hdmi_enable_hdmi_20 && clock > HDMI_14_MAX_TMDS_CLK)
2045 return MODE_CLOCK_HIGH;
2047 /* 4096x2160@60 is not reliable without overclocking core */
2048 if (!vc4->hvs->vc5_hdmi_enable_4096by2160 &&
2049 mode->hdisplay > 3840 && mode->vdisplay >= 2160 &&
2050 drm_mode_vrefresh(mode) >= 50)
2051 return MODE_CLOCK_HIGH;
2053 if (info->max_tmds_clock && clock > (info->max_tmds_clock * 1000))
2054 return MODE_CLOCK_HIGH;
2059 static unsigned long long
2060 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
2062 enum vc4_hdmi_output_format fmt)
2064 unsigned long long clock = mode->clock * 1000ULL;
2066 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
2069 if (fmt == VC4_HDMI_OUTPUT_YUV422)
2072 clock = clock * bpc;
2079 vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi *vc4_hdmi,
2080 struct vc4_hdmi_connector_state *vc4_state,
2081 const struct drm_display_mode *mode,
2082 unsigned int bpc, unsigned int fmt)
2084 unsigned long long clock;
2086 clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
2087 if (vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode, clock) != MODE_OK)
2090 vc4_state->tmds_char_rate = clock;
2096 vc4_hdmi_encoder_compute_format(const struct vc4_hdmi *vc4_hdmi,
2097 struct vc4_hdmi_connector_state *vc4_state,
2098 const struct drm_display_mode *mode,
2101 struct drm_device *dev = vc4_hdmi->connector.dev;
2102 const struct drm_connector *connector = &vc4_hdmi->connector;
2103 const struct drm_display_info *info = &connector->display_info;
2104 unsigned int format;
2106 drm_dbg(dev, "Trying with an RGB output\n");
2108 format = VC4_HDMI_OUTPUT_RGB;
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, Trying with an YUV422 output\n");
2122 format = VC4_HDMI_OUTPUT_YUV422;
2123 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
2126 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
2129 vc4_state->output_format = format;
2134 drm_dbg(dev, "Failed. No Format Supported for that bpc count.\n");
2140 vc4_hdmi_encoder_compute_config(const struct vc4_hdmi *vc4_hdmi,
2141 struct vc4_hdmi_connector_state *vc4_state,
2142 const struct drm_display_mode *mode)
2144 struct drm_device *dev = vc4_hdmi->connector.dev;
2145 struct drm_connector_state *conn_state = &vc4_state->base;
2146 unsigned int max_bpc = clamp_t(unsigned int, conn_state->max_requested_bpc, 8, 12);
2150 for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
2151 drm_dbg(dev, "Trying with a %d bpc output\n", bpc);
2153 ret = vc4_hdmi_encoder_compute_format(vc4_hdmi, vc4_state,
2158 vc4_state->output_bpc = bpc;
2161 "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
2162 mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
2163 vc4_state->output_bpc,
2164 vc4_hdmi_output_fmt_str(vc4_state->output_format),
2165 vc4_state->tmds_char_rate);
2173 #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
2174 #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
2176 static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
2177 struct drm_crtc_state *crtc_state,
2178 struct drm_connector_state *conn_state)
2180 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2181 struct drm_connector *connector = &vc4_hdmi->connector;
2182 struct drm_connector_state *old_conn_state =
2183 drm_atomic_get_old_connector_state(conn_state->state, connector);
2184 struct vc4_hdmi_connector_state *old_vc4_state =
2185 conn_state_to_vc4_hdmi_conn_state(old_conn_state);
2186 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
2187 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
2188 unsigned long long tmds_char_rate = mode->clock * 1000;
2189 unsigned long long tmds_bit_rate;
2192 if (vc4_hdmi->variant->unsupported_odd_h_timings) {
2193 if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
2194 /* Only try to fixup DBLCLK modes to get 480i and 576i
2196 * A generic solution for all modes with odd horizontal
2197 * timing values seems impossible based on trying to
2198 * solve it for 1366x768 monitors.
2200 if ((mode->hsync_start - mode->hdisplay) & 1)
2201 mode->hsync_start--;
2202 if ((mode->hsync_end - mode->hsync_start) & 1)
2206 /* Now check whether we still have odd values remaining */
2207 if ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
2208 (mode->hsync_end % 2) || (mode->htotal % 2))
2213 * The 1440p@60 pixel rate is in the same range than the first
2214 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
2215 * bandwidth). Slightly lower the frequency to bring it out of
2218 tmds_bit_rate = tmds_char_rate * 10;
2219 if (vc4_hdmi->disable_wifi_frequencies &&
2220 (tmds_bit_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
2221 tmds_bit_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
2222 mode->clock = 238560;
2223 tmds_char_rate = mode->clock * 1000;
2226 ret = vc4_hdmi_encoder_compute_config(vc4_hdmi, vc4_state, mode);
2230 /* vc4_hdmi_encoder_compute_config may have changed output_bpc and/or output_format */
2231 if (vc4_state->output_bpc != old_vc4_state->output_bpc ||
2232 vc4_state->output_format != old_vc4_state->output_format)
2233 crtc_state->mode_changed = true;
2238 static enum drm_mode_status
2239 vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
2240 const struct drm_display_mode *mode)
2242 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2244 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
2245 !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
2246 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
2247 (mode->hsync_end % 2) || (mode->htotal % 2)))
2248 return MODE_H_ILLEGAL;
2250 return vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode, mode->clock * 1000);
2253 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
2254 .atomic_check = vc4_hdmi_encoder_atomic_check,
2255 .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
2256 .mode_valid = vc4_hdmi_encoder_mode_valid,
2259 static int vc4_hdmi_late_register(struct drm_encoder *encoder)
2261 struct drm_device *drm = encoder->dev;
2262 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2263 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
2265 drm_debugfs_add_file(drm, variant->debugfs_name,
2266 vc4_hdmi_debugfs_regs, vc4_hdmi);
2271 static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
2272 .late_register = vc4_hdmi_late_register,
2275 static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
2278 u32 channel_map = 0;
2280 for (i = 0; i < 8; i++) {
2281 if (channel_mask & BIT(i))
2282 channel_map |= i << (3 * i);
2287 static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
2290 u32 channel_map = 0;
2292 for (i = 0; i < 8; i++) {
2293 if (channel_mask & BIT(i))
2294 channel_map |= i << (4 * i);
2299 static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi)
2301 struct drm_device *drm = vc4_hdmi->connector.dev;
2302 unsigned long flags;
2306 if (!drm_dev_enter(drm, &idx))
2309 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2310 hotplug = HDMI_READ(HDMI_HOTPLUG);
2311 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2315 return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED);
2318 /* HDMI audio codec callbacks */
2319 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
2320 unsigned int samplerate)
2322 struct drm_device *drm = vc4_hdmi->connector.dev;
2324 unsigned long flags;
2328 if (!drm_dev_enter(drm, &idx))
2331 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
2332 rational_best_approximation(hsm_clock, samplerate,
2333 VC4_HD_MAI_SMP_N_MASK >>
2334 VC4_HD_MAI_SMP_N_SHIFT,
2335 (VC4_HD_MAI_SMP_M_MASK >>
2336 VC4_HD_MAI_SMP_M_SHIFT) + 1,
2339 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2340 HDMI_WRITE(HDMI_MAI_SMP,
2341 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
2342 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
2343 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2348 static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
2350 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
2354 lockdep_assert_held(&vc4_hdmi->mutex);
2355 lockdep_assert_held(&vc4_hdmi->hw_lock);
2357 n = 128 * samplerate / 1000;
2358 tmp = (u64)(mode->clock * 1000) * n;
2359 do_div(tmp, 128 * samplerate);
2362 HDMI_WRITE(HDMI_CRP_CFG,
2363 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
2364 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
2367 * We could get slightly more accurate clocks in some cases by
2368 * providing a CTS_1 value. The two CTS values are alternated
2369 * between based on the period fields
2371 HDMI_WRITE(HDMI_CTS_0, cts);
2372 HDMI_WRITE(HDMI_CTS_1, cts);
2375 static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
2377 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
2379 return snd_soc_card_get_drvdata(card);
2382 static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
2384 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
2386 lockdep_assert_held(&vc4_hdmi->mutex);
2389 * If the encoder is currently in DVI mode, treat the codec DAI
2392 if (!display->is_hdmi)
2398 static int vc4_hdmi_audio_startup(struct device *dev, void *data)
2400 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2401 struct drm_device *drm = vc4_hdmi->connector.dev;
2402 unsigned long flags;
2406 mutex_lock(&vc4_hdmi->mutex);
2408 if (!drm_dev_enter(drm, &idx)) {
2413 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2418 vc4_hdmi->audio.streaming = true;
2420 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2421 HDMI_WRITE(HDMI_MAI_CTL,
2422 VC4_HD_MAI_CTL_RESET |
2423 VC4_HD_MAI_CTL_FLUSH |
2424 VC4_HD_MAI_CTL_DLATE |
2425 VC4_HD_MAI_CTL_ERRORE |
2426 VC4_HD_MAI_CTL_ERRORF);
2427 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2429 if (vc4_hdmi->variant->phy_rng_enable)
2430 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
2435 mutex_unlock(&vc4_hdmi->mutex);
2440 static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
2442 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2443 struct device *dev = &vc4_hdmi->pdev->dev;
2444 unsigned long flags;
2447 lockdep_assert_held(&vc4_hdmi->mutex);
2449 vc4_hdmi->audio.streaming = false;
2450 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
2452 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
2454 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2456 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
2457 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
2458 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
2460 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2463 static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
2465 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2466 struct drm_device *drm = vc4_hdmi->connector.dev;
2467 unsigned long flags;
2470 mutex_lock(&vc4_hdmi->mutex);
2472 if (!drm_dev_enter(drm, &idx))
2475 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2477 HDMI_WRITE(HDMI_MAI_CTL,
2478 VC4_HD_MAI_CTL_DLATE |
2479 VC4_HD_MAI_CTL_ERRORE |
2480 VC4_HD_MAI_CTL_ERRORF);
2482 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2484 if (vc4_hdmi->variant->phy_rng_disable)
2485 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
2487 vc4_hdmi->audio.streaming = false;
2488 vc4_hdmi_audio_reset(vc4_hdmi);
2493 mutex_unlock(&vc4_hdmi->mutex);
2496 static int sample_rate_to_mai_fmt(int samplerate)
2498 switch (samplerate) {
2500 return VC4_HDMI_MAI_SAMPLE_RATE_8000;
2502 return VC4_HDMI_MAI_SAMPLE_RATE_11025;
2504 return VC4_HDMI_MAI_SAMPLE_RATE_12000;
2506 return VC4_HDMI_MAI_SAMPLE_RATE_16000;
2508 return VC4_HDMI_MAI_SAMPLE_RATE_22050;
2510 return VC4_HDMI_MAI_SAMPLE_RATE_24000;
2512 return VC4_HDMI_MAI_SAMPLE_RATE_32000;
2514 return VC4_HDMI_MAI_SAMPLE_RATE_44100;
2516 return VC4_HDMI_MAI_SAMPLE_RATE_48000;
2518 return VC4_HDMI_MAI_SAMPLE_RATE_64000;
2520 return VC4_HDMI_MAI_SAMPLE_RATE_88200;
2522 return VC4_HDMI_MAI_SAMPLE_RATE_96000;
2524 return VC4_HDMI_MAI_SAMPLE_RATE_128000;
2526 return VC4_HDMI_MAI_SAMPLE_RATE_176400;
2528 return VC4_HDMI_MAI_SAMPLE_RATE_192000;
2530 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
2534 /* HDMI audio codec callbacks */
2535 static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
2536 struct hdmi_codec_daifmt *daifmt,
2537 struct hdmi_codec_params *params)
2539 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2540 struct drm_device *drm = vc4_hdmi->connector.dev;
2541 struct vc4_dev *vc4 = to_vc4_dev(drm);
2542 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2543 unsigned int sample_rate = params->sample_rate;
2544 unsigned int channels = params->channels;
2545 unsigned long flags;
2546 u32 audio_packet_config, channel_mask;
2548 u32 mai_audio_format;
2549 u32 mai_sample_rate;
2553 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
2554 sample_rate, params->sample_width, channels);
2556 mutex_lock(&vc4_hdmi->mutex);
2558 if (!drm_dev_enter(drm, &idx)) {
2563 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2568 vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
2570 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2571 HDMI_WRITE(HDMI_MAI_CTL,
2572 VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
2573 VC4_HD_MAI_CTL_WHOLSMP |
2574 VC4_HD_MAI_CTL_CHALIGN |
2575 VC4_HD_MAI_CTL_ENABLE);
2577 mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
2578 if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
2579 params->channels == 8)
2580 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
2582 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
2583 HDMI_WRITE(HDMI_MAI_FMT,
2584 VC4_SET_FIELD(mai_sample_rate,
2585 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
2586 VC4_SET_FIELD(mai_audio_format,
2587 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));
2589 /* The B frame identifier should match the value used by alsa-lib (8) */
2590 audio_packet_config =
2591 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
2592 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
2593 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
2595 channel_mask = GENMASK(channels - 1, 0);
2596 audio_packet_config |= VC4_SET_FIELD(channel_mask,
2597 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
2599 /* Set the MAI threshold */
2600 if (vc4->gen >= VC4_GEN_5)
2601 HDMI_WRITE(HDMI_MAI_THR,
2602 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
2603 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
2604 VC4_SET_FIELD(0x1c, VC4_HD_MAI_THR_DREQHIGH) |
2605 VC4_SET_FIELD(0x1c, VC4_HD_MAI_THR_DREQLOW));
2607 HDMI_WRITE(HDMI_MAI_THR,
2608 VC4_SET_FIELD(0x8, VC4_HD_MAI_THR_PANICHIGH) |
2609 VC4_SET_FIELD(0x8, VC4_HD_MAI_THR_PANICLOW) |
2610 VC4_SET_FIELD(0x6, VC4_HD_MAI_THR_DREQHIGH) |
2611 VC4_SET_FIELD(0x8, VC4_HD_MAI_THR_DREQLOW));
2613 HDMI_WRITE(HDMI_MAI_CONFIG,
2614 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
2615 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
2616 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
2618 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
2619 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
2620 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
2622 vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
2624 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2626 memcpy(&vc4_hdmi->audio.infoframe, ¶ms->cea, sizeof(params->cea));
2627 vc4_hdmi_set_audio_infoframe(encoder);
2632 mutex_unlock(&vc4_hdmi->mutex);
2637 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
2638 .name = "vc4-hdmi-cpu-dai-component",
2639 .legacy_dai_naming = 1,
2642 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
2644 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
2646 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
2651 static const struct snd_soc_dai_ops vc4_snd_dai_ops = {
2652 .probe = vc4_hdmi_audio_cpu_dai_probe,
2655 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
2656 .name = "vc4-hdmi-cpu-dai",
2657 .ops = &vc4_snd_dai_ops,
2659 .stream_name = "Playback",
2662 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
2663 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
2664 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
2665 SNDRV_PCM_RATE_192000,
2666 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
2670 static const struct snd_dmaengine_pcm_config pcm_conf = {
2671 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
2672 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
2675 static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
2676 uint8_t *buf, size_t len)
2678 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2679 struct drm_connector *connector = &vc4_hdmi->connector;
2681 mutex_lock(&vc4_hdmi->mutex);
2682 memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
2683 mutex_unlock(&vc4_hdmi->mutex);
2688 static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
2689 .get_eld = vc4_hdmi_audio_get_eld,
2690 .prepare = vc4_hdmi_audio_prepare,
2691 .audio_shutdown = vc4_hdmi_audio_shutdown,
2692 .audio_startup = vc4_hdmi_audio_startup,
2695 static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
2696 .ops = &vc4_hdmi_codec_ops,
2697 .max_i2s_channels = 8,
2701 static void vc4_hdmi_audio_codec_release(void *ptr)
2703 struct vc4_hdmi *vc4_hdmi = ptr;
2705 platform_device_unregister(vc4_hdmi->audio.codec_pdev);
2706 vc4_hdmi->audio.codec_pdev = NULL;
2709 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
2711 const struct vc4_hdmi_register *mai_data =
2712 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
2713 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
2714 struct snd_soc_card *card = &vc4_hdmi->audio.card;
2715 struct device *dev = &vc4_hdmi->pdev->dev;
2716 struct platform_device *codec_pdev;
2717 struct resource *iomem;
2722 * ASoC makes it a bit hard to retrieve a pointer to the
2723 * vc4_hdmi structure. Registering the card will overwrite our
2724 * device drvdata with a pointer to the snd_soc_card structure,
2725 * which can then be used to retrieve whatever drvdata we want
2728 * However, that doesn't fly in the case where we wouldn't
2729 * register an ASoC card (because of an old DT that is missing
2730 * the dmas properties for example), then the card isn't
2731 * registered and the device drvdata wouldn't be set.
2733 * We can deal with both cases by making sure a snd_soc_card
2734 * pointer and a vc4_hdmi structure are pointing to the same
2735 * memory address, so we can treat them indistinctly without any
2738 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
2739 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
2741 if (!of_find_property(dev->of_node, "dmas", &len) || !len) {
2743 "'dmas' DT property is missing or empty, no HDMI audio\n");
2747 if (mai_data->reg != VC4_HD) {
2748 WARN_ONCE(true, "MAI isn't in the HD block\n");
2753 * Get the physical address of VC4_HD_MAI_DATA.
2755 index = of_property_match_string(dev->of_node, "reg-names", "hd");
2756 /* Before BCM2711, we don't have a named register range */
2759 iomem = platform_get_resource(vc4_hdmi->pdev, IORESOURCE_MEM, index);
2761 vc4_hdmi->audio.dma_data.addr = iomem->start + mai_data->offset;
2762 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2763 vc4_hdmi->audio.dma_data.maxburst = 2;
2766 * NOTE: Strictly speaking, we should probably use a DRM-managed
2767 * registration there to avoid removing all the audio components
2768 * by the time the driver doesn't have any user anymore.
2770 * However, the ASoC core uses a number of devm_kzalloc calls
2771 * when registering, even when using non-device-managed
2772 * functions (such as in snd_soc_register_component()).
2774 * If we call snd_soc_unregister_component() in a DRM-managed
2775 * action, the device-managed actions have already been executed
2776 * and thus we would access memory that has been freed.
2778 * Using device-managed hooks here probably leaves us open to a
2779 * bunch of issues if userspace still has a handle on the ALSA
2780 * device when the device is removed. However, this is mitigated
2781 * by the use of drm_dev_enter()/drm_dev_exit() in the audio
2782 * path to prevent the access to the device resources if it
2783 * isn't there anymore.
2785 * Then, the vc4_hdmi structure is DRM-managed and thus only
2786 * freed whenever the last user has closed the DRM device file.
2787 * It should thus outlive ALSA in most situations.
2789 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
2791 dev_err(dev, "Could not register PCM component: %d\n", ret);
2795 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
2796 &vc4_hdmi_audio_cpu_dai_drv, 1);
2798 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
2802 codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
2803 PLATFORM_DEVID_AUTO,
2804 &vc4_hdmi_codec_pdata,
2805 sizeof(vc4_hdmi_codec_pdata));
2806 if (IS_ERR(codec_pdev)) {
2807 dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
2808 return PTR_ERR(codec_pdev);
2810 vc4_hdmi->audio.codec_pdev = codec_pdev;
2812 ret = devm_add_action_or_reset(dev, vc4_hdmi_audio_codec_release, vc4_hdmi);
2816 dai_link->cpus = &vc4_hdmi->audio.cpu;
2817 dai_link->codecs = &vc4_hdmi->audio.codec;
2818 dai_link->platforms = &vc4_hdmi->audio.platform;
2820 dai_link->num_cpus = 1;
2821 dai_link->num_codecs = 1;
2822 dai_link->num_platforms = 1;
2824 dai_link->name = "MAI";
2825 dai_link->stream_name = "MAI PCM";
2826 dai_link->codecs->dai_name = "i2s-hifi";
2827 dai_link->cpus->dai_name = dev_name(dev);
2828 dai_link->codecs->name = dev_name(&codec_pdev->dev);
2829 dai_link->platforms->name = dev_name(dev);
2831 card->dai_link = dai_link;
2832 card->num_links = 1;
2833 card->name = vc4_hdmi->variant->card_name;
2834 card->driver_name = "vc4-hdmi";
2836 card->owner = THIS_MODULE;
2839 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
2840 * stores a pointer to the snd card object in dev->driver_data. This
2841 * means we cannot use it for something else. The hdmi back-pointer is
2842 * now stored in card->drvdata and should be retrieved with
2843 * snd_soc_card_get_drvdata() if needed.
2845 snd_soc_card_set_drvdata(card, vc4_hdmi);
2846 ret = devm_snd_soc_register_card(dev, card);
2848 dev_err_probe(dev, ret, "Could not register sound card\n");
2854 static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
2856 struct vc4_hdmi *vc4_hdmi = priv;
2857 struct drm_connector *connector = &vc4_hdmi->connector;
2858 struct drm_device *dev = connector->dev;
2860 if (dev && dev->registered)
2861 drm_connector_helper_hpd_irq_event(connector);
2866 static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
2868 struct drm_connector *connector = &vc4_hdmi->connector;
2869 struct platform_device *pdev = vc4_hdmi->pdev;
2872 if (vc4_hdmi->variant->external_irq_controller) {
2873 unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
2874 unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
2876 ret = devm_request_threaded_irq(&pdev->dev, hpd_con,
2878 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2879 "vc4 hdmi hpd connected", vc4_hdmi);
2883 ret = devm_request_threaded_irq(&pdev->dev, hpd_rm,
2885 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2886 "vc4 hdmi hpd disconnected", vc4_hdmi);
2890 connector->polled = DRM_CONNECTOR_POLL_HPD;
2896 #ifdef CONFIG_DRM_VC4_HDMI_CEC
2897 static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
2899 struct vc4_hdmi *vc4_hdmi = priv;
2901 if (vc4_hdmi->cec_rx_msg.len)
2902 cec_received_msg(vc4_hdmi->cec_adap,
2903 &vc4_hdmi->cec_rx_msg);
2908 static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
2910 struct vc4_hdmi *vc4_hdmi = priv;
2912 if (vc4_hdmi->cec_tx_ok) {
2913 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
2917 * This CEC implementation makes 1 retry, so if we
2918 * get a NACK, then that means it made 2 attempts.
2920 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
2926 static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
2928 struct vc4_hdmi *vc4_hdmi = priv;
2931 if (vc4_hdmi->cec_irq_was_rx)
2932 ret = vc4_cec_irq_handler_rx_thread(irq, priv);
2934 ret = vc4_cec_irq_handler_tx_thread(irq, priv);
2939 static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
2941 struct drm_device *dev = vc4_hdmi->connector.dev;
2942 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
2945 lockdep_assert_held(&vc4_hdmi->hw_lock);
2947 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
2948 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
2950 if (msg->len > 16) {
2951 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
2955 for (i = 0; i < msg->len; i += 4) {
2956 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
2958 msg->msg[i] = val & 0xff;
2959 msg->msg[i + 1] = (val >> 8) & 0xff;
2960 msg->msg[i + 2] = (val >> 16) & 0xff;
2961 msg->msg[i + 3] = (val >> 24) & 0xff;
2965 static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2970 * We don't need to protect the register access using
2971 * drm_dev_enter() there because the interrupt handler lifetime
2972 * is tied to the device itself, and not to the DRM device.
2974 * So when the device will be gone, one of the first thing we
2975 * will be doing will be to unregister the interrupt handler,
2976 * and then unregister the DRM device. drm_dev_enter() would
2977 * thus always succeed if we are here.
2980 lockdep_assert_held(&vc4_hdmi->hw_lock);
2982 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
2983 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
2984 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
2985 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
2987 return IRQ_WAKE_THREAD;
2990 static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
2992 struct vc4_hdmi *vc4_hdmi = priv;
2995 spin_lock(&vc4_hdmi->hw_lock);
2996 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
2997 spin_unlock(&vc4_hdmi->hw_lock);
3002 static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
3006 lockdep_assert_held(&vc4_hdmi->hw_lock);
3009 * We don't need to protect the register access using
3010 * drm_dev_enter() there because the interrupt handler lifetime
3011 * is tied to the device itself, and not to the DRM device.
3013 * So when the device will be gone, one of the first thing we
3014 * will be doing will be to unregister the interrupt handler,
3015 * and then unregister the DRM device. drm_dev_enter() would
3016 * thus always succeed if we are here.
3019 vc4_hdmi->cec_rx_msg.len = 0;
3020 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
3021 vc4_cec_read_msg(vc4_hdmi, cntrl1);
3022 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
3023 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
3024 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
3026 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
3028 return IRQ_WAKE_THREAD;
3031 static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
3033 struct vc4_hdmi *vc4_hdmi = priv;
3036 spin_lock(&vc4_hdmi->hw_lock);
3037 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
3038 spin_unlock(&vc4_hdmi->hw_lock);
3043 static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
3045 struct vc4_hdmi *vc4_hdmi = priv;
3046 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
3051 * We don't need to protect the register access using
3052 * drm_dev_enter() there because the interrupt handler lifetime
3053 * is tied to the device itself, and not to the DRM device.
3055 * So when the device will be gone, one of the first thing we
3056 * will be doing will be to unregister the interrupt handler,
3057 * and then unregister the DRM device. drm_dev_enter() would
3058 * thus always succeed if we are here.
3061 if (!(stat & VC4_HDMI_CPU_CEC))
3064 spin_lock(&vc4_hdmi->hw_lock);
3065 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
3066 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
3067 if (vc4_hdmi->cec_irq_was_rx)
3068 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
3070 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
3072 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
3073 spin_unlock(&vc4_hdmi->hw_lock);
3078 static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
3080 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3081 struct drm_device *drm = vc4_hdmi->connector.dev;
3082 /* clock period in microseconds */
3083 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
3084 unsigned long flags;
3089 if (!drm_dev_enter(drm, &idx))
3091 * We can't return an error code, because the CEC
3092 * framework will emit WARN_ON messages at unbind
3097 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
3103 mutex_lock(&vc4_hdmi->mutex);
3105 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3107 val = HDMI_READ(HDMI_CEC_CNTRL_5);
3108 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
3109 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
3110 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
3111 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
3112 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
3114 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
3115 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
3116 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
3117 HDMI_WRITE(HDMI_CEC_CNTRL_2,
3118 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
3119 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
3120 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
3121 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
3122 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
3123 HDMI_WRITE(HDMI_CEC_CNTRL_3,
3124 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
3125 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
3126 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
3127 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
3128 HDMI_WRITE(HDMI_CEC_CNTRL_4,
3129 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
3130 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
3131 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
3132 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
3134 if (!vc4_hdmi->variant->external_irq_controller)
3135 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
3137 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3139 mutex_unlock(&vc4_hdmi->mutex);
3145 static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
3147 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3148 struct drm_device *drm = vc4_hdmi->connector.dev;
3149 unsigned long flags;
3152 if (!drm_dev_enter(drm, &idx))
3154 * We can't return an error code, because the CEC
3155 * framework will emit WARN_ON messages at unbind
3160 mutex_lock(&vc4_hdmi->mutex);
3162 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3164 if (!vc4_hdmi->variant->external_irq_controller)
3165 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
3167 HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
3168 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
3170 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3172 mutex_unlock(&vc4_hdmi->mutex);
3174 pm_runtime_put(&vc4_hdmi->pdev->dev);
3181 static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
3184 return vc4_hdmi_cec_enable(adap);
3186 return vc4_hdmi_cec_disable(adap);
3189 static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
3191 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3192 struct drm_device *drm = vc4_hdmi->connector.dev;
3193 unsigned long flags;
3196 if (!drm_dev_enter(drm, &idx))
3198 * We can't return an error code, because the CEC
3199 * framework will emit WARN_ON messages at unbind
3204 mutex_lock(&vc4_hdmi->mutex);
3205 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3206 HDMI_WRITE(HDMI_CEC_CNTRL_1,
3207 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
3208 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
3209 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3210 mutex_unlock(&vc4_hdmi->mutex);
3217 static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
3218 u32 signal_free_time, struct cec_msg *msg)
3220 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3221 struct drm_device *dev = vc4_hdmi->connector.dev;
3222 unsigned long flags;
3227 if (!drm_dev_enter(dev, &idx))
3230 if (msg->len > 16) {
3231 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
3236 mutex_lock(&vc4_hdmi->mutex);
3238 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3240 for (i = 0; i < msg->len; i += 4)
3241 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
3243 (msg->msg[i + 1] << 8) |
3244 (msg->msg[i + 2] << 16) |
3245 (msg->msg[i + 3] << 24));
3247 val = HDMI_READ(HDMI_CEC_CNTRL_1);
3248 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
3249 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
3250 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
3251 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
3252 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
3254 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
3256 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3257 mutex_unlock(&vc4_hdmi->mutex);
3263 static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
3264 .adap_enable = vc4_hdmi_cec_adap_enable,
3265 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
3266 .adap_transmit = vc4_hdmi_cec_adap_transmit,
3269 static void vc4_hdmi_cec_release(void *ptr)
3271 struct vc4_hdmi *vc4_hdmi = ptr;
3273 cec_unregister_adapter(vc4_hdmi->cec_adap);
3274 vc4_hdmi->cec_adap = NULL;
3277 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
3279 struct cec_connector_info conn_info;
3280 struct platform_device *pdev = vc4_hdmi->pdev;
3281 struct device *dev = &pdev->dev;
3284 if (!of_property_present(dev->of_node, "interrupts")) {
3285 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
3289 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
3291 vc4_hdmi->variant->card_name,
3293 CEC_CAP_CONNECTOR_INFO, 1);
3294 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
3298 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
3299 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
3301 if (vc4_hdmi->variant->external_irq_controller) {
3302 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-rx"),
3303 vc4_cec_irq_handler_rx_bare,
3304 vc4_cec_irq_handler_rx_thread, 0,
3305 "vc4 hdmi cec rx", vc4_hdmi);
3307 goto err_delete_cec_adap;
3309 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-tx"),
3310 vc4_cec_irq_handler_tx_bare,
3311 vc4_cec_irq_handler_tx_thread, 0,
3312 "vc4 hdmi cec tx", vc4_hdmi);
3314 goto err_delete_cec_adap;
3316 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
3317 vc4_cec_irq_handler,
3318 vc4_cec_irq_handler_thread, 0,
3319 "vc4 hdmi cec", vc4_hdmi);
3321 goto err_delete_cec_adap;
3324 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
3326 goto err_delete_cec_adap;
3329 * NOTE: Strictly speaking, we should probably use a DRM-managed
3330 * registration there to avoid removing the CEC adapter by the
3331 * time the DRM driver doesn't have any user anymore.
3333 * However, the CEC framework already cleans up the CEC adapter
3334 * only when the last user has closed its file descriptor, so we
3335 * don't need to handle it in DRM.
3337 * By the time the device-managed hook is executed, we will give
3338 * up our reference to the CEC adapter and therefore don't
3339 * really care when it's actually freed.
3341 * There's still a problematic sequence: if we unregister our
3342 * CEC adapter, but the userspace keeps a handle on the CEC
3343 * adapter but not the DRM device for some reason. In such a
3344 * case, our vc4_hdmi structure will be freed, but the
3345 * cec_adapter structure will have a dangling pointer to what
3346 * used to be our HDMI controller. If we get a CEC call at that
3347 * moment, we could end up with a use-after-free. Fortunately,
3348 * the CEC framework already handles this too, by calling
3349 * cec_is_registered() in cec_ioctl() and cec_poll().
3351 ret = devm_add_action_or_reset(dev, vc4_hdmi_cec_release, vc4_hdmi);
3357 err_delete_cec_adap:
3358 cec_delete_adapter(vc4_hdmi->cec_adap);
3363 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
3369 static void vc4_hdmi_free_regset(struct drm_device *drm, void *ptr)
3371 struct debugfs_reg32 *regs = ptr;
3376 static int vc4_hdmi_build_regset(struct drm_device *drm,
3377 struct vc4_hdmi *vc4_hdmi,
3378 struct debugfs_regset32 *regset,
3379 enum vc4_hdmi_regs reg)
3381 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
3382 struct debugfs_reg32 *regs, *new_regs;
3383 unsigned int count = 0;
3387 regs = kcalloc(variant->num_registers, sizeof(*regs),
3392 for (i = 0; i < variant->num_registers; i++) {
3393 const struct vc4_hdmi_register *field = &variant->registers[i];
3395 if (field->reg != reg)
3398 regs[count].name = field->name;
3399 regs[count].offset = field->offset;
3403 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
3407 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
3408 regset->regs = new_regs;
3409 regset->nregs = count;
3411 ret = drmm_add_action_or_reset(drm, vc4_hdmi_free_regset, new_regs);
3418 static int vc4_hdmi_init_resources(struct drm_device *drm,
3419 struct vc4_hdmi *vc4_hdmi)
3421 struct platform_device *pdev = vc4_hdmi->pdev;
3422 struct device *dev = &pdev->dev;
3425 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
3426 if (IS_ERR(vc4_hdmi->hdmicore_regs))
3427 return PTR_ERR(vc4_hdmi->hdmicore_regs);
3429 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
3430 if (IS_ERR(vc4_hdmi->hd_regs))
3431 return PTR_ERR(vc4_hdmi->hd_regs);
3433 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3437 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3441 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
3442 if (IS_ERR(vc4_hdmi->pixel_clock)) {
3443 ret = PTR_ERR(vc4_hdmi->pixel_clock);
3444 if (ret != -EPROBE_DEFER)
3445 DRM_ERROR("Failed to get pixel clock\n");
3449 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3450 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3451 DRM_ERROR("Failed to get HDMI state machine clock\n");
3452 return PTR_ERR(vc4_hdmi->hsm_clock);
3454 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
3455 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
3460 static int vc5_hdmi_init_resources(struct drm_device *drm,
3461 struct vc4_hdmi *vc4_hdmi)
3463 struct platform_device *pdev = vc4_hdmi->pdev;
3464 struct device *dev = &pdev->dev;
3465 struct resource *res;
3468 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
3472 vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
3473 resource_size(res));
3474 if (!vc4_hdmi->hdmicore_regs)
3477 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
3481 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
3482 if (!vc4_hdmi->hd_regs)
3485 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
3489 vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
3490 if (!vc4_hdmi->cec_regs)
3493 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
3497 vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
3498 if (!vc4_hdmi->csc_regs)
3501 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
3505 vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
3506 if (!vc4_hdmi->dvp_regs)
3509 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
3513 vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
3514 if (!vc4_hdmi->phy_regs)
3517 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
3521 vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
3522 if (!vc4_hdmi->ram_regs)
3525 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
3529 vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
3530 if (!vc4_hdmi->rm_regs)
3533 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3534 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3535 DRM_ERROR("Failed to get HDMI state machine clock\n");
3536 return PTR_ERR(vc4_hdmi->hsm_clock);
3539 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
3540 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
3541 DRM_ERROR("Failed to get pixel bvb clock\n");
3542 return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
3545 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
3546 if (IS_ERR(vc4_hdmi->audio_clock)) {
3547 DRM_ERROR("Failed to get audio clock\n");
3548 return PTR_ERR(vc4_hdmi->audio_clock);
3551 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
3552 if (IS_ERR(vc4_hdmi->cec_clock)) {
3553 DRM_ERROR("Failed to get CEC clock\n");
3554 return PTR_ERR(vc4_hdmi->cec_clock);
3557 vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
3558 if (IS_ERR(vc4_hdmi->reset)) {
3559 DRM_ERROR("Failed to get HDMI reset line\n");
3560 return PTR_ERR(vc4_hdmi->reset);
3563 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3567 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3571 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->cec_regset, VC5_CEC);
3575 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->csc_regset, VC5_CSC);
3579 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->dvp_regset, VC5_DVP);
3583 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->phy_regset, VC5_PHY);
3587 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->ram_regset, VC5_RAM);
3591 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->rm_regset, VC5_RM);
3598 static int vc4_hdmi_runtime_suspend(struct device *dev)
3600 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3602 clk_disable_unprepare(vc4_hdmi->audio_clock);
3603 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3608 static int vc4_hdmi_runtime_resume(struct device *dev)
3610 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3611 unsigned long __maybe_unused flags;
3612 u32 __maybe_unused value;
3616 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
3621 * Whenever the RaspberryPi boots without an HDMI monitor
3622 * plugged in, the firmware won't have initialized the HSM clock
3623 * rate and it will be reported as 0.
3625 * If we try to access a register of the controller in such a
3626 * case, it will lead to a silent CPU stall. Let's make sure we
3627 * prevent such a case.
3629 rate = clk_get_rate(vc4_hdmi->hsm_clock);
3632 goto err_disable_clk;
3635 ret = clk_prepare_enable(vc4_hdmi->audio_clock);
3637 goto err_disable_clk;
3639 if (vc4_hdmi->variant->reset)
3640 vc4_hdmi->variant->reset(vc4_hdmi);
3642 #ifdef CONFIG_DRM_VC4_HDMI_CEC
3643 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3644 value = HDMI_READ(HDMI_CEC_CNTRL_1);
3645 /* Set the logical address to Unregistered */
3646 value |= VC4_HDMI_CEC_ADDR_MASK;
3647 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
3648 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3650 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
3652 if (!vc4_hdmi->variant->external_irq_controller) {
3653 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3654 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
3655 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3662 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3666 static void vc4_hdmi_put_ddc_device(void *ptr)
3668 struct vc4_hdmi *vc4_hdmi = ptr;
3670 put_device(&vc4_hdmi->ddc->dev);
3673 static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
3675 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
3676 struct platform_device *pdev = to_platform_device(dev);
3677 struct drm_device *drm = dev_get_drvdata(master);
3678 struct vc4_hdmi *vc4_hdmi;
3679 struct drm_encoder *encoder;
3680 struct device_node *ddc_node;
3683 vc4_hdmi = drmm_kzalloc(drm, sizeof(*vc4_hdmi), GFP_KERNEL);
3687 ret = drmm_mutex_init(drm, &vc4_hdmi->mutex);
3691 spin_lock_init(&vc4_hdmi->hw_lock);
3692 INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
3694 dev_set_drvdata(dev, vc4_hdmi);
3695 encoder = &vc4_hdmi->encoder.base;
3696 vc4_hdmi->encoder.type = variant->encoder_type;
3697 vc4_hdmi->encoder.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
3698 vc4_hdmi->encoder.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
3699 vc4_hdmi->encoder.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
3700 vc4_hdmi->encoder.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
3701 vc4_hdmi->encoder.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
3702 vc4_hdmi->pdev = pdev;
3703 vc4_hdmi->variant = variant;
3706 * Since we don't know the state of the controller and its
3707 * display (if any), let's assume it's always enabled.
3708 * vc4_hdmi_disable_scrambling() will thus run at boot, make
3709 * sure it's disabled, and avoid any inconsistency.
3711 if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK)
3712 vc4_hdmi->scdc_enabled = true;
3714 ret = variant->init_resources(drm, vc4_hdmi);
3718 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
3720 DRM_ERROR("Failed to find ddc node in device tree\n");
3724 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
3725 of_node_put(ddc_node);
3726 if (!vc4_hdmi->ddc) {
3727 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
3728 return -EPROBE_DEFER;
3731 ret = devm_add_action_or_reset(dev, vc4_hdmi_put_ddc_device, vc4_hdmi);
3735 /* Only use the GPIO HPD pin if present in the DT, otherwise
3736 * we'll use the HDMI core's register.
3738 vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
3739 if (IS_ERR(vc4_hdmi->hpd_gpio)) {
3740 return PTR_ERR(vc4_hdmi->hpd_gpio);
3743 vc4_hdmi->disable_wifi_frequencies =
3744 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
3746 ret = devm_pm_runtime_enable(dev);
3751 * We need to have the device powered up at this point to call
3752 * our reset hook and for the CEC init.
3754 ret = pm_runtime_resume_and_get(dev);
3758 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
3759 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1") ||
3760 of_device_is_compatible(dev->of_node, "brcm,bcm2712-hdmi0") ||
3761 of_device_is_compatible(dev->of_node, "brcm,bcm2712-hdmi1")) &&
3762 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
3763 clk_prepare_enable(vc4_hdmi->pixel_clock);
3764 clk_prepare_enable(vc4_hdmi->hsm_clock);
3765 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
3768 ret = drmm_encoder_init(drm, encoder,
3769 &vc4_hdmi_encoder_funcs,
3770 DRM_MODE_ENCODER_TMDS,
3773 goto err_put_runtime_pm;
3775 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
3777 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
3779 goto err_put_runtime_pm;
3781 ret = vc4_hdmi_hotplug_init(vc4_hdmi);
3783 goto err_put_runtime_pm;
3785 ret = vc4_hdmi_cec_init(vc4_hdmi);
3787 goto err_put_runtime_pm;
3789 ret = vc4_hdmi_audio_init(vc4_hdmi);
3791 goto err_put_runtime_pm;
3793 pm_runtime_put_sync(dev);
3798 pm_runtime_put_sync(dev);
3803 static const struct component_ops vc4_hdmi_ops = {
3804 .bind = vc4_hdmi_bind,
3807 static int vc4_hdmi_dev_probe(struct platform_device *pdev)
3809 return component_add(&pdev->dev, &vc4_hdmi_ops);
3812 static void vc4_hdmi_dev_remove(struct platform_device *pdev)
3814 component_del(&pdev->dev, &vc4_hdmi_ops);
3817 static const struct vc4_hdmi_variant bcm2835_variant = {
3818 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3819 .debugfs_name = "hdmi_regs",
3820 .card_name = "vc4-hdmi",
3821 .max_pixel_clock = 162000000,
3822 .registers = vc4_hdmi_fields,
3823 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
3825 .init_resources = vc4_hdmi_init_resources,
3826 .csc_setup = vc4_hdmi_csc_setup,
3827 .reset = vc4_hdmi_reset,
3828 .set_timings = vc4_hdmi_set_timings,
3829 .phy_init = vc4_hdmi_phy_init,
3830 .phy_disable = vc4_hdmi_phy_disable,
3831 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
3832 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
3833 .channel_map = vc4_hdmi_channel_map,
3834 .supports_hdr = false,
3837 static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
3838 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3839 .debugfs_name = "hdmi0_regs",
3840 .card_name = "vc4-hdmi-0",
3841 .max_pixel_clock = 600000000,
3842 .registers = vc5_hdmi_hdmi0_fields,
3843 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
3844 .phy_lane_mapping = {
3850 .unsupported_odd_h_timings = true,
3851 .external_irq_controller = true,
3853 .init_resources = vc5_hdmi_init_resources,
3854 .csc_setup = vc5_hdmi_csc_setup,
3855 .reset = vc5_hdmi_reset,
3856 .set_timings = vc5_hdmi_set_timings,
3857 .phy_init = vc5_hdmi_phy_init,
3858 .phy_disable = vc5_hdmi_phy_disable,
3859 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3860 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3861 .channel_map = vc5_hdmi_channel_map,
3862 .supports_hdr = true,
3863 .hp_detect = vc5_hdmi_hp_detect,
3866 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
3867 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
3868 .debugfs_name = "hdmi1_regs",
3869 .card_name = "vc4-hdmi-1",
3870 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
3871 .registers = vc5_hdmi_hdmi1_fields,
3872 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
3873 .phy_lane_mapping = {
3879 .unsupported_odd_h_timings = true,
3880 .external_irq_controller = true,
3882 .init_resources = vc5_hdmi_init_resources,
3883 .csc_setup = vc5_hdmi_csc_setup,
3884 .reset = vc5_hdmi_reset,
3885 .set_timings = vc5_hdmi_set_timings,
3886 .phy_init = vc5_hdmi_phy_init,
3887 .phy_disable = vc5_hdmi_phy_disable,
3888 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3889 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3890 .channel_map = vc5_hdmi_channel_map,
3891 .supports_hdr = true,
3892 .hp_detect = vc5_hdmi_hp_detect,
3895 static const struct vc4_hdmi_variant bcm2712_hdmi0_variant = {
3896 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3897 .debugfs_name = "hdmi0_regs",
3898 .card_name = "vc4-hdmi-0",
3899 .max_pixel_clock = 600000000,
3900 .registers = vc6_hdmi_hdmi0_fields,
3901 .num_registers = ARRAY_SIZE(vc6_hdmi_hdmi0_fields),
3902 .phy_lane_mapping = {
3908 .unsupported_odd_h_timings = false,
3909 .external_irq_controller = true,
3911 .init_resources = vc5_hdmi_init_resources,
3912 .csc_setup = vc5_hdmi_csc_setup,
3913 .reset = vc5_hdmi_reset,
3914 .set_timings = vc5_hdmi_set_timings,
3915 .phy_init = vc6_hdmi_phy_init,
3916 .phy_disable = vc6_hdmi_phy_disable,
3917 .channel_map = vc5_hdmi_channel_map,
3918 .supports_hdr = true,
3919 .hp_detect = vc5_hdmi_hp_detect,
3922 static const struct vc4_hdmi_variant bcm2712_hdmi1_variant = {
3923 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
3924 .debugfs_name = "hdmi1_regs",
3925 .card_name = "vc4-hdmi-1",
3926 .max_pixel_clock = 600000000,
3927 .registers = vc6_hdmi_hdmi1_fields,
3928 .num_registers = ARRAY_SIZE(vc6_hdmi_hdmi1_fields),
3929 .phy_lane_mapping = {
3935 .unsupported_odd_h_timings = false,
3936 .external_irq_controller = true,
3938 .init_resources = vc5_hdmi_init_resources,
3939 .csc_setup = vc5_hdmi_csc_setup,
3940 .reset = vc5_hdmi_reset,
3941 .set_timings = vc5_hdmi_set_timings,
3942 .phy_init = vc6_hdmi_phy_init,
3943 .phy_disable = vc6_hdmi_phy_disable,
3944 .channel_map = vc5_hdmi_channel_map,
3945 .supports_hdr = true,
3946 .hp_detect = vc5_hdmi_hp_detect,
3949 static const struct of_device_id vc4_hdmi_dt_match[] = {
3950 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
3951 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
3952 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
3953 { .compatible = "brcm,bcm2712-hdmi0", .data = &bcm2712_hdmi0_variant },
3954 { .compatible = "brcm,bcm2712-hdmi1", .data = &bcm2712_hdmi1_variant },
3958 static const struct dev_pm_ops vc4_hdmi_pm_ops = {
3959 SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
3960 vc4_hdmi_runtime_resume,
3964 struct platform_driver vc4_hdmi_driver = {
3965 .probe = vc4_hdmi_dev_probe,
3966 .remove_new = vc4_hdmi_dev_remove,
3969 .of_match_table = vc4_hdmi_dt_match,
3970 .pm = &vc4_hdmi_pm_ops,