drm/vc4: hdmi: Fix hotplug extcon uevent to works
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / vc4 / vc4_hdmi.c
index 9b3e642..5a20e7b 100644 (file)
@@ -32,6 +32,7 @@
  */
 
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_drv.h>
 #include <drm/drm_edid.h>
 #include <drm/drm_probe_helper.h>
 #include <drm/drm_simple_kms_helper.h>
 #include <linux/clk.h>
 #include <linux/component.h>
 #include <linux/gpio/consumer.h>
+#include <linux/extcon-provider.h>
 #include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
 #include <linux/of_address.h>
 #include <linux/of_gpio.h>
 #include <linux/of_platform.h>
 #include "vc4_hdmi_regs.h"
 #include "vc4_regs.h"
 
+/*
+ * "Broadcast RGB" property.
+ * Allows overriding of HDMI full or limited range RGB
+ */
+#define VC4_BROADCAST_RGB_AUTO 0
+#define VC4_BROADCAST_RGB_FULL 1
+#define VC4_BROADCAST_RGB_LIMITED 2
+
 #define VC5_HDMI_HORZA_HFP_SHIFT               16
 #define VC5_HDMI_HORZA_HFP_MASK                        VC4_MASK(28, 16)
 #define VC5_HDMI_HORZA_VPOS                    BIT(15)
@@ -79,6 +91,8 @@
 #define VC5_HDMI_VERTB_VSPO_SHIFT              16
 #define VC5_HDMI_VERTB_VSPO_MASK               VC4_MASK(29, 16)
 
+#define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT  0
+#define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK   VC4_MASK(3, 0)
 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT  0
 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK   VC4_MASK(3, 0)
 
 
 #define HDMI_14_MAX_TMDS_CLK   (340 * 1000 * 1000)
 
-static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode)
+/* bit field to force hotplug detection. bit0 = HDMI0 */
+static int force_hotplug = 0;
+module_param(force_hotplug, int, 0644);
+
+static const char * const output_format_str[] = {
+       [VC4_HDMI_OUTPUT_RGB]           = "RGB",
+       [VC4_HDMI_OUTPUT_YUV420]        = "YUV 4:2:0",
+       [VC4_HDMI_OUTPUT_YUV422]        = "YUV 4:2:2",
+       [VC4_HDMI_OUTPUT_YUV444]        = "YUV 4:4:4",
+};
+
+static const char *vc4_hdmi_output_fmt_str(enum vc4_hdmi_output_format fmt)
+{
+       if (fmt >= ARRAY_SIZE(output_format_str))
+               return "invalid";
+
+       return output_format_str[fmt];
+}
+
+static unsigned long long
+vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
+                                   unsigned int bpc, enum vc4_hdmi_output_format fmt);
+
+static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder)
+{
+       struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       struct drm_display_info *display = &vc4_hdmi->connector.display_info;
+
+       lockdep_assert_held(&vc4_hdmi->mutex);
+
+       if (!display->is_hdmi)
+               return false;
+
+       if (!display->hdmi.scdc.supported ||
+           !display->hdmi.scdc.scrambling.supported)
+               return false;
+
+       return true;
+}
+
+static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
+                                          unsigned int bpc,
+                                          enum vc4_hdmi_output_format fmt)
+{
+       unsigned long long clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
+
+       return clock > HDMI_14_MAX_TMDS_CLK;
+}
+
+static bool vc4_hdmi_is_full_range(struct vc4_hdmi *vc4_hdmi,
+                                  const struct drm_display_mode *mode)
 {
-       return (mode->clock * 1000) > HDMI_14_MAX_TMDS_CLK;
+       struct drm_display_info *display = &vc4_hdmi->connector.display_info;
+
+       if (vc4_hdmi->broadcast_rgb == VC4_BROADCAST_RGB_LIMITED)
+               return false;
+       else if (vc4_hdmi->broadcast_rgb == VC4_BROADCAST_RGB_FULL)
+               return true;
+       return !display->is_hdmi ||
+               drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
 }
 
 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
 {
        struct drm_info_node *node = (struct drm_info_node *)m->private;
        struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
+       struct drm_device *drm = vc4_hdmi->connector.dev;
        struct drm_printer p = drm_seq_file_printer(m);
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               return -ENODEV;
 
        drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
        drm_print_regset32(&p, &vc4_hdmi->hd_regset);
+       drm_print_regset32(&p, &vc4_hdmi->cec_regset);
+       drm_print_regset32(&p, &vc4_hdmi->csc_regset);
+       drm_print_regset32(&p, &vc4_hdmi->dvp_regset);
+       drm_print_regset32(&p, &vc4_hdmi->phy_regset);
+       drm_print_regset32(&p, &vc4_hdmi->ram_regset);
+       drm_print_regset32(&p, &vc4_hdmi->rm_regset);
+
+       drm_dev_exit(idx);
 
        return 0;
 }
 
 static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
 {
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
+       int idx;
+
+       /*
+        * We can be called by our bind callback, when the
+        * connector->dev pointer might not be initialised yet.
+        */
+       if (drm && !drm_dev_enter(drm, &idx))
+               return;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
        HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
        udelay(1);
        HDMI_WRITE(HDMI_M_CTL, 0);
@@ -133,23 +230,63 @@ static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
                   VC4_HDMI_SW_RESET_FORMAT_DETECT);
 
        HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       if (drm)
+               drm_dev_exit(idx);
 }
 
 static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
 {
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
+       int idx;
+
+       /*
+        * We can be called by our bind callback, when the
+        * connector->dev pointer might not be initialised yet.
+        */
+       if (drm && !drm_dev_enter(drm, &idx))
+               return;
+
        reset_control_reset(vc4_hdmi->reset);
 
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
        HDMI_WRITE(HDMI_DVP_CTL, 0);
 
        HDMI_WRITE(HDMI_CLOCK_STOP,
                   HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       if (drm)
+               drm_dev_exit(idx);
 }
 
 #ifdef CONFIG_DRM_VC4_HDMI_CEC
 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
 {
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long cec_rate;
+       unsigned long flags;
        u16 clk_cnt;
        u32 value;
+       int idx;
+
+       /*
+        * This function is called by our runtime_resume implementation
+        * and thus at bind time, when we haven't registered our
+        * connector yet and thus don't have a pointer to the DRM
+        * device.
+        */
+       if (drm && !drm_dev_enter(drm, &idx))
+               return;
+
+       cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
 
        value = HDMI_READ(HDMI_CEC_CNTRL_1);
        value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
@@ -158,84 +295,234 @@ static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
         * Set the clock divider: the hsm_clock rate and this divider
         * setting will give a 40 kHz CEC clock.
         */
-       clk_cnt = clk_get_rate(vc4_hdmi->cec_clock) / CEC_CLOCK_FREQ;
+       clk_cnt = cec_rate / CEC_CLOCK_FREQ;
        value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
        HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       if (drm)
+               drm_dev_exit(idx);
 }
 #else
 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
 #endif
 
-static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder);
+static int reset_pipe(struct drm_crtc *crtc,
+                       struct drm_modeset_acquire_ctx *ctx)
+{
+       struct drm_atomic_state *state;
+       struct drm_crtc_state *crtc_state;
+       int ret;
+
+       state = drm_atomic_state_alloc(crtc->dev);
+       if (!state)
+               return -ENOMEM;
+
+       state->acquire_ctx = ctx;
 
-static enum drm_connector_status
-vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
+       crtc_state = drm_atomic_get_crtc_state(state, crtc);
+       if (IS_ERR(crtc_state)) {
+               ret = PTR_ERR(crtc_state);
+               goto out;
+       }
+
+       crtc_state->connectors_changed = true;
+
+       ret = drm_atomic_commit(state);
+out:
+       drm_atomic_state_put(state);
+
+       return ret;
+}
+
+static int vc4_hdmi_reset_link(struct drm_connector *connector,
+                              struct drm_modeset_acquire_ctx *ctx)
 {
+       struct drm_device *drm = connector->dev;
        struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
-       bool connected = false;
+       struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
+       struct drm_connector_state *conn_state;
+       struct drm_crtc_state *crtc_state;
+       struct drm_crtc *crtc;
+       bool scrambling_needed;
+       u8 config;
+       int ret;
 
-       WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
+       if (!connector)
+               return 0;
 
-       if (vc4_hdmi->hpd_gpio) {
-               if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
-                       connected = true;
-       } else if (drm_probe_ddc(vc4_hdmi->ddc)) {
-               connected = true;
-       } else if (HDMI_READ(HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED) {
-               connected = true;
+       ret = drm_modeset_lock(&drm->mode_config.connection_mutex, ctx);
+       if (ret)
+               return ret;
+
+       conn_state = connector->state;
+       crtc = conn_state->crtc;
+       if (!crtc)
+               return 0;
+
+       ret = drm_modeset_lock(&crtc->mutex, ctx);
+       if (ret)
+               return ret;
+
+       crtc_state = crtc->state;
+       if (!crtc_state->active)
+               return 0;
+
+       if (!vc4_hdmi_supports_scrambling(encoder))
+               return 0;
+
+       scrambling_needed = vc4_hdmi_mode_needs_scrambling(&vc4_hdmi->saved_adjusted_mode,
+                                                          vc4_hdmi->output_bpc,
+                                                          vc4_hdmi->output_format);
+       if (!scrambling_needed)
+               return 0;
+
+       if (conn_state->commit &&
+           !try_wait_for_completion(&conn_state->commit->hw_done))
+               return 0;
+
+       ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
+       if (ret < 0) {
+               drm_err(drm, "Failed to read TMDS config: %d\n", ret);
+               return 0;
        }
 
-       if (connected) {
-               if (connector->status != connector_status_connected) {
-                       struct edid *edid = drm_get_edid(connector, vc4_hdmi->ddc);
+       if (!!(config & SCDC_SCRAMBLING_ENABLE) == scrambling_needed)
+               return 0;
+
+       /*
+        * HDMI 2.0 says that one should not send scrambled data
+        * prior to configuring the sink scrambling, and that
+        * TMDS clock/data transmission should be suspended when
+        * changing the TMDS clock rate in the sink. So let's
+        * just do a full modeset here, even though some sinks
+        * would be perfectly happy if were to just reconfigure
+        * the SCDC settings on the fly.
+        */
+       return reset_pipe(crtc, ctx);
+}
 
-                       if (edid) {
-                               cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
-                               vc4_hdmi->encoder.hdmi_monitor = drm_detect_hdmi_monitor(edid);
-                               kfree(edid);
-                       }
-               }
+static void vc4_hdmi_handle_hotplug(struct vc4_hdmi *vc4_hdmi,
+                                   struct drm_modeset_acquire_ctx *ctx,
+                                   enum drm_connector_status status)
+{
+       struct drm_connector *connector = &vc4_hdmi->connector;
+       struct edid *edid;
+
+       /*
+        * NOTE: This function should really be called with
+        * vc4_hdmi->mutex held, but doing so results in reentrancy
+        * issues since cec_s_phys_addr_from_edid might call
+        * .adap_enable, which leads to that funtion being called with
+        * our mutex held.
+        *
+        * A similar situation occurs with
+        * drm_atomic_helper_connector_hdmi_reset_link() that will call
+        * into our KMS hooks if the scrambling was enabled.
+        *
+        * Concurrency isn't an issue at the moment since we don't share
+        * any state with any of the other frameworks so we can ignore
+        * the lock for now.
+        */
+
+       if (status == connector->status)
+               return;
 
-               vc4_hdmi_enable_scrambling(&vc4_hdmi->encoder.base.base);
-               pm_runtime_put(&vc4_hdmi->pdev->dev);
-               return connector_status_connected;
+       if (status == connector_status_disconnected) {
+               cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
+               return;
        }
 
-       cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
-       pm_runtime_put(&vc4_hdmi->pdev->dev);
-       return connector_status_disconnected;
+       edid = drm_get_edid(connector, vc4_hdmi->ddc);
+       if (!edid)
+               return;
+
+       cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
+       kfree(edid);
+
+       vc4_hdmi_reset_link(connector, ctx);
 }
 
-static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
+static int vc4_hdmi_connector_detect_ctx(struct drm_connector *connector,
+                                        struct drm_modeset_acquire_ctx *ctx,
+                                        bool force)
 {
-       drm_connector_unregister(connector);
-       drm_connector_cleanup(connector);
+       struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
+       enum drm_connector_status status = connector_status_disconnected;
+
+       /*
+        * NOTE: This function should really take vc4_hdmi->mutex, but
+        * doing so results in reentrancy issues since
+        * vc4_hdmi_handle_hotplug() can call into other functions that
+        * would take the mutex while it's held here.
+        *
+        * Concurrency isn't an issue at the moment since we don't share
+        * any state with any of the other frameworks so we can ignore
+        * the lock for now.
+        */
+
+       WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
+
+       if (force_hotplug & BIT(vc4_hdmi->encoder.type - VC4_ENCODER_TYPE_HDMI0))
+               status = connector_status_connected;
+       else if (vc4_hdmi->hpd_gpio) {
+               if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
+                       status = connector_status_connected;
+       } else {
+               if (vc4_hdmi->variant->hp_detect &&
+                   vc4_hdmi->variant->hp_detect(vc4_hdmi))
+                       status = connector_status_connected;
+       }
+
+       vc4_hdmi_handle_hotplug(vc4_hdmi, ctx, status);
+       pm_runtime_put(&vc4_hdmi->pdev->dev);
+
+#ifdef CONFIG_EXTCON
+       if (status != vc4_hdmi->status) {
+               extcon_set_state_sync(vc4_hdmi->edev, EXTCON_DISP_HDMI,
+                                     (status == connector_status_connected ?
+                                     true : false));
+               vc4_hdmi->status = status;
+       }
+#endif
+
+       return status;
 }
 
 static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
 {
        struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
-       struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
+       struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
        int ret = 0;
        struct edid *edid;
 
+       /*
+        * NOTE: This function should really take vc4_hdmi->mutex, but
+        * doing so results in reentrancy issues since
+        * cec_s_phys_addr_from_edid might call .adap_enable, which
+        * leads to that funtion being called with our mutex held.
+        *
+        * Concurrency isn't an issue at the moment since we don't share
+        * any state with any of the other frameworks so we can ignore
+        * the lock for now.
+        */
+
        edid = drm_get_edid(connector, vc4_hdmi->ddc);
        cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
        if (!edid)
                return -ENODEV;
 
-       vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
-
        drm_connector_update_edid_property(connector, edid);
        ret = drm_add_edid_modes(connector, edid);
        kfree(edid);
 
-       if (vc4_hdmi->disable_4kp60) {
+       if (!vc4->hvs->vc5_hdmi_enable_hdmi_20) {
                struct drm_device *drm = connector->dev;
-               struct drm_display_mode *mode;
+               const struct drm_display_mode *mode;
 
                list_for_each_entry(mode, &connector->probed_modes, head) {
-                       if (vc4_hdmi_mode_needs_scrambling(mode)) {
+                       if (vc4_hdmi_mode_needs_scrambling(mode, 8, VC4_HDMI_OUTPUT_RGB)) {
                                drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
                                drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
                        }
@@ -250,14 +537,17 @@ static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
 {
        struct drm_connector_state *old_state =
                drm_atomic_get_old_connector_state(state, connector);
+       struct vc4_hdmi_connector_state *old_vc4_state = conn_state_to_vc4_hdmi_conn_state(old_state);
        struct drm_connector_state *new_state =
                drm_atomic_get_new_connector_state(state, connector);
+       struct vc4_hdmi_connector_state *new_vc4_state = conn_state_to_vc4_hdmi_conn_state(new_state);
        struct drm_crtc *crtc = new_state->crtc;
 
        if (!crtc)
                return 0;
 
        if (old_state->colorspace != new_state->colorspace ||
+           old_vc4_state->broadcast_rgb != new_vc4_state->broadcast_rgb ||
            !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
                struct drm_crtc_state *crtc_state;
 
@@ -271,6 +561,65 @@ static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
        return 0;
 }
 
+/**
+ * vc4_hdmi_connector_atomic_get_property - hook for
+ *                                             connector->atomic_get_property.
+ * @connector: Connector to get the property for.
+ * @state: Connector state to retrieve the property from.
+ * @property: Property to retrieve.
+ * @val: Return value for the property.
+ *
+ * Returns the atomic property value for a digital connector.
+ */
+int vc4_hdmi_connector_get_property(struct drm_connector *connector,
+                                   const struct drm_connector_state *state,
+                                   struct drm_property *property,
+                                   uint64_t *val)
+{
+       struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
+       const struct vc4_hdmi_connector_state *vc4_conn_state =
+                               const_conn_state_to_vc4_hdmi_conn_state(state);
+
+       if (property == vc4_hdmi->broadcast_rgb_property) {
+               *val = vc4_conn_state->broadcast_rgb;
+       } else {
+               DRM_DEBUG_ATOMIC("Unknown property [PROP:%d:%s]\n",
+                                property->base.id, property->name);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+/**
+ * vc4_hdmi_connector_atomic_set_property - hook for
+ *                                             connector->atomic_set_property.
+ * @connector: Connector to set the property for.
+ * @state: Connector state to set the property on.
+ * @property: Property to set.
+ * @val: New value for the property.
+ *
+ * Sets the atomic property value for a digital connector.
+ */
+int vc4_hdmi_connector_set_property(struct drm_connector *connector,
+                                   struct drm_connector_state *state,
+                                   struct drm_property *property,
+                                   uint64_t val)
+{
+       struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
+       struct vc4_hdmi_connector_state *vc4_conn_state =
+                               conn_state_to_vc4_hdmi_conn_state(state);
+
+       if (property == vc4_hdmi->broadcast_rgb_property) {
+               vc4_conn_state->broadcast_rgb = val;
+               return 0;
+       }
+
+       DRM_DEBUG_ATOMIC("Unknown property [PROP:%d:%s]\n",
+                        property->base.id, property->name);
+       return -EINVAL;
+}
+
 static void vc4_hdmi_connector_reset(struct drm_connector *connector)
 {
        struct vc4_hdmi_connector_state *old_state =
@@ -289,6 +638,7 @@ static void vc4_hdmi_connector_reset(struct drm_connector *connector)
 
        new_state->base.max_bpc = 8;
        new_state->base.max_requested_bpc = 8;
+       new_state->output_format = VC4_HDMI_OUTPUT_RGB;
        drm_atomic_helper_connector_tv_reset(connector);
 }
 
@@ -304,36 +654,69 @@ vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
                return NULL;
 
        new_state->pixel_rate = vc4_state->pixel_rate;
+       new_state->output_bpc = vc4_state->output_bpc;
+       new_state->output_format = vc4_state->output_format;
+       new_state->broadcast_rgb = vc4_state->broadcast_rgb;
        __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
 
        return &new_state->base;
 }
 
 static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
-       .detect = vc4_hdmi_connector_detect,
        .fill_modes = drm_helper_probe_single_connector_modes,
-       .destroy = vc4_hdmi_connector_destroy,
        .reset = vc4_hdmi_connector_reset,
        .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
        .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
+       .atomic_get_property = vc4_hdmi_connector_get_property,
+       .atomic_set_property = vc4_hdmi_connector_set_property,
 };
 
 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
+       .detect_ctx = vc4_hdmi_connector_detect_ctx,
        .get_modes = vc4_hdmi_connector_get_modes,
        .atomic_check = vc4_hdmi_connector_atomic_check,
 };
 
+static const struct drm_prop_enum_list broadcast_rgb_names[] = {
+       { VC4_BROADCAST_RGB_AUTO, "Automatic" },
+       { VC4_BROADCAST_RGB_FULL, "Full" },
+       { VC4_BROADCAST_RGB_LIMITED, "Limited 16:235" },
+};
+
+static void
+vc4_hdmi_attach_broadcast_rgb_property(struct drm_device *dev,
+                                      struct vc4_hdmi *vc4_hdmi)
+{
+       struct drm_property *prop = vc4_hdmi->broadcast_rgb_property;
+
+       if (!prop) {
+               prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
+                                               "Broadcast RGB",
+                                               broadcast_rgb_names,
+                                               ARRAY_SIZE(broadcast_rgb_names));
+               if (!prop)
+                       return;
+
+               vc4_hdmi->broadcast_rgb_property = prop;
+       }
+
+       drm_object_attach_property(&vc4_hdmi->connector.base, prop, 0);
+}
+
 static int vc4_hdmi_connector_init(struct drm_device *dev,
                                   struct vc4_hdmi *vc4_hdmi)
 {
        struct drm_connector *connector = &vc4_hdmi->connector;
-       struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
+       struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
        int ret;
 
-       drm_connector_init_with_ddc(dev, connector,
-                                   &vc4_hdmi_connector_funcs,
-                                   DRM_MODE_CONNECTOR_HDMIA,
-                                   vc4_hdmi->ddc);
+       ret = drmm_connector_init(dev, connector,
+                                 &vc4_hdmi_connector_funcs,
+                                 DRM_MODE_CONNECTOR_HDMIA,
+                                 vc4_hdmi->ddc);
+       if (ret)
+               return ret;
+
        drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
 
        /*
@@ -361,10 +744,13 @@ static int vc4_hdmi_connector_init(struct drm_device *dev,
 
        connector->interlace_allowed = 1;
        connector->doublescan_allowed = 0;
+       connector->stereo_allowed = 1;
 
        if (vc4_hdmi->variant->supports_hdr)
                drm_connector_attach_hdr_output_metadata_property(connector);
 
+       vc4_hdmi_attach_broadcast_rgb_property(dev, vc4_hdmi);
+
        drm_connector_attach_encoder(connector, encoder);
 
        return 0;
@@ -375,31 +761,50 @@ static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
                                bool poll)
 {
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
        u32 packet_id = type - 0x80;
+       unsigned long flags;
+       int ret = 0;
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               return -ENODEV;
 
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
        HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
                   HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
 
-       if (!poll)
-               return 0;
+       if (poll) {
+               ret = wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
+                                BIT(packet_id)), 100);
+       }
 
-       return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
-                         BIT(packet_id)), 100);
+       drm_dev_exit(idx);
+       return ret;
 }
 
 static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
                                     union hdmi_infoframe *frame)
 {
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
        u32 packet_id = frame->any.type - 0x80;
        const struct vc4_hdmi_register *ram_packet_start =
                &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
        u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
+       u32 packet_reg_next = ram_packet_start->offset +
+               VC4_HDMI_PACKET_STRIDE * (packet_id + 1);
        void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
                                                       ram_packet_start->reg);
-       uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
+       uint8_t buffer[VC4_HDMI_PACKET_STRIDE] = {};
+       unsigned long flags;
        ssize_t len, i;
        int ret;
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               return;
 
        WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
                    VC4_HDMI_RAM_PACKET_ENABLE),
@@ -407,14 +812,16 @@ static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
 
        len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
        if (len < 0)
-               return;
+               goto out;
 
        ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
        if (ret) {
                DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
-               return;
+               goto out;
        }
 
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
        for (i = 0; i < len; i += 7) {
                writel(buffer[i + 0] << 0 |
                       buffer[i + 1] << 8 |
@@ -430,25 +837,65 @@ static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
                packet_reg += 4;
        }
 
+       /*
+        * clear remainder of packet ram as it's included in the
+        * infoframe and triggers a checksum error on hdmi analyser
+        */
+       for (; packet_reg < packet_reg_next; packet_reg += 4)
+               writel(0, base + packet_reg);
+
        HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
                   HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
        ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
                        BIT(packet_id)), 100);
        if (ret)
                DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
+
+out:
+       drm_dev_exit(idx);
+}
+
+static void vc4_hdmi_avi_infoframe_colorspace(struct hdmi_avi_infoframe *frame,
+                                             enum vc4_hdmi_output_format fmt)
+{
+       switch (fmt) {
+       case VC4_HDMI_OUTPUT_RGB:
+               frame->colorspace = HDMI_COLORSPACE_RGB;
+               break;
+
+       case VC4_HDMI_OUTPUT_YUV420:
+               frame->colorspace = HDMI_COLORSPACE_YUV420;
+               break;
+
+       case VC4_HDMI_OUTPUT_YUV422:
+               frame->colorspace = HDMI_COLORSPACE_YUV422;
+               break;
+
+       case VC4_HDMI_OUTPUT_YUV444:
+               frame->colorspace = HDMI_COLORSPACE_YUV444;
+               break;
+
+       default:
+               break;
+       }
 }
 
 static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
 {
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
-       struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
        struct drm_connector *connector = &vc4_hdmi->connector;
        struct drm_connector_state *cstate = connector->state;
-       struct drm_crtc *crtc = encoder->crtc;
-       const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
+       struct vc4_hdmi_connector_state *vc4_state =
+               conn_state_to_vc4_hdmi_conn_state(cstate);
+       const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
        union hdmi_infoframe frame;
        int ret;
 
+       lockdep_assert_held(&vc4_hdmi->mutex);
+
        ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
                                                       connector, mode);
        if (ret < 0) {
@@ -458,10 +905,11 @@ static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
 
        drm_hdmi_avi_infoframe_quant_range(&frame.avi,
                                           connector, mode,
-                                          vc4_encoder->limited_rgb_range ?
-                                          HDMI_QUANTIZATION_RANGE_LIMITED :
-                                          HDMI_QUANTIZATION_RANGE_FULL);
-       drm_hdmi_avi_infoframe_colorspace(&frame.avi, cstate);
+                                          vc4_hdmi_is_full_range(vc4_hdmi, mode) ?
+                                          HDMI_QUANTIZATION_RANGE_FULL :
+                                          HDMI_QUANTIZATION_RANGE_LIMITED);
+       drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
+       vc4_hdmi_avi_infoframe_colorspace(&frame.avi, vc4_state->output_format);
        drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
 
        vc4_hdmi_write_infoframe(encoder, &frame);
@@ -500,6 +948,8 @@ static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
        struct drm_connector_state *conn_state = connector->state;
        union hdmi_infoframe frame;
 
+       lockdep_assert_held(&vc4_hdmi->mutex);
+
        if (!vc4_hdmi->variant->supports_hdr)
                return;
 
@@ -516,6 +966,8 @@ static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
 {
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
 
+       lockdep_assert_held(&vc4_hdmi->mutex);
+
        vc4_hdmi_set_avi_infoframe(encoder);
        vc4_hdmi_set_spd_infoframe(encoder);
        /*
@@ -528,41 +980,40 @@ static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
        vc4_hdmi_set_hdr_infoframe(encoder);
 }
 
-static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder,
-                                        struct drm_display_mode *mode)
-{
-       struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
-       struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
-       struct drm_display_info *display = &vc4_hdmi->connector.display_info;
-
-       if (!vc4_encoder->hdmi_monitor)
-               return false;
-
-       if (!display->hdmi.scdc.supported ||
-           !display->hdmi.scdc.scrambling.supported)
-               return false;
-
-       return true;
-}
-
 #define SCRAMBLING_POLLING_DELAY_MS    1000
 
 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
 {
-       struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
+       unsigned long flags;
+       int idx;
 
-       if (!vc4_hdmi_supports_scrambling(encoder, mode))
+       lockdep_assert_held(&vc4_hdmi->mutex);
+
+       if (!vc4_hdmi_supports_scrambling(encoder))
+               return;
+
+       if (!vc4_hdmi_mode_needs_scrambling(mode,
+                                           vc4_hdmi->output_bpc,
+                                           vc4_hdmi->output_format))
                return;
 
-       if (!vc4_hdmi_mode_needs_scrambling(mode))
+       if (!drm_dev_enter(drm, &idx))
                return;
 
        drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
        drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
 
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
        HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
                   VC5_HDMI_SCRAMBLER_CTL_ENABLE);
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       drm_dev_exit(idx);
+
+       vc4_hdmi->scdc_enabled = true;
 
        queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
                           msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
@@ -571,27 +1022,32 @@ static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
 static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
 {
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
-       struct drm_crtc *crtc = encoder->crtc;
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
+       int idx;
 
-       /*
-        * At boot, encoder->crtc will be NULL. Since we don't know the
-        * state of the scrambler and in order to avoid any
-        * inconsistency, let's disable it all the time.
-        */
-       if (crtc && !vc4_hdmi_supports_scrambling(encoder, &crtc->mode))
-               return;
+       lockdep_assert_held(&vc4_hdmi->mutex);
 
-       if (crtc && !vc4_hdmi_mode_needs_scrambling(&crtc->mode))
+       if (!vc4_hdmi->scdc_enabled)
                return;
 
+       vc4_hdmi->scdc_enabled = false;
+
        if (delayed_work_pending(&vc4_hdmi->scrambling_work))
                cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
 
+       if (!drm_dev_enter(drm, &idx))
+               return;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
        HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
                   ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
 
        drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
        drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
+
+       drm_dev_exit(idx);
 }
 
 static void vc4_hdmi_scrambling_wq(struct work_struct *work)
@@ -614,26 +1070,58 @@ static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
                                               struct drm_atomic_state *state)
 {
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
+       int idx;
+
+       mutex_lock(&vc4_hdmi->mutex);
+
+       vc4_hdmi->output_enabled = false;
+
+       if (!drm_dev_enter(drm, &idx))
+               goto out;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
 
        HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
 
        HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
 
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
        mdelay(1);
 
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
        HDMI_WRITE(HDMI_VID_CTL,
                   HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
        vc4_hdmi_disable_scrambling(encoder);
+
+       drm_dev_exit(idx);
+
+out:
+       mutex_unlock(&vc4_hdmi->mutex);
 }
 
 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
                                                 struct drm_atomic_state *state)
 {
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
        int ret;
+       int idx;
 
+       mutex_lock(&vc4_hdmi->mutex);
+
+       if (!drm_dev_enter(drm, &idx))
+               goto out;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
        HDMI_WRITE(HDMI_VID_CTL,
                   HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
 
        if (vc4_hdmi->variant->phy_disable)
                vc4_hdmi->variant->phy_disable(vc4_hdmi);
@@ -644,20 +1132,31 @@ static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
        ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
        if (ret < 0)
                DRM_ERROR("Failed to release power domain: %d\n", ret);
-}
 
-static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
-{
+       drm_dev_exit(idx);
+
+out:
+       mutex_unlock(&vc4_hdmi->mutex);
 }
 
-static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
+static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
+                              struct drm_connector_state *state,
+                              const struct drm_display_mode *mode)
 {
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
        u32 csc_ctl;
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               return;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
 
        csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
                                VC4_HD_CSC_CTL_ORDER);
 
-       if (enable) {
+       if (!vc4_hdmi_is_full_range(vc4_hdmi, mode)) {
                /* CEA VICs other than #1 requre limited range RGB
                 * output unless overridden by an AVI infoframe.
                 * Apply a colorspace conversion to squash 0-255 down
@@ -683,51 +1182,252 @@ static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
 
        /* The RGB order applies even when CSC is disabled. */
        HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       drm_dev_exit(idx);
 }
 
-static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
+/*
+ * If we need to output Full Range RGB, then use the unity matrix
+ *
+ * [ 1      0      0      0]
+ * [ 0      1      0      0]
+ * [ 0      0      1      0]
+ *
+ * CEA VICs other than #1 require limited range RGB output unless
+ * overridden by an AVI infoframe. Apply a colorspace conversion to
+ * squash 0-255 down to 16-235. The matrix here is:
+ *
+ * [ 0.8594 0      0      16]
+ * [ 0      0.8594 0      16]
+ * [ 0      0      0.8594 16]
+ *
+ * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
+ */
+static const u16 vc5_hdmi_csc_full_rgb_to_rgb[2][3][4] = {
+       {
+               /* Full range - unity */
+               { 0x2000, 0x0000, 0x0000, 0x0000 },
+               { 0x0000, 0x2000, 0x0000, 0x0000 },
+               { 0x0000, 0x0000, 0x2000, 0x0000 },
+       }, {
+               /* Limited range */
+               { 0x1b80, 0x0000, 0x0000, 0x0400 },
+               { 0x0000, 0x1b80, 0x0000, 0x0400 },
+               { 0x0000, 0x0000, 0x1b80, 0x0400 },
+       }
+};
+
+/*
+ * Conversion between Full Range RGB and YUV using the BT.601 Colorspace
+ *
+ * Full range
+ * [    0.299000   0.587000   0.114000   0.000000 ]
+ * [   -0.168736  -0.331264   0.500000 128.000000 ]
+ * [    0.500000  -0.418688  -0.081312 128.000000 ]
+ *
+ * Limited range
+ * [    0.255785   0.502160   0.097523  16.000000 ]
+ * [   -0.147644  -0.289856   0.437500 128.000000 ]
+ * [    0.437500  -0.366352  -0.071148 128.000000 ]
+ *
+ * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
+ */
+static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt601[2][3][4] = {
+       {
+               /* Full range */
+               { 0x0991, 0x12c9, 0x03a6, 0x0000 },
+               { 0xfa9b, 0xf567, 0x1000, 0x2000 },
+               { 0x1000, 0xf29b, 0xfd67, 0x2000 },
+       }, {
+               /* Limited range */
+               { 0x082f, 0x1012, 0x031f, 0x0400 },
+               { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
+               { 0x0e00, 0xf448, 0xfdba, 0x2000 },
+       }
+};
+
+/*
+ * Conversion between Full Range RGB and YUV using the BT.709 Colorspace
+ *
+ * Full range
+ * [    0.212600   0.715200   0.072200   0.000000 ]
+ * [   -0.114572  -0.385428   0.500000 128.000000 ]
+ * [    0.500000  -0.454153  -0.045847 128.000000 ]
+ *
+ * Limited range
+ * [    0.181873   0.611831   0.061765  16.000000 ]
+ * [   -0.100251  -0.337249   0.437500 128.000000 ]
+ * [    0.437500  -0.397384  -0.040116 128.000000 ]
+ *
+ * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
+ */
+static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt709[2][3][4] = {
+       {
+               /* Full range */
+               { 0x06ce, 0x16e3, 0x024f, 0x0000 },
+               { 0xfc56, 0xf3ac, 0x1000, 0x2000 },
+               { 0x1000, 0xf179, 0xfe89, 0x2000 },
+       }, {
+               /* Limited range        */
+               { 0x05d2, 0x1394, 0x01fa, 0x0400 },
+               { 0xfccc, 0xf536, 0x0e00, 0x2000 },
+               { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
+       }
+};
+
+/*
+ * Conversion between Full Range RGB and YUV using the BT.2020 Colorspace
+ *
+ * Full range
+ * [    0.262700   0.678000   0.059300   0.000000 ]
+ * [   -0.139630  -0.360370   0.500000 128.000000 ]
+ * [    0.500000  -0.459786  -0.040214 128.000000 ]
+ *
+ * Limited range
+ * [    0.224732   0.580008   0.050729  16.000000 ]
+ * [   -0.122176  -0.315324   0.437500 128.000000 ]
+ * [    0.437500  -0.402312  -0.035188 128.000000 ]
+ *
+ * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
+ */
+static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt2020[2][3][4] = {
+       {
+               /* Full range */
+               { 0x0868, 0x15b2, 0x01e6, 0x0000 },
+               { 0xfb89, 0xf479, 0x1000, 0x2000 },
+               { 0x1000, 0xf14a, 0xfeb8, 0x2000 },
+       }, {
+               /* Limited range */
+               { 0x0731, 0x128f, 0x01a0, 0x0400 },
+               { 0xfc18, 0xf5ea, 0x0e00, 0x2000 },
+               { 0x0e00, 0xf321, 0xfee1, 0x2000 },
+       }
+};
+
+static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
+                                   const u16 coeffs[3][4])
 {
-       u32 csc_ctl;
+       lockdep_assert_held(&vc4_hdmi->hw_lock);
 
-       csc_ctl = 0x07; /* RGB_CONVERT_MODE = custom matrix, || USE_RGB_TO_YCBCR */
+       HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
+       HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
+       HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
+       HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
+       HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
+       HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
+}
 
-       if (enable) {
-               /* CEA VICs other than #1 requre limited range RGB
-                * output unless overridden by an AVI infoframe.
-                * Apply a colorspace conversion to squash 0-255 down
-                * to 16-235.  The matrix here is:
-                *
-                * [ 0.8594 0      0      16]
-                * [ 0      0.8594 0      16]
-                * [ 0      0      0.8594 16]
-                * [ 0      0      0       1]
-                * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
-                */
-               HDMI_WRITE(HDMI_CSC_12_11, (0x0000 << 16) | 0x1b80);
-               HDMI_WRITE(HDMI_CSC_14_13, (0x0400 << 16) | 0x0000);
-               HDMI_WRITE(HDMI_CSC_22_21, (0x1b80 << 16) | 0x0000);
-               HDMI_WRITE(HDMI_CSC_24_23, (0x0400 << 16) | 0x0000);
-               HDMI_WRITE(HDMI_CSC_32_31, (0x0000 << 16) | 0x0000);
-               HDMI_WRITE(HDMI_CSC_34_33, (0x0400 << 16) | 0x1b80);
-       } else {
-               /* Still use the matrix for full range, but make it unity.
-                * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
-                */
-               HDMI_WRITE(HDMI_CSC_12_11, (0x0000 << 16) | 0x2000);
-               HDMI_WRITE(HDMI_CSC_14_13, (0x0000 << 16) | 0x0000);
-               HDMI_WRITE(HDMI_CSC_22_21, (0x2000 << 16) | 0x0000);
-               HDMI_WRITE(HDMI_CSC_24_23, (0x0000 << 16) | 0x0000);
-               HDMI_WRITE(HDMI_CSC_32_31, (0x0000 << 16) | 0x0000);
-               HDMI_WRITE(HDMI_CSC_34_33, (0x0000 << 16) | 0x2000);
+static void vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi *vc4_hdmi,
+                                        const u16 coeffs[3][4])
+{
+       lockdep_assert_held(&vc4_hdmi->hw_lock);
+
+       /* YUV444 needs the CSC matrices using the channels in a different order */
+       HDMI_WRITE(HDMI_CSC_12_11, (coeffs[2][1] << 16) | coeffs[2][0]);
+       HDMI_WRITE(HDMI_CSC_14_13, (coeffs[2][3] << 16) | coeffs[2][2]);
+       HDMI_WRITE(HDMI_CSC_22_21, (coeffs[0][1] << 16) | coeffs[0][0]);
+       HDMI_WRITE(HDMI_CSC_24_23, (coeffs[0][3] << 16) | coeffs[0][2]);
+       HDMI_WRITE(HDMI_CSC_32_31, (coeffs[1][1] << 16) | coeffs[1][0]);
+       HDMI_WRITE(HDMI_CSC_34_33, (coeffs[1][3] << 16) | coeffs[1][2]);
+}
+
+static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
+                              struct drm_connector_state *state,
+                              const struct drm_display_mode *mode)
+{
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       struct vc4_hdmi_connector_state *vc4_state =
+               conn_state_to_vc4_hdmi_conn_state(state);
+       unsigned int lim_range = vc4_hdmi_is_full_range(vc4_hdmi, mode) ? 0 : 1;
+       const u16 (*csc)[4];
+       unsigned long flags;
+       u32 if_cfg = 0;
+       u32 if_xbar = 0x543210;
+       u32 csc_chan_ctl = 0;
+       u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
+                                                              VC5_MT_CP_CSC_CTL_MODE);
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               return;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
+       switch (vc4_state->output_format) {
+       case VC4_HDMI_OUTPUT_YUV444:
+       case VC4_HDMI_OUTPUT_YUV422:
+               switch (state->colorspace) {
+               default:
+               case DRM_MODE_COLORIMETRY_NO_DATA:
+               case DRM_MODE_COLORIMETRY_BT709_YCC:
+               case DRM_MODE_COLORIMETRY_XVYCC_709:
+               case DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED:
+               case DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT:
+                       csc = vc5_hdmi_csc_full_rgb_to_yuv_bt709[lim_range];
+                       break;
+               case DRM_MODE_COLORIMETRY_SMPTE_170M_YCC:
+               case DRM_MODE_COLORIMETRY_XVYCC_601:
+               case DRM_MODE_COLORIMETRY_SYCC_601:
+               case DRM_MODE_COLORIMETRY_OPYCC_601:
+               case DRM_MODE_COLORIMETRY_BT601_YCC:
+                       csc = vc5_hdmi_csc_full_rgb_to_yuv_bt601[lim_range];
+                       break;
+               case DRM_MODE_COLORIMETRY_BT2020_CYCC:
+               case DRM_MODE_COLORIMETRY_BT2020_YCC:
+               case DRM_MODE_COLORIMETRY_BT2020_RGB:
+               case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
+               case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
+                       csc = vc5_hdmi_csc_full_rgb_to_yuv_bt2020[lim_range];
+                       break;
+               }
+
+               if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
+                       csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
+                                                VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
+                               VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
+                               VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
+
+                       csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
+                                                     VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
+
+                       if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
+                                               VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);
+
+                       vc5_hdmi_set_csc_coeffs(vc4_hdmi, csc);
+               } else {
+                       vc5_hdmi_set_csc_coeffs_swap(vc4_hdmi, csc);
+               }
+
+               break;
+
+       case VC4_HDMI_OUTPUT_RGB:
+               if_xbar = 0x354021;
+
+               vc5_hdmi_set_csc_coeffs(vc4_hdmi,
+                                       vc5_hdmi_csc_full_rgb_to_rgb[lim_range]);
+               break;
+
+       default:
+               break;
        }
 
+       HDMI_WRITE(HDMI_VEC_INTERFACE_CFG, if_cfg);
+       HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, if_xbar);
+       HDMI_WRITE(HDMI_CSC_CHANNEL_CTL, csc_chan_ctl);
        HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       drm_dev_exit(idx);
 }
 
 static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
                                 struct drm_connector_state *state,
-                                struct drm_display_mode *mode)
+                                const struct drm_display_mode *mode)
 {
+       struct drm_device *drm = vc4_hdmi->connector.dev;
        bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
        bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
        bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
@@ -745,6 +1445,14 @@ static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
                          VC4_SET_FIELD(mode->crtc_vtotal -
                                        mode->crtc_vsync_end,
                                        VC4_HDMI_VERTB_VBP));
+       unsigned long flags;
+       u32 reg;
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               return;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
 
        HDMI_WRITE(HDMI_HORZA,
                   (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
@@ -768,12 +1476,24 @@ static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
 
        HDMI_WRITE(HDMI_VERTB0, vertb_even);
        HDMI_WRITE(HDMI_VERTB1, vertb);
+
+       reg = HDMI_READ(HDMI_MISC_CONTROL);
+       reg &= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
+       reg |= VC4_SET_FIELD(pixel_rep - 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP);
+       HDMI_WRITE(HDMI_MISC_CONTROL, reg);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       drm_dev_exit(idx);
 }
 
 static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
                                 struct drm_connector_state *state,
-                                struct drm_display_mode *mode)
+                                const struct drm_display_mode *mode)
 {
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       const struct vc4_hdmi_connector_state *vc4_state =
+               conn_state_to_vc4_hdmi_conn_state(state);
        bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
        bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
        bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
@@ -785,17 +1505,24 @@ static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
                     VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
        u32 vertb = (VC4_SET_FIELD(mode->htotal >> (2 - pixel_rep),
                                   VC5_HDMI_VERTB_VSPO) |
-                    VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
+                    VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
+                                  interlaced,
                                   VC4_HDMI_VERTB_VBP));
        u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
                          VC4_SET_FIELD(mode->crtc_vtotal -
-                                       mode->crtc_vsync_end - interlaced,
+                                       mode->crtc_vsync_end,
                                        VC4_HDMI_VERTB_VBP));
+       unsigned long flags;
        unsigned char gcp;
        bool gcp_en;
        u32 reg;
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               return;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
 
-       HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, 0x354021);
        HDMI_WRITE(HDMI_HORZA,
                   (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
                   (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
@@ -819,7 +1546,7 @@ static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
        HDMI_WRITE(HDMI_VERTB0, vertb_even);
        HDMI_WRITE(HDMI_VERTB1, vertb);
 
-       switch (state->max_bpc) {
+       switch (vc4_state->output_bpc) {
        case 12:
                gcp = 6;
                gcp_en = true;
@@ -835,6 +1562,15 @@ static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
                break;
        }
 
+       /*
+        * YCC422 is always 36-bit and not considered deep colour so
+        * doesn't signal in GCP
+        */
+       if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
+               gcp = 4;
+               gcp_en = false;
+       }
+
        reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
        reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
                 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
@@ -854,16 +1590,28 @@ static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
 
        reg = HDMI_READ(HDMI_MISC_CONTROL);
        reg &= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
-       reg |= VC4_SET_FIELD(0, VC5_HDMI_MISC_CONTROL_PIXEL_REP);
+       reg |= VC4_SET_FIELD(pixel_rep - 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP);
        HDMI_WRITE(HDMI_MISC_CONTROL, reg);
 
        HDMI_WRITE(HDMI_CLOCK_STOP, 0);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       drm_dev_exit(idx);
 }
 
 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
 {
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
        u32 drift;
        int ret;
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               return;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
 
        drift = HDMI_READ(HDMI_FIFO_CTL);
        drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
@@ -872,46 +1620,49 @@ static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
                   drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
        HDMI_WRITE(HDMI_FIFO_CTL,
                   drift | VC4_HDMI_FIFO_CTL_RECENTER);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
        usleep_range(1000, 1100);
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
        HDMI_WRITE(HDMI_FIFO_CTL,
                   drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
        HDMI_WRITE(HDMI_FIFO_CTL,
                   drift | VC4_HDMI_FIFO_CTL_RECENTER);
 
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
        ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
                       VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
        WARN_ONCE(ret, "Timeout waiting for "
                  "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
-}
-
-static struct drm_connector_state *
-vc4_hdmi_encoder_get_connector_state(struct drm_encoder *encoder,
-                                    struct drm_atomic_state *state)
-{
-       struct drm_connector_state *conn_state;
-       struct drm_connector *connector;
-       unsigned int i;
-
-       for_each_new_connector_in_state(state, connector, conn_state, i) {
-               if (conn_state->best_encoder == encoder)
-                       return conn_state;
-       }
 
-       return NULL;
+       drm_dev_exit(idx);
 }
 
 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
                                                struct drm_atomic_state *state)
 {
+       struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       struct drm_connector *connector = &vc4_hdmi->connector;
        struct drm_connector_state *conn_state =
-               vc4_hdmi_encoder_get_connector_state(encoder, state);
+               drm_atomic_get_new_connector_state(state, connector);
        struct vc4_hdmi_connector_state *vc4_conn_state =
                conn_state_to_vc4_hdmi_conn_state(conn_state);
-       struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
-       struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
        unsigned long pixel_rate = vc4_conn_state->pixel_rate;
        unsigned long bvb_rate, hsm_rate;
+       unsigned long flags;
        int ret;
+       int idx;
+
+       mutex_lock(&vc4_hdmi->mutex);
+
+       if (!drm_dev_enter(drm, &idx))
+               goto out;
 
        /*
         * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
@@ -933,13 +1684,13 @@ static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
        ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
        if (ret) {
                DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
-               return;
+               goto err_dev_exit;
        }
 
        ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
        if (ret < 0) {
                DRM_ERROR("Failed to retain power domain: %d\n", ret);
-               return;
+               goto err_dev_exit;
        }
 
        ret = clk_set_rate(vc4_hdmi->pixel_clock, pixel_rate);
@@ -979,56 +1730,83 @@ static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
        if (vc4_hdmi->variant->phy_init)
                vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
 
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
        HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
                   HDMI_READ(HDMI_SCHEDULER_CONTROL) |
                   VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
                   VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
 
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
        if (vc4_hdmi->variant->set_timings)
                vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
 
+       drm_dev_exit(idx);
+
+       mutex_unlock(&vc4_hdmi->mutex);
+
        return;
 
 err_disable_pixel_clock:
        clk_disable_unprepare(vc4_hdmi->pixel_clock);
 err_put_runtime_pm:
        pm_runtime_put(&vc4_hdmi->pdev->dev);
-
+err_dev_exit:
+       drm_dev_exit(idx);
+out:
+       mutex_unlock(&vc4_hdmi->mutex);
        return;
 }
 
 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
                                             struct drm_atomic_state *state)
 {
-       struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
-       struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       struct drm_connector *connector = &vc4_hdmi->connector;
+       const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
+       struct drm_connector_state *conn_state =
+               drm_atomic_get_new_connector_state(state, connector);
+       unsigned long flags;
+       int idx;
 
-       if (vc4_encoder->hdmi_monitor &&
-           drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED) {
-               if (vc4_hdmi->variant->csc_setup)
-                       vc4_hdmi->variant->csc_setup(vc4_hdmi, true);
+       mutex_lock(&vc4_hdmi->mutex);
 
-               vc4_encoder->limited_rgb_range = true;
-       } else {
-               if (vc4_hdmi->variant->csc_setup)
-                       vc4_hdmi->variant->csc_setup(vc4_hdmi, false);
+       if (!drm_dev_enter(drm, &idx))
+               return;
 
-               vc4_encoder->limited_rgb_range = false;
-       }
+       if (vc4_hdmi->variant->csc_setup)
+               vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
 
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
        HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       drm_dev_exit(idx);
+
+       mutex_unlock(&vc4_hdmi->mutex);
 }
 
 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
                                              struct drm_atomic_state *state)
 {
-       struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
-       struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
+       struct drm_display_info *display = &vc4_hdmi->connector.display_info;
        bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
        bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
+       unsigned long flags;
        int ret;
+       int idx;
+
+       mutex_lock(&vc4_hdmi->mutex);
+
+       if (!drm_dev_enter(drm, &idx))
+               return;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
 
        HDMI_WRITE(HDMI_VID_CTL,
                   VC4_HD_VID_CTL_ENABLE |
@@ -1041,11 +1819,13 @@ static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
        HDMI_WRITE(HDMI_VID_CTL,
                   HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
 
-       if (vc4_encoder->hdmi_monitor) {
+       if (display->is_hdmi) {
                HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
                           HDMI_READ(HDMI_SCHEDULER_CONTROL) |
                           VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
 
+               spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
                ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
                               VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
                WARN_ONCE(ret, "Timeout waiting for "
@@ -1058,31 +1838,271 @@ static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
                           HDMI_READ(HDMI_SCHEDULER_CONTROL) &
                           ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
 
+               spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
                ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
                                 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
                WARN_ONCE(ret, "Timeout waiting for "
                          "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
        }
 
-       if (vc4_encoder->hdmi_monitor) {
+       if (display->is_hdmi) {
+               spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
                WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
                          VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
-               HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
-                          HDMI_READ(HDMI_SCHEDULER_CONTROL) |
-                          VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
 
                HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
                           VC4_HDMI_RAM_PACKET_ENABLE);
 
+               spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+               vc4_hdmi->output_enabled = true;
+
                vc4_hdmi_set_infoframes(encoder);
        }
 
        vc4_hdmi_recenter_fifo(vc4_hdmi);
        vc4_hdmi_enable_scrambling(encoder);
+
+       drm_dev_exit(idx);
+       mutex_unlock(&vc4_hdmi->mutex);
+}
+
+static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
+                                            struct drm_crtc_state *crtc_state,
+                                            struct drm_connector_state *conn_state)
+{
+       struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       struct vc4_hdmi_connector_state *vc4_state =
+               conn_state_to_vc4_hdmi_conn_state(conn_state);
+
+       mutex_lock(&vc4_hdmi->mutex);
+       vc4_hdmi->output_bpc = vc4_state->output_bpc;
+       vc4_hdmi->output_format = vc4_state->output_format;
+       vc4_hdmi->broadcast_rgb = vc4_state->broadcast_rgb;
+       memcpy(&vc4_hdmi->saved_adjusted_mode,
+              &crtc_state->adjusted_mode,
+              sizeof(vc4_hdmi->saved_adjusted_mode));
+       mutex_unlock(&vc4_hdmi->mutex);
+}
+
+static bool
+vc4_hdmi_sink_supports_format_bpc(const struct vc4_hdmi *vc4_hdmi,
+                                 const struct drm_display_info *info,
+                                 const struct drm_display_mode *mode,
+                                 unsigned int format, unsigned int bpc)
+{
+       struct drm_device *dev = vc4_hdmi->connector.dev;
+       u8 vic = drm_match_cea_mode(mode);
+
+       if (vic == 1 && bpc != 8) {
+               drm_dbg(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
+               return false;
+       }
+
+       if (!info->is_hdmi &&
+           (format != VC4_HDMI_OUTPUT_RGB || bpc != 8)) {
+               drm_dbg(dev, "DVI Monitors require an RGB output at 8 bpc\n");
+               return false;
+       }
+
+       switch (format) {
+       case VC4_HDMI_OUTPUT_RGB:
+               drm_dbg(dev, "RGB Format, checking the constraints.\n");
+
+               if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
+                       drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
+                       return false;
+               }
+
+               if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
+                       drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
+                       return false;
+               }
+
+               drm_dbg(dev, "RGB format supported in that configuration.\n");
+
+               return true;
+
+       case VC4_HDMI_OUTPUT_YUV422:
+               drm_dbg(dev, "YUV422 format, checking the constraints.\n");
+
+               if (!(info->color_formats & DRM_COLOR_FORMAT_YCRCB422)) {
+                       drm_dbg(dev, "Sink doesn't support YUV422.\n");
+                       return false;
+               }
+
+               if (bpc != 12) {
+                       drm_dbg(dev, "YUV422 only supports 12 bpc.\n");
+                       return false;
+               }
+
+               drm_dbg(dev, "YUV422 format supported in that configuration.\n");
+
+               return true;
+
+       case VC4_HDMI_OUTPUT_YUV444:
+               drm_dbg(dev, "YUV444 format, checking the constraints.\n");
+
+               if (!(info->color_formats & DRM_COLOR_FORMAT_YCRCB444)) {
+                       drm_dbg(dev, "Sink doesn't support YUV444.\n");
+                       return false;
+               }
+
+               if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
+                       drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
+                       return false;
+               }
+
+               if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
+                       drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
+                       return false;
+               }
+
+               drm_dbg(dev, "YUV444 format supported in that configuration.\n");
+
+               return true;
+       }
+
+       return false;
+}
+
+static enum drm_mode_status
+vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi *vc4_hdmi,
+                            const struct drm_display_mode *mode,
+                            unsigned long long clock)
+{
+       const struct drm_connector *connector = &vc4_hdmi->connector;
+       const struct drm_display_info *info = &connector->display_info;
+       struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
+
+       if (clock > vc4_hdmi->variant->max_pixel_clock)
+               return MODE_CLOCK_HIGH;
+
+       if (!vc4->hvs->vc5_hdmi_enable_hdmi_20 && clock > HDMI_14_MAX_TMDS_CLK)
+               return MODE_CLOCK_HIGH;
+
+       /* 4096x2160@60 is not reliable without overclocking core */
+       if (!vc4->hvs->vc5_hdmi_enable_4096by2160 &&
+           mode->hdisplay > 3840 && mode->vdisplay >= 2160 &&
+           drm_mode_vrefresh(mode) >= 50)
+               return MODE_CLOCK_HIGH;
+
+       if (info->max_tmds_clock && clock > (info->max_tmds_clock * 1000))
+               return MODE_CLOCK_HIGH;
+
+       return MODE_OK;
+}
+
+static unsigned long long
+vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
+                                   unsigned int bpc,
+                                   enum vc4_hdmi_output_format fmt)
+{
+       unsigned long long clock = mode->clock * 1000;
+
+       if (mode->flags & DRM_MODE_FLAG_DBLCLK)
+               clock = clock * 2;
+
+       if (fmt == VC4_HDMI_OUTPUT_YUV422)
+               bpc = 8;
+
+       return clock * bpc / 8;
+}
+
+static int
+vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi *vc4_hdmi,
+                              struct vc4_hdmi_connector_state *vc4_state,
+                              const struct drm_display_mode *mode,
+                              unsigned int bpc, unsigned int fmt)
+{
+       unsigned long long clock;
+
+       clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
+       if (vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode, clock) != MODE_OK)
+               return -EINVAL;
+
+       vc4_state->pixel_rate = clock;
+
+       return 0;
+}
+
+static int
+vc4_hdmi_encoder_compute_format(const struct vc4_hdmi *vc4_hdmi,
+                               struct vc4_hdmi_connector_state *vc4_state,
+                               const struct drm_display_mode *mode,
+                               unsigned int bpc)
+{
+       struct drm_device *dev = vc4_hdmi->connector.dev;
+       const struct drm_connector *connector = &vc4_hdmi->connector;
+       const struct drm_display_info *info = &connector->display_info;
+       unsigned int format;
+
+       drm_dbg(dev, "Trying with an RGB output\n");
+
+       format = VC4_HDMI_OUTPUT_RGB;
+       if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
+               int ret;
+
+               ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
+                                                    mode, bpc, format);
+               if (!ret) {
+                       vc4_state->output_format = format;
+                       return 0;
+               }
+       }
+
+       drm_dbg(dev, "Failed, Trying with an YUV422 output\n");
+
+       format = VC4_HDMI_OUTPUT_YUV422;
+       if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
+               int ret;
+
+               ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
+                                                    mode, bpc, format);
+               if (!ret) {
+                       vc4_state->output_format = format;
+                       return 0;
+               }
+       }
+
+       drm_dbg(dev, "Failed. No Format Supported for that bpc count.\n");
+
+       return -EINVAL;
 }
 
-static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
+static int
+vc4_hdmi_encoder_compute_config(const struct vc4_hdmi *vc4_hdmi,
+                               struct vc4_hdmi_connector_state *vc4_state,
+                               const struct drm_display_mode *mode)
 {
+       struct drm_device *dev = vc4_hdmi->connector.dev;
+       struct drm_connector_state *conn_state = &vc4_state->base;
+       unsigned int max_bpc = clamp_t(unsigned int, conn_state->max_bpc, 8, 12);
+       unsigned int bpc;
+       int ret;
+
+       for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
+               drm_dbg(dev, "Trying with a %d bpc output\n", bpc);
+
+               ret = vc4_hdmi_encoder_compute_format(vc4_hdmi, vc4_state,
+                                                     mode, bpc);
+               if (ret)
+                       continue;
+
+               vc4_state->output_bpc = bpc;
+
+               drm_dbg(dev,
+                       "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
+                       mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
+                       vc4_state->output_bpc,
+                       vc4_hdmi_output_fmt_str(vc4_state->output_format),
+                       vc4_state->pixel_rate);
+
+               break;
+       }
+
+       return ret;
 }
 
 #define WIFI_2_4GHz_CH1_MIN_FREQ       2400000000ULL
@@ -1095,14 +2115,32 @@ static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
        struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
        struct drm_display_mode *mode = &crtc_state->adjusted_mode;
        struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       struct drm_connector *connector = &vc4_hdmi->connector;
+       struct drm_connector_state *old_conn_state = drm_atomic_get_old_connector_state(conn_state->state, connector);
+       struct vc4_hdmi_connector_state *old_vc4_state = conn_state_to_vc4_hdmi_conn_state(old_conn_state);
        unsigned long long pixel_rate = mode->clock * 1000;
        unsigned long long tmds_rate;
+       int ret;
 
-       if (vc4_hdmi->variant->unsupported_odd_h_timings &&
-           !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
-           ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
-            (mode->hsync_end % 2) || (mode->htotal % 2)))
-               return -EINVAL;
+       if (vc4_hdmi->variant->unsupported_odd_h_timings) {
+               if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
+                       /* Only try to fixup DBLCLK modes to get 480i and 576i
+                        * working.
+                        * A generic solution for all modes with odd horizontal
+                        * timing values seems impossible based on trying to
+                        * solve it for 1366x768 monitors.
+                        */
+                       if ((mode->hsync_start - mode->hdisplay) & 1)
+                               mode->hsync_start--;
+                       if ((mode->hsync_end - mode->hsync_start) & 1)
+                               mode->hsync_end--;
+               }
+
+               /* Now check whether we still have odd values remaining */
+               if ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
+                   (mode->hsync_end % 2) || (mode->htotal % 2))
+                       return -EINVAL;
+       }
 
        /*
         * The 1440p@60 pixel rate is in the same range than the first
@@ -1118,24 +2156,14 @@ static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
                pixel_rate = mode->clock * 1000;
        }
 
-       if (conn_state->max_bpc == 12) {
-               pixel_rate = pixel_rate * 150;
-               do_div(pixel_rate, 100);
-       } else if (conn_state->max_bpc == 10) {
-               pixel_rate = pixel_rate * 125;
-               do_div(pixel_rate, 100);
-       }
-
-       if (mode->flags & DRM_MODE_FLAG_DBLCLK)
-               pixel_rate = pixel_rate * 2;
-
-       if (pixel_rate > vc4_hdmi->variant->max_pixel_clock)
-               return -EINVAL;
-
-       if (vc4_hdmi->disable_4kp60 && (pixel_rate > HDMI_14_MAX_TMDS_CLK))
-               return -EINVAL;
+       ret = vc4_hdmi_encoder_compute_config(vc4_hdmi, vc4_state, mode);
+       if (ret)
+               return ret;
 
-       vc4_state->pixel_rate = pixel_rate;
+       /* vc4_hdmi_encoder_compute_config may have changed output_bpc and/or output_format */
+       if (vc4_state->output_bpc != old_vc4_state->output_bpc ||
+           vc4_state->output_format != old_vc4_state->output_format)
+               crtc_state->mode_changed = true;
 
        return 0;
 }
@@ -1152,20 +2180,33 @@ vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
             (mode->hsync_end % 2) || (mode->htotal % 2)))
                return MODE_H_ILLEGAL;
 
-       if ((mode->clock * 1000) > vc4_hdmi->variant->max_pixel_clock)
-               return MODE_CLOCK_HIGH;
-
-       if (vc4_hdmi->disable_4kp60 && vc4_hdmi_mode_needs_scrambling(mode))
-               return MODE_CLOCK_HIGH;
-
-       return MODE_OK;
+       return vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode, mode->clock * 1000);
 }
 
 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
        .atomic_check = vc4_hdmi_encoder_atomic_check,
+       .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
        .mode_valid = vc4_hdmi_encoder_mode_valid,
-       .disable = vc4_hdmi_encoder_disable,
-       .enable = vc4_hdmi_encoder_enable,
+};
+
+static int vc4_hdmi_late_register(struct drm_encoder *encoder)
+{
+       struct drm_device *drm = encoder->dev;
+       struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
+       const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
+       int ret;
+
+       ret = vc4_debugfs_add_file(drm->primary, variant->debugfs_name,
+                                  vc4_hdmi_debugfs_regs,
+                                  vc4_hdmi);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+
+static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
+       .late_register = vc4_hdmi_late_register,
 };
 
 static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
@@ -1192,13 +2233,39 @@ static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
        return channel_map;
 }
 
+static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi)
+{
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
+       u32 hotplug;
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               return false;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+       hotplug = HDMI_READ(HDMI_HOTPLUG);
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       drm_dev_exit(idx);
+
+       return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED);
+}
+
 /* HDMI audio codec callbacks */
 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
                                         unsigned int samplerate)
 {
-       u32 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       u32 hsm_clock;
+       unsigned long flags;
        unsigned long n, m;
+       int idx;
 
+       if (!drm_dev_enter(drm, &idx))
+               return;
+
+       hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
        rational_best_approximation(hsm_clock, samplerate,
                                    VC4_HD_MAI_SMP_N_MASK >>
                                    VC4_HD_MAI_SMP_N_SHIFT,
@@ -1206,19 +2273,24 @@ static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
                                     VC4_HD_MAI_SMP_M_SHIFT) + 1,
                                    &n, &m);
 
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
        HDMI_WRITE(HDMI_MAI_SMP,
                   VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
                   VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       drm_dev_exit(idx);
 }
 
 static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
 {
-       struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
-       struct drm_crtc *crtc = encoder->crtc;
-       const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
+       const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
        u32 n, cts;
        u64 tmp;
 
+       lockdep_assert_held(&vc4_hdmi->mutex);
+       lockdep_assert_held(&vc4_hdmi->hw_lock);
+
        n = 128 * samplerate / 1000;
        tmp = (u64)(mode->clock * 1000) * n;
        do_div(tmp, 128 * samplerate);
@@ -1244,64 +2316,118 @@ static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
        return snd_soc_card_get_drvdata(card);
 }
 
-static int vc4_hdmi_audio_startup(struct device *dev, void *data)
+static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
 {
-       struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
-       struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
+       struct drm_display_info *display = &vc4_hdmi->connector.display_info;
+
+       lockdep_assert_held(&vc4_hdmi->mutex);
 
        /*
-        * If the HDMI encoder hasn't probed, or the encoder is
-        * currently in DVI mode, treat the codec dai as missing.
+        * If the encoder is currently in DVI mode, treat the codec DAI
+        * as missing.
         */
-       if (!encoder->crtc || !(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
-                               VC4_HDMI_RAM_PACKET_ENABLE))
-               return -ENODEV;
+       if (!display->is_hdmi)
+               return false;
+
+       return true;
+}
+
+static int vc4_hdmi_audio_startup(struct device *dev, void *data)
+{
+       struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
+       int ret = 0;
+       int idx;
+
+       mutex_lock(&vc4_hdmi->mutex);
+
+       if (!drm_dev_enter(drm, &idx)) {
+               ret = -ENODEV;
+               goto out;
+       }
+
+       if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
+               ret = -ENODEV;
+               goto out_dev_exit;
+       }
 
        vc4_hdmi->audio.streaming = true;
 
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
        HDMI_WRITE(HDMI_MAI_CTL,
                   VC4_HD_MAI_CTL_RESET |
                   VC4_HD_MAI_CTL_FLUSH |
                   VC4_HD_MAI_CTL_DLATE |
                   VC4_HD_MAI_CTL_ERRORE |
                   VC4_HD_MAI_CTL_ERRORF);
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
 
        if (vc4_hdmi->variant->phy_rng_enable)
                vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
 
-       return 0;
+out_dev_exit:
+       drm_dev_exit(idx);
+out:
+       mutex_unlock(&vc4_hdmi->mutex);
+
+       return ret;
 }
 
 static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
 {
-       struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
+       struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
        struct device *dev = &vc4_hdmi->pdev->dev;
+       unsigned long flags;
        int ret;
 
+       lockdep_assert_held(&vc4_hdmi->mutex);
+
        vc4_hdmi->audio.streaming = false;
        ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
        if (ret)
                dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
 
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
        HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
        HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
        HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
 }
 
 static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
 {
        struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
+       int idx;
+
+       mutex_lock(&vc4_hdmi->mutex);
+
+       if (!drm_dev_enter(drm, &idx))
+               goto out;
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
 
        HDMI_WRITE(HDMI_MAI_CTL,
                   VC4_HD_MAI_CTL_DLATE |
                   VC4_HD_MAI_CTL_ERRORE |
                   VC4_HD_MAI_CTL_ERRORF);
 
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
        if (vc4_hdmi->variant->phy_rng_disable)
                vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
 
        vc4_hdmi->audio.streaming = false;
        vc4_hdmi_audio_reset(vc4_hdmi);
+
+       drm_dev_exit(idx);
+
+out:
+       mutex_unlock(&vc4_hdmi->mutex);
 }
 
 static int sample_rate_to_mai_fmt(int samplerate)
@@ -1348,25 +2474,42 @@ static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
                                  struct hdmi_codec_params *params)
 {
        struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
-       struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
        unsigned int sample_rate = params->sample_rate;
        unsigned int channels = params->channels;
+       unsigned long flags;
        u32 audio_packet_config, channel_mask;
        u32 channel_map;
        u32 mai_audio_format;
        u32 mai_sample_rate;
+       int ret = 0;
+       int idx;
 
        dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
                sample_rate, params->sample_width, channels);
 
+       mutex_lock(&vc4_hdmi->mutex);
+
+       if (!drm_dev_enter(drm, &idx)) {
+               ret = -ENODEV;
+               goto out;
+       }
+
+       if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
+               ret = -EINVAL;
+               goto out_dev_exit;
+       }
+
+       vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
        HDMI_WRITE(HDMI_MAI_CTL,
                   VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
                   VC4_HD_MAI_CTL_WHOLSMP |
                   VC4_HD_MAI_CTL_CHALIGN |
                   VC4_HD_MAI_CTL_ENABLE);
 
-       vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
-
        mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
        if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
            params->channels == 8)
@@ -1404,12 +2547,21 @@ static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
        channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
        HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
        HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
+
        vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
 
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
        memcpy(&vc4_hdmi->audio.infoframe, &params->cea, sizeof(params->cea));
-       vc4_hdmi_set_audio_infoframe(encoder);
+       if (vc4_hdmi->output_enabled)
+               vc4_hdmi_set_audio_infoframe(encoder);
 
-       return 0;
+out_dev_exit:
+       drm_dev_exit(idx);
+out:
+       mutex_unlock(&vc4_hdmi->mutex);
+
+       return ret;
 }
 
 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
@@ -1451,7 +2603,9 @@ static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
        struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
        struct drm_connector *connector = &vc4_hdmi->connector;
 
+       mutex_lock(&vc4_hdmi->mutex);
        memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
+       mutex_unlock(&vc4_hdmi->mutex);
 
        return 0;
 }
@@ -1469,6 +2623,14 @@ static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
        .i2s = 1,
 };
 
+static void vc4_hdmi_audio_codec_release(void *ptr)
+{
+       struct vc4_hdmi *vc4_hdmi = ptr;
+
+       platform_device_unregister(vc4_hdmi->audio.codec_pdev);
+       vc4_hdmi->audio.codec_pdev = NULL;
+}
+
 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
 {
        const struct vc4_hdmi_register *mai_data =
@@ -1478,10 +2640,32 @@ static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
        struct device *dev = &vc4_hdmi->pdev->dev;
        struct platform_device *codec_pdev;
        const __be32 *addr;
-       int index, len;
+       int index;
        int ret;
+       int len;
 
-       if (!of_find_property(dev->of_node, "dmas", &len) || !len) {
+       /*
+        * ASoC makes it a bit hard to retrieve a pointer to the
+        * vc4_hdmi structure. Registering the card will overwrite our
+        * device drvdata with a pointer to the snd_soc_card structure,
+        * which can then be used to retrieve whatever drvdata we want
+        * to associate.
+        *
+        * However, that doesn't fly in the case where we wouldn't
+        * register an ASoC card (because of an old DT that is missing
+        * the dmas properties for example), then the card isn't
+        * registered and the device drvdata wouldn't be set.
+        *
+        * We can deal with both cases by making sure a snd_soc_card
+        * pointer and a vc4_hdmi structure are pointing to the same
+        * memory address, so we can treat them indistinctly without any
+        * issue.
+        */
+       BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
+       BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
+
+       if (!of_find_property(dev->of_node, "dmas", &len) ||
+           len == 0) {
                dev_warn(dev,
                         "'dmas' DT property is missing or empty, no HDMI audio\n");
                return 0;
@@ -1510,6 +2694,30 @@ static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
        vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
        vc4_hdmi->audio.dma_data.maxburst = 2;
 
+       /*
+        * NOTE: Strictly speaking, we should probably use a DRM-managed
+        * registration there to avoid removing all the audio components
+        * by the time the driver doesn't have any user anymore.
+        *
+        * However, the ASoC core uses a number of devm_kzalloc calls
+        * when registering, even when using non-device-managed
+        * functions (such as in snd_soc_register_component()).
+        *
+        * If we call snd_soc_unregister_component() in a DRM-managed
+        * action, the device-managed actions have already been executed
+        * and thus we would access memory that has been freed.
+        *
+        * Using device-managed hooks here probably leaves us open to a
+        * bunch of issues if userspace still has a handle on the ALSA
+        * device when the device is removed. However, this is mitigated
+        * by the use of drm_dev_enter()/drm_dev_exit() in the audio
+        * path to prevent the access to the device resources if it
+        * isn't there anymore.
+        *
+        * Then, the vc4_hdmi structure is DRM-managed and thus only
+        * freed whenever the last user has closed the DRM device file.
+        * It should thus outlive ALSA in most situations.
+        */
        ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
        if (ret) {
                dev_err(dev, "Could not register PCM component: %d\n", ret);
@@ -1533,6 +2741,10 @@ static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
        }
        vc4_hdmi->audio.codec_pdev = codec_pdev;
 
+       ret = devm_add_action_or_reset(dev, vc4_hdmi_audio_codec_release, vc4_hdmi);
+       if (ret)
+               return ret;
+
        dai_link->cpus          = &vc4_hdmi->audio.cpu;
        dai_link->codecs        = &vc4_hdmi->audio.codec;
        dai_link->platforms     = &vc4_hdmi->audio.platform;
@@ -1571,19 +2783,14 @@ static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
 
 }
 
-static void vc4_hdmi_audio_exit(struct vc4_hdmi *vc4_hdmi)
-{
-       platform_device_unregister(vc4_hdmi->audio.codec_pdev);
-       vc4_hdmi->audio.codec_pdev = NULL;
-}
-
 static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
 {
        struct vc4_hdmi *vc4_hdmi = priv;
-       struct drm_device *dev = vc4_hdmi->connector.dev;
+       struct drm_connector *connector = &vc4_hdmi->connector;
+       struct drm_device *dev = connector->dev;
 
        if (dev && dev->registered)
-               drm_kms_helper_hotplug_event(dev);
+               drm_connector_helper_hpd_irq_event(connector);
 
        return IRQ_HANDLED;
 }
@@ -1598,36 +2805,24 @@ static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
                unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
                unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
 
-               ret = request_threaded_irq(hpd_con,
-                                          NULL,
-                                          vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
-                                          "vc4 hdmi hpd connected", vc4_hdmi);
+               ret = devm_request_threaded_irq(&pdev->dev, hpd_con,
+                                               NULL,
+                                               vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
+                                               "vc4 hdmi hpd connected", vc4_hdmi);
                if (ret)
                        return ret;
 
-               ret = request_threaded_irq(hpd_rm,
-                                          NULL,
-                                          vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
-                                          "vc4 hdmi hpd disconnected", vc4_hdmi);
-               if (ret) {
-                       free_irq(hpd_con, vc4_hdmi);
+               ret = devm_request_threaded_irq(&pdev->dev, hpd_rm,
+                                               NULL,
+                                               vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
+                                               "vc4 hdmi hpd disconnected", vc4_hdmi);
+               if (ret)
                        return ret;
-               }
-
-               connector->polled = DRM_CONNECTOR_POLL_HPD;
-       }
-
-       return 0;
-}
-
-static void vc4_hdmi_hotplug_exit(struct vc4_hdmi *vc4_hdmi)
-{
-       struct platform_device *pdev = vc4_hdmi->pdev;
 
-       if (vc4_hdmi->variant->external_irq_controller) {
-               free_irq(platform_get_irq_byname(pdev, "hpd-connected"), vc4_hdmi);
-               free_irq(platform_get_irq_byname(pdev, "hpd-removed"), vc4_hdmi);
+               connector->polled = DRM_CONNECTOR_POLL_HPD;
        }
+
+       return 0;
 }
 
 #ifdef CONFIG_DRM_VC4_HDMI_CEC
@@ -1679,6 +2874,8 @@ static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
        struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
        unsigned int i;
 
+       lockdep_assert_held(&vc4_hdmi->hw_lock);
+
        msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
                                        VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
 
@@ -1697,11 +2894,23 @@ static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
        }
 }
 
-static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
+static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
 {
-       struct vc4_hdmi *vc4_hdmi = priv;
        u32 cntrl1;
 
+       /*
+        * We don't need to protect the register access using
+        * drm_dev_enter() there because the interrupt handler lifetime
+        * is tied to the device itself, and not to the DRM device.
+        *
+        * So when the device will be gone, one of the first thing we
+        * will be doing will be to unregister the interrupt handler,
+        * and then unregister the DRM device. drm_dev_enter() would
+        * thus always succeed if we are here.
+        */
+
+       lockdep_assert_held(&vc4_hdmi->hw_lock);
+
        cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
        vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
        cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
@@ -1710,11 +2919,35 @@ static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
        return IRQ_WAKE_THREAD;
 }
 
-static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
+static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
 {
        struct vc4_hdmi *vc4_hdmi = priv;
+       irqreturn_t ret;
+
+       spin_lock(&vc4_hdmi->hw_lock);
+       ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
+       spin_unlock(&vc4_hdmi->hw_lock);
+
+       return ret;
+}
+
+static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
+{
        u32 cntrl1;
 
+       lockdep_assert_held(&vc4_hdmi->hw_lock);
+
+       /*
+        * We don't need to protect the register access using
+        * drm_dev_enter() there because the interrupt handler lifetime
+        * is tied to the device itself, and not to the DRM device.
+        *
+        * So when the device will be gone, one of the first thing we
+        * will be doing will be to unregister the interrupt handler,
+        * and then unregister the DRM device. drm_dev_enter() would
+        * thus always succeed if we are here.
+        */
+
        vc4_hdmi->cec_rx_msg.len = 0;
        cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
        vc4_cec_read_msg(vc4_hdmi, cntrl1);
@@ -1727,6 +2960,18 @@ static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
        return IRQ_WAKE_THREAD;
 }
 
+static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
+{
+       struct vc4_hdmi *vc4_hdmi = priv;
+       irqreturn_t ret;
+
+       spin_lock(&vc4_hdmi->hw_lock);
+       ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
+       spin_unlock(&vc4_hdmi->hw_lock);
+
+       return ret;
+}
+
 static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
 {
        struct vc4_hdmi *vc4_hdmi = priv;
@@ -1734,72 +2979,170 @@ static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
        irqreturn_t ret;
        u32 cntrl5;
 
+       /*
+        * We don't need to protect the register access using
+        * drm_dev_enter() there because the interrupt handler lifetime
+        * is tied to the device itself, and not to the DRM device.
+        *
+        * So when the device will be gone, one of the first thing we
+        * will be doing will be to unregister the interrupt handler,
+        * and then unregister the DRM device. drm_dev_enter() would
+        * thus always succeed if we are here.
+        */
+
        if (!(stat & VC4_HDMI_CPU_CEC))
                return IRQ_NONE;
 
+       spin_lock(&vc4_hdmi->hw_lock);
        cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
        vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
        if (vc4_hdmi->cec_irq_was_rx)
-               ret = vc4_cec_irq_handler_rx_bare(irq, priv);
+               ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
        else
-               ret = vc4_cec_irq_handler_tx_bare(irq, priv);
+               ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
 
        HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
+       spin_unlock(&vc4_hdmi->hw_lock);
+
        return ret;
 }
 
-static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
+static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
 {
        struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
        /* clock period in microseconds */
        const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
-       u32 val = HDMI_READ(HDMI_CEC_CNTRL_5);
+       unsigned long flags;
+       u32 val;
+       int ret;
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               /*
+                * We can't return an error code, because the CEC
+                * framework will emit WARN_ON messages at unbind
+                * otherwise.
+                */
+               return 0;
+
+       ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
+       if (ret) {
+               drm_dev_exit(idx);
+               return ret;
+       }
 
+       mutex_lock(&vc4_hdmi->mutex);
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
+       val = HDMI_READ(HDMI_CEC_CNTRL_5);
        val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
                 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
                 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
        val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
               ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
 
-       if (enable) {
-               HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
-                          VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
-               HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
-               HDMI_WRITE(HDMI_CEC_CNTRL_2,
-                          ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
-                          ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
-                          ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
-                          ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
-                          ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
-               HDMI_WRITE(HDMI_CEC_CNTRL_3,
-                          ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
-                          ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
-                          ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
-                          ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
-               HDMI_WRITE(HDMI_CEC_CNTRL_4,
-                          ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
-                          ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
-                          ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
-                          ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
-
-               if (!vc4_hdmi->variant->external_irq_controller)
-                       HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
-       } else {
-               if (!vc4_hdmi->variant->external_irq_controller)
-                       HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
-               HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
-                          VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
-       }
+       HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
+                  VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
+       HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
+       HDMI_WRITE(HDMI_CEC_CNTRL_2,
+                  ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
+                  ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
+                  ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
+                  ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
+                  ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
+       HDMI_WRITE(HDMI_CEC_CNTRL_3,
+                  ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
+                  ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
+                  ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
+                  ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
+       HDMI_WRITE(HDMI_CEC_CNTRL_4,
+                  ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
+                  ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
+                  ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
+                  ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
+
+       if (!vc4_hdmi->variant->external_irq_controller)
+               HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       mutex_unlock(&vc4_hdmi->mutex);
+       drm_dev_exit(idx);
+
+       return 0;
+}
+
+static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
+{
+       struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               /*
+                * We can't return an error code, because the CEC
+                * framework will emit WARN_ON messages at unbind
+                * otherwise.
+                */
+               return 0;
+
+       mutex_lock(&vc4_hdmi->mutex);
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
+       if (!vc4_hdmi->variant->external_irq_controller)
+               HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
+
+       HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
+                  VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       mutex_unlock(&vc4_hdmi->mutex);
+
+       pm_runtime_put(&vc4_hdmi->pdev->dev);
+
+       drm_dev_exit(idx);
+
        return 0;
 }
 
+static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
+{
+       if (enable)
+               return vc4_hdmi_cec_enable(adap);
+       else
+               return vc4_hdmi_cec_disable(adap);
+}
+
 static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
 {
        struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
+       struct drm_device *drm = vc4_hdmi->connector.dev;
+       unsigned long flags;
+       int idx;
+
+       if (!drm_dev_enter(drm, &idx))
+               /*
+                * We can't return an error code, because the CEC
+                * framework will emit WARN_ON messages at unbind
+                * otherwise.
+                */
+               return 0;
 
+       mutex_lock(&vc4_hdmi->mutex);
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
        HDMI_WRITE(HDMI_CEC_CNTRL_1,
                   (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
                   (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+       mutex_unlock(&vc4_hdmi->mutex);
+
+       drm_dev_exit(idx);
+
        return 0;
 }
 
@@ -1808,14 +3151,24 @@ static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
 {
        struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
        struct drm_device *dev = vc4_hdmi->connector.dev;
+       unsigned long flags;
        u32 val;
        unsigned int i;
+       int idx;
+
+       if (!drm_dev_enter(dev, &idx))
+               return -ENODEV;
 
        if (msg->len > 16) {
                drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
+               drm_dev_exit(idx);
                return -ENOMEM;
        }
 
+       mutex_lock(&vc4_hdmi->mutex);
+
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+
        for (i = 0; i < msg->len; i += 4)
                HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
                           (msg->msg[i]) |
@@ -1831,6 +3184,11 @@ static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
        val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
 
        HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
+
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+       mutex_unlock(&vc4_hdmi->mutex);
+       drm_dev_exit(idx);
+
        return 0;
 }
 
@@ -1840,12 +3198,20 @@ static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
        .adap_transmit = vc4_hdmi_cec_adap_transmit,
 };
 
+static void vc4_hdmi_cec_release(void *ptr)
+{
+       struct vc4_hdmi *vc4_hdmi = ptr;
+
+       cec_unregister_adapter(vc4_hdmi->cec_adap);
+       vc4_hdmi->cec_adap = NULL;
+}
+
 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
 {
        struct cec_connector_info conn_info;
        struct platform_device *pdev = vc4_hdmi->pdev;
        struct device *dev = &pdev->dev;
-       u32 value;
+       unsigned long flags;
        int ret;
 
        if (!of_find_property(dev->of_node, "interrupts", NULL)) {
@@ -1864,53 +3230,65 @@ static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
        cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
        cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
 
-       value = HDMI_READ(HDMI_CEC_CNTRL_1);
-       /* Set the logical address to Unregistered */
-       value |= VC4_HDMI_CEC_ADDR_MASK;
-       HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
-
-       vc4_hdmi_cec_update_clk_div(vc4_hdmi);
-
        if (vc4_hdmi->variant->external_irq_controller) {
-               ret = request_threaded_irq(platform_get_irq_byname(pdev, "cec-rx"),
-                                          vc4_cec_irq_handler_rx_bare,
-                                          vc4_cec_irq_handler_rx_thread, 0,
-                                          "vc4 hdmi cec rx", vc4_hdmi);
+               ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-rx"),
+                                               vc4_cec_irq_handler_rx_bare,
+                                               vc4_cec_irq_handler_rx_thread, 0,
+                                               "vc4 hdmi cec rx", vc4_hdmi);
                if (ret)
                        goto err_delete_cec_adap;
 
-               ret = request_threaded_irq(platform_get_irq_byname(pdev, "cec-tx"),
-                                          vc4_cec_irq_handler_tx_bare,
-                                          vc4_cec_irq_handler_tx_thread, 0,
-                                          "vc4 hdmi cec tx", vc4_hdmi);
+               ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-tx"),
+                                               vc4_cec_irq_handler_tx_bare,
+                                               vc4_cec_irq_handler_tx_thread, 0,
+                                               "vc4 hdmi cec tx", vc4_hdmi);
                if (ret)
-                       goto err_remove_cec_rx_handler;
+                       goto err_delete_cec_adap;
        } else {
+               spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
                HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
+               spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
 
-               ret = request_threaded_irq(platform_get_irq(pdev, 0),
-                                          vc4_cec_irq_handler,
-                                          vc4_cec_irq_handler_thread, 0,
-                                          "vc4 hdmi cec", vc4_hdmi);
+               ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
+                                               vc4_cec_irq_handler,
+                                               vc4_cec_irq_handler_thread, 0,
+                                               "vc4 hdmi cec", vc4_hdmi);
                if (ret)
                        goto err_delete_cec_adap;
        }
 
        ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
        if (ret < 0)
-               goto err_remove_handlers;
-
-       return 0;
+               goto err_delete_cec_adap;
 
-err_remove_handlers:
-       if (vc4_hdmi->variant->external_irq_controller)
-               free_irq(platform_get_irq_byname(pdev, "cec-tx"), vc4_hdmi);
-       else
-               free_irq(platform_get_irq(pdev, 0), vc4_hdmi);
+       /*
+        * NOTE: Strictly speaking, we should probably use a DRM-managed
+        * registration there to avoid removing the CEC adapter by the
+        * time the DRM driver doesn't have any user anymore.
+        *
+        * However, the CEC framework already cleans up the CEC adapter
+        * only when the last user has closed its file descriptor, so we
+        * don't need to handle it in DRM.
+        *
+        * By the time the device-managed hook is executed, we will give
+        * up our reference to the CEC adapter and therefore don't
+        * really care when it's actually freed.
+        *
+        * There's still a problematic sequence: if we unregister our
+        * CEC adapter, but the userspace keeps a handle on the CEC
+        * adapter but not the DRM device for some reason. In such a
+        * case, our vc4_hdmi structure will be freed, but the
+        * cec_adapter structure will have a dangling pointer to what
+        * used to be our HDMI controller. If we get a CEC call at that
+        * moment, we could end up with a use-after-free. Fortunately,
+        * the CEC framework already handles this too, by calling
+        * cec_is_registered() in cec_ioctl() and cec_poll().
+        */
+       ret = devm_add_action_or_reset(dev, vc4_hdmi_cec_release, vc4_hdmi);
+       if (ret)
+               return ret;
 
-err_remove_cec_rx_handler:
-       if (vc4_hdmi->variant->external_irq_controller)
-               free_irq(platform_get_irq_byname(pdev, "cec-rx"), vc4_hdmi);
+       return 0;
 
 err_delete_cec_adap:
        cec_delete_adapter(vc4_hdmi->cec_adap);
@@ -1918,18 +3296,27 @@ err_delete_cec_adap:
        return ret;
 }
 
-static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi)
+static int vc4_hdmi_cec_resume(struct vc4_hdmi *vc4_hdmi)
 {
-       struct platform_device *pdev = vc4_hdmi->pdev;
+       unsigned long flags;
+       u32 value;
 
-       if (vc4_hdmi->variant->external_irq_controller) {
-               free_irq(platform_get_irq_byname(pdev, "cec-rx"), vc4_hdmi);
-               free_irq(platform_get_irq_byname(pdev, "cec-tx"), vc4_hdmi);
-       } else {
-               free_irq(platform_get_irq(pdev, 0), vc4_hdmi);
+       spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+       value = HDMI_READ(HDMI_CEC_CNTRL_1);
+       /* Set the logical address to Unregistered */
+       value |= VC4_HDMI_CEC_ADDR_MASK;
+       HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
+       spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
+
+       vc4_hdmi_cec_update_clk_div(vc4_hdmi);
+
+       if (!vc4_hdmi->variant->external_irq_controller) {
+               spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
+               HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
+               spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
        }
 
-       cec_unregister_adapter(vc4_hdmi->cec_adap);
+       return 0;
 }
 #else
 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
@@ -1937,11 +3324,21 @@ static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
        return 0;
 }
 
-static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {};
-
+static int vc4_hdmi_cec_resume(struct vc4_hdmi *vc4_hdmi)
+{
+       return 0;
+}
 #endif
 
-static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi,
+static void vc4_hdmi_free_regset(struct drm_device *drm, void *ptr)
+{
+       struct debugfs_reg32 *regs = ptr;
+
+       kfree(regs);
+}
+
+static int vc4_hdmi_build_regset(struct drm_device *drm,
+                                struct vc4_hdmi *vc4_hdmi,
                                 struct debugfs_regset32 *regset,
                                 enum vc4_hdmi_regs reg)
 {
@@ -1949,6 +3346,7 @@ static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi,
        struct debugfs_reg32 *regs, *new_regs;
        unsigned int count = 0;
        unsigned int i;
+       int ret;
 
        regs = kcalloc(variant->num_registers, sizeof(*regs),
                       GFP_KERNEL);
@@ -1974,10 +3372,15 @@ static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi,
        regset->regs = new_regs;
        regset->nregs = count;
 
+       ret = drmm_add_action_or_reset(drm, vc4_hdmi_free_regset, new_regs);
+       if (ret)
+               return ret;
+
        return 0;
 }
 
-static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
+static int vc4_hdmi_init_resources(struct drm_device *drm,
+                                  struct vc4_hdmi *vc4_hdmi)
 {
        struct platform_device *pdev = vc4_hdmi->pdev;
        struct device *dev = &pdev->dev;
@@ -1991,11 +3394,11 @@ static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
        if (IS_ERR(vc4_hdmi->hd_regs))
                return PTR_ERR(vc4_hdmi->hd_regs);
 
-       ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
+       ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
        if (ret)
                return ret;
 
-       ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
+       ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
        if (ret)
                return ret;
 
@@ -2018,11 +3421,13 @@ static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
        return 0;
 }
 
-static int vc5_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
+static int vc5_hdmi_init_resources(struct drm_device *drm,
+                                  struct vc4_hdmi *vc4_hdmi)
 {
        struct platform_device *pdev = vc4_hdmi->pdev;
        struct device *dev = &pdev->dev;
        struct resource *res;
+       int ret;
 
        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
        if (!res)
@@ -2119,10 +3524,42 @@ static int vc5_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
                return PTR_ERR(vc4_hdmi->reset);
        }
 
+       ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
+       if (ret)
+               return ret;
+
+       ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
+       if (ret)
+               return ret;
+
+       ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->cec_regset, VC5_CEC);
+       if (ret)
+               return ret;
+
+       ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->csc_regset, VC5_CSC);
+       if (ret)
+               return ret;
+
+       ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->dvp_regset, VC5_DVP);
+       if (ret)
+               return ret;
+
+       ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->phy_regset, VC5_PHY);
+       if (ret)
+               return ret;
+
+       ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->ram_regset, VC5_RAM);
+       if (ret)
+               return ret;
+
+       ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->rm_regset, VC5_RM);
+       if (ret)
+               return ret;
+
        return 0;
 }
 
-static int vc4_hdmi_runtime_suspend(struct device *dev)
+static int __maybe_unused vc4_hdmi_runtime_suspend(struct device *dev)
 {
        struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
 
@@ -2140,9 +3577,32 @@ static int vc4_hdmi_runtime_resume(struct device *dev)
        if (ret)
                return ret;
 
+       if (vc4_hdmi->variant->reset)
+               vc4_hdmi->variant->reset(vc4_hdmi);
+
+       ret = vc4_hdmi_cec_resume(vc4_hdmi);
+       if (ret) {
+               clk_disable_unprepare(vc4_hdmi->hsm_clock);
+               return ret;
+       }
+
        return 0;
 }
 
+static void vc4_hdmi_put_ddc_device(void *ptr)
+{
+       struct vc4_hdmi *vc4_hdmi = ptr;
+
+       put_device(&vc4_hdmi->ddc->dev);
+}
+
+#ifdef CONFIG_EXTCON
+static const unsigned int vc4_hdmi_extcon_cable[] = {
+       EXTCON_DISP_HDMI,
+       EXTCON_NONE,
+};
+#endif
+
 static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
 {
        const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
@@ -2153,23 +3613,38 @@ static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
        struct device_node *ddc_node;
        int ret;
 
-       vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
+       vc4_hdmi = drmm_kzalloc(drm, sizeof(*vc4_hdmi), GFP_KERNEL);
        if (!vc4_hdmi)
                return -ENOMEM;
+
+       ret = drmm_mutex_init(drm, &vc4_hdmi->mutex);
+       if (ret)
+               return ret;
+
+       spin_lock_init(&vc4_hdmi->hw_lock);
        INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
 
        dev_set_drvdata(dev, vc4_hdmi);
-       encoder = &vc4_hdmi->encoder.base.base;
-       vc4_hdmi->encoder.base.type = variant->encoder_type;
-       vc4_hdmi->encoder.base.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
-       vc4_hdmi->encoder.base.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
-       vc4_hdmi->encoder.base.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
-       vc4_hdmi->encoder.base.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
-       vc4_hdmi->encoder.base.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
+       encoder = &vc4_hdmi->encoder.base;
+       vc4_hdmi->encoder.type = variant->encoder_type;
+       vc4_hdmi->encoder.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
+       vc4_hdmi->encoder.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
+       vc4_hdmi->encoder.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
+       vc4_hdmi->encoder.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
+       vc4_hdmi->encoder.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
        vc4_hdmi->pdev = pdev;
        vc4_hdmi->variant = variant;
 
-       ret = variant->init_resources(vc4_hdmi);
+       /*
+        * Since we don't know the state of the controller and its
+        * display (if any), let's assume it's always enabled.
+        * vc4_hdmi_disable_scrambling() will thus run at boot, make
+        * sure it's disabled, and avoid any inconsistency.
+        */
+       if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK)
+               vc4_hdmi->scdc_enabled = true;
+
+       ret = variant->init_resources(drm, vc4_hdmi);
        if (ret)
                return ret;
 
@@ -2186,26 +3661,38 @@ static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
                return -EPROBE_DEFER;
        }
 
+       ret = devm_add_action_or_reset(dev, vc4_hdmi_put_ddc_device, vc4_hdmi);
+       if (ret)
+               return ret;
+
+#ifdef CONFIG_EXTCON
+       vc4_hdmi->status = connector_status_disconnected;
+
+       /* Initialize extcon device */
+       vc4_hdmi->edev = devm_extcon_dev_allocate(dev, vc4_hdmi_extcon_cable);
+       if (IS_ERR(vc4_hdmi->edev)) {
+               dev_err(dev, "failed to allocate memory for extcon\n");
+               return PTR_ERR(vc4_hdmi->edev);
+       }
+
+       ret = devm_extcon_dev_register(dev, vc4_hdmi->edev);
+       if (ret) {
+               dev_err(dev, "failed to register extcon device\n");
+               return ret;
+       }
+#endif
+
        /* Only use the GPIO HPD pin if present in the DT, otherwise
         * we'll use the HDMI core's register.
         */
        vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
        if (IS_ERR(vc4_hdmi->hpd_gpio)) {
-               ret = PTR_ERR(vc4_hdmi->hpd_gpio);
-               goto err_put_ddc;
+               return PTR_ERR(vc4_hdmi->hpd_gpio);
        }
 
        vc4_hdmi->disable_wifi_frequencies =
                of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
 
-       if (variant->max_pixel_clock == 600000000) {
-               struct vc4_dev *vc4 = to_vc4_dev(drm);
-               long max_rate = clk_round_rate(vc4->hvs->core_clk, 550000000);
-
-               if (max_rate < 550000000)
-                       vc4_hdmi->disable_4kp60 = true;
-       }
-
        /*
         * If we boot without any cable connected to the HDMI connector,
         * the firmware will skip the HSM initialization and leave it
@@ -2217,20 +3704,15 @@ static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
         */
        ret = clk_set_min_rate(vc4_hdmi->hsm_clock, HSM_MIN_CLOCK_FREQ);
        if (ret)
-               goto err_put_ddc;
+               return ret;
 
-       pm_runtime_enable(dev);
+       ret = devm_pm_runtime_enable(dev);
+       if (ret)
+               return ret;
 
-       /*
-        *  We need to have the device powered up at this point to call
-        *  our reset hook and for the CEC init.
-        */
        ret = pm_runtime_resume_and_get(dev);
        if (ret)
-               goto err_disable_runtime_pm;
-
-       if (vc4_hdmi->variant->reset)
-               vc4_hdmi->variant->reset(vc4_hdmi);
+               return ret;
 
        if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
             of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
@@ -2240,93 +3722,43 @@ static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
                clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
        }
 
-       drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
+       ret = drmm_encoder_init(drm, encoder,
+                               &vc4_hdmi_encoder_funcs,
+                               DRM_MODE_ENCODER_TMDS,
+                               NULL);
+       if (ret)
+               goto err_put_runtime_pm;
+
        drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
 
        ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
        if (ret)
-               goto err_destroy_encoder;
+               goto err_put_runtime_pm;
 
        ret = vc4_hdmi_hotplug_init(vc4_hdmi);
        if (ret)
-               goto err_destroy_conn;
+               goto err_put_runtime_pm;
 
        ret = vc4_hdmi_cec_init(vc4_hdmi);
        if (ret)
-               goto err_free_hotplug;
+               goto err_put_runtime_pm;
 
        ret = vc4_hdmi_audio_init(vc4_hdmi);
        if (ret)
-               goto err_free_cec;
-
-       vc4_debugfs_add_file(drm, variant->debugfs_name,
-                            vc4_hdmi_debugfs_regs,
-                            vc4_hdmi);
+               goto err_put_runtime_pm;
 
        pm_runtime_put_sync(dev);
 
        return 0;
 
-err_free_cec:
-       vc4_hdmi_cec_exit(vc4_hdmi);
-err_free_hotplug:
-       vc4_hdmi_hotplug_exit(vc4_hdmi);
-err_destroy_conn:
-       vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
-err_destroy_encoder:
-       drm_encoder_cleanup(encoder);
+err_put_runtime_pm:
        pm_runtime_put_sync(dev);
-err_disable_runtime_pm:
-       pm_runtime_disable(dev);
-err_put_ddc:
-       put_device(&vc4_hdmi->ddc->dev);
 
        return ret;
 }
 
-static void vc4_hdmi_unbind(struct device *dev, struct device *master,
-                           void *data)
-{
-       struct vc4_hdmi *vc4_hdmi;
-
-       /*
-        * ASoC makes it a bit hard to retrieve a pointer to the
-        * vc4_hdmi structure. Registering the card will overwrite our
-        * device drvdata with a pointer to the snd_soc_card structure,
-        * which can then be used to retrieve whatever drvdata we want
-        * to associate.
-        *
-        * However, that doesn't fly in the case where we wouldn't
-        * register an ASoC card (because of an old DT that is missing
-        * the dmas properties for example), then the card isn't
-        * registered and the device drvdata wouldn't be set.
-        *
-        * We can deal with both cases by making sure a snd_soc_card
-        * pointer and a vc4_hdmi structure are pointing to the same
-        * memory address, so we can treat them indistinctly without any
-        * issue.
-        */
-       BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
-       BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
-       vc4_hdmi = dev_get_drvdata(dev);
-
-       kfree(vc4_hdmi->hdmi_regset.regs);
-       kfree(vc4_hdmi->hd_regset.regs);
-
-       vc4_hdmi_audio_exit(vc4_hdmi);
-       vc4_hdmi_cec_exit(vc4_hdmi);
-       vc4_hdmi_hotplug_exit(vc4_hdmi);
-       vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
-       drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
-
-       pm_runtime_disable(dev);
-
-       put_device(&vc4_hdmi->ddc->dev);
-}
-
 static const struct component_ops vc4_hdmi_ops = {
        .bind   = vc4_hdmi_bind,
-       .unbind = vc4_hdmi_unbind,
 };
 
 static int vc4_hdmi_dev_probe(struct platform_device *pdev)
@@ -2364,7 +3796,7 @@ static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
        .encoder_type           = VC4_ENCODER_TYPE_HDMI0,
        .debugfs_name           = "hdmi0_regs",
        .card_name              = "vc4-hdmi-0",
-       .max_pixel_clock        = HDMI_14_MAX_TMDS_CLK,
+       .max_pixel_clock        = 600000000,
        .registers              = vc5_hdmi_hdmi0_fields,
        .num_registers          = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
        .phy_lane_mapping       = {
@@ -2386,6 +3818,7 @@ static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
        .phy_rng_disable        = vc5_hdmi_phy_rng_disable,
        .channel_map            = vc5_hdmi_channel_map,
        .supports_hdr           = true,
+       .hp_detect              = vc5_hdmi_hp_detect,
 };
 
 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
@@ -2414,6 +3847,7 @@ static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
        .phy_rng_disable        = vc5_hdmi_phy_rng_disable,
        .channel_map            = vc5_hdmi_channel_map,
        .supports_hdr           = true,
+       .hp_detect              = vc5_hdmi_hp_detect,
 };
 
 static const struct of_device_id vc4_hdmi_dt_match[] = {