1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Broadcom
4 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
5 * Copyright (C) 2013 Red Hat
6 * Author: Rob Clark <robdclark@gmail.com>
10 * DOC: VC4 Falcon HDMI module
12 * The HDMI core has a state machine and a PHY. On BCM2835, most of
13 * the unit operates off of the HSM clock from CPRMAN. It also
14 * internally uses the PLLH_PIX clock for the PHY.
16 * HDMI infoframes are kept within a small packet ram, where each
17 * packet can be individually enabled for including in a frame.
19 * HDMI audio is implemented entirely within the HDMI IP block. A
20 * register in the HDMI encoder takes SPDIF frames from the DMA engine
21 * and transfers them over an internal MAI (multi-channel audio
22 * interconnect) bus to the encoder side for insertion into the video
25 * The driver's HDMI encoder does not yet support power management.
26 * The HDMI encoder's power domain and the HSM/pixel clocks are kept
27 * continuously running, and only the HDMI logic and packet ram are
28 * powered off/on at disable/enable time.
30 * The driver does not yet support CEC control, though the HDMI
31 * encoder block has CEC support.
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_edid.h>
37 #include <drm/drm_probe_helper.h>
38 #include <drm/drm_simple_kms_helper.h>
39 #include <drm/drm_scdc_helper.h>
40 #include <linux/clk.h>
41 #include <linux/component.h>
42 #include <linux/gpio/consumer.h>
43 #include <linux/extcon-provider.h>
44 #include <linux/i2c.h>
45 #include <linux/module.h>
46 #include <linux/moduleparam.h>
47 #include <linux/of_address.h>
48 #include <linux/of_gpio.h>
49 #include <linux/of_platform.h>
50 #include <linux/pm_runtime.h>
51 #include <linux/rational.h>
52 #include <linux/reset.h>
53 #include <sound/dmaengine_pcm.h>
54 #include <sound/hdmi-codec.h>
55 #include <sound/pcm_drm_eld.h>
56 #include <sound/pcm_params.h>
57 #include <sound/soc.h>
58 #include "media/cec.h"
61 #include "vc4_hdmi_regs.h"
65 * "Broadcast RGB" property.
66 * Allows overriding of HDMI full or limited range RGB
68 #define VC4_BROADCAST_RGB_AUTO 0
69 #define VC4_BROADCAST_RGB_FULL 1
70 #define VC4_BROADCAST_RGB_LIMITED 2
72 #define VC5_HDMI_HORZA_HFP_SHIFT 16
73 #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
74 #define VC5_HDMI_HORZA_VPOS BIT(15)
75 #define VC5_HDMI_HORZA_HPOS BIT(14)
76 #define VC5_HDMI_HORZA_HAP_SHIFT 0
77 #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
79 #define VC5_HDMI_HORZB_HBP_SHIFT 16
80 #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
81 #define VC5_HDMI_HORZB_HSP_SHIFT 0
82 #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
84 #define VC5_HDMI_VERTA_VSP_SHIFT 24
85 #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
86 #define VC5_HDMI_VERTA_VFP_SHIFT 16
87 #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
88 #define VC5_HDMI_VERTA_VAL_SHIFT 0
89 #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
91 #define VC5_HDMI_VERTB_VSPO_SHIFT 16
92 #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
94 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
95 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
96 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
97 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
99 #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
101 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
102 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
104 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
105 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
107 #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
109 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
110 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
112 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK VC4_MASK(7, 0)
113 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_SET_AVMUTE BIT(0)
114 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE BIT(4)
116 # define VC4_HD_M_SW_RST BIT(2)
117 # define VC4_HD_M_ENABLE BIT(0)
119 #define HSM_MIN_CLOCK_FREQ 120000000
120 #define CEC_CLOCK_FREQ 40000
122 #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
124 /* bit field to force hotplug detection. bit0 = HDMI0 */
125 static int force_hotplug = 0;
126 module_param(force_hotplug, int, 0644);
128 static const char * const output_format_str[] = {
129 [VC4_HDMI_OUTPUT_RGB] = "RGB",
130 [VC4_HDMI_OUTPUT_YUV420] = "YUV 4:2:0",
131 [VC4_HDMI_OUTPUT_YUV422] = "YUV 4:2:2",
132 [VC4_HDMI_OUTPUT_YUV444] = "YUV 4:4:4",
135 static const char *vc4_hdmi_output_fmt_str(enum vc4_hdmi_output_format fmt)
137 if (fmt >= ARRAY_SIZE(output_format_str))
140 return output_format_str[fmt];
143 static unsigned long long
144 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
145 unsigned int bpc, enum vc4_hdmi_output_format fmt);
147 static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder)
149 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
150 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
152 lockdep_assert_held(&vc4_hdmi->mutex);
154 if (!display->is_hdmi)
157 if (!display->hdmi.scdc.supported ||
158 !display->hdmi.scdc.scrambling.supported)
164 static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
166 enum vc4_hdmi_output_format fmt)
168 unsigned long long clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
170 return clock > HDMI_14_MAX_TMDS_CLK;
173 static bool vc4_hdmi_is_full_range(struct vc4_hdmi *vc4_hdmi,
174 const struct drm_display_mode *mode)
176 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
178 if (vc4_hdmi->broadcast_rgb == VC4_BROADCAST_RGB_LIMITED)
180 else if (vc4_hdmi->broadcast_rgb == VC4_BROADCAST_RGB_FULL)
182 return !display->is_hdmi ||
183 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
186 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
188 struct drm_info_node *node = (struct drm_info_node *)m->private;
189 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
190 struct drm_device *drm = vc4_hdmi->connector.dev;
191 struct drm_printer p = drm_seq_file_printer(m);
194 if (!drm_dev_enter(drm, &idx))
197 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
198 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
199 drm_print_regset32(&p, &vc4_hdmi->cec_regset);
200 drm_print_regset32(&p, &vc4_hdmi->csc_regset);
201 drm_print_regset32(&p, &vc4_hdmi->dvp_regset);
202 drm_print_regset32(&p, &vc4_hdmi->phy_regset);
203 drm_print_regset32(&p, &vc4_hdmi->ram_regset);
204 drm_print_regset32(&p, &vc4_hdmi->rm_regset);
211 static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
213 struct drm_device *drm = vc4_hdmi->connector.dev;
218 * We can be called by our bind callback, when the
219 * connector->dev pointer might not be initialised yet.
221 if (drm && !drm_dev_enter(drm, &idx))
224 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
226 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
228 HDMI_WRITE(HDMI_M_CTL, 0);
230 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
232 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
233 VC4_HDMI_SW_RESET_HDMI |
234 VC4_HDMI_SW_RESET_FORMAT_DETECT);
236 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
238 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
244 static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
246 struct drm_device *drm = vc4_hdmi->connector.dev;
251 * We can be called by our bind callback, when the
252 * connector->dev pointer might not be initialised yet.
254 if (drm && !drm_dev_enter(drm, &idx))
257 reset_control_reset(vc4_hdmi->reset);
259 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
261 HDMI_WRITE(HDMI_DVP_CTL, 0);
263 HDMI_WRITE(HDMI_CLOCK_STOP,
264 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
266 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
272 #ifdef CONFIG_DRM_VC4_HDMI_CEC
273 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
275 struct drm_device *drm = vc4_hdmi->connector.dev;
276 unsigned long cec_rate;
283 * This function is called by our runtime_resume implementation
284 * and thus at bind time, when we haven't registered our
285 * connector yet and thus don't have a pointer to the DRM
288 if (drm && !drm_dev_enter(drm, &idx))
291 cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
293 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
295 value = HDMI_READ(HDMI_CEC_CNTRL_1);
296 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
299 * Set the clock divider: the hsm_clock rate and this divider
300 * setting will give a 40 kHz CEC clock.
302 clk_cnt = cec_rate / CEC_CLOCK_FREQ;
303 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
304 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
306 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
312 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
315 static int reset_pipe(struct drm_crtc *crtc,
316 struct drm_modeset_acquire_ctx *ctx)
318 struct drm_atomic_state *state;
319 struct drm_crtc_state *crtc_state;
322 state = drm_atomic_state_alloc(crtc->dev);
326 state->acquire_ctx = ctx;
328 crtc_state = drm_atomic_get_crtc_state(state, crtc);
329 if (IS_ERR(crtc_state)) {
330 ret = PTR_ERR(crtc_state);
334 crtc_state->connectors_changed = true;
336 ret = drm_atomic_commit(state);
338 drm_atomic_state_put(state);
343 static int vc4_hdmi_reset_link(struct drm_connector *connector,
344 struct drm_modeset_acquire_ctx *ctx)
346 struct drm_device *drm = connector->dev;
347 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
348 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
349 struct drm_connector_state *conn_state;
350 struct drm_crtc_state *crtc_state;
351 struct drm_crtc *crtc;
352 bool scrambling_needed;
359 ret = drm_modeset_lock(&drm->mode_config.connection_mutex, ctx);
363 conn_state = connector->state;
364 crtc = conn_state->crtc;
368 ret = drm_modeset_lock(&crtc->mutex, ctx);
372 crtc_state = crtc->state;
373 if (!crtc_state->active)
376 if (!vc4_hdmi_supports_scrambling(encoder))
379 scrambling_needed = vc4_hdmi_mode_needs_scrambling(&vc4_hdmi->saved_adjusted_mode,
380 vc4_hdmi->output_bpc,
381 vc4_hdmi->output_format);
382 if (!scrambling_needed)
385 if (conn_state->commit &&
386 !try_wait_for_completion(&conn_state->commit->hw_done))
389 ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
391 drm_err(drm, "Failed to read TMDS config: %d\n", ret);
395 if (!!(config & SCDC_SCRAMBLING_ENABLE) == scrambling_needed)
399 * HDMI 2.0 says that one should not send scrambled data
400 * prior to configuring the sink scrambling, and that
401 * TMDS clock/data transmission should be suspended when
402 * changing the TMDS clock rate in the sink. So let's
403 * just do a full modeset here, even though some sinks
404 * would be perfectly happy if were to just reconfigure
405 * the SCDC settings on the fly.
407 return reset_pipe(crtc, ctx);
410 static void vc4_hdmi_handle_hotplug(struct vc4_hdmi *vc4_hdmi,
411 struct drm_modeset_acquire_ctx *ctx,
412 enum drm_connector_status status)
414 struct drm_connector *connector = &vc4_hdmi->connector;
419 * NOTE: This function should really be called with
420 * vc4_hdmi->mutex held, but doing so results in reentrancy
421 * issues since cec_s_phys_addr_from_edid might call
422 * .adap_enable, which leads to that funtion being called with
425 * A similar situation occurs with
426 * drm_atomic_helper_connector_hdmi_reset_link() that will call
427 * into our KMS hooks if the scrambling was enabled.
429 * Concurrency isn't an issue at the moment since we don't share
430 * any state with any of the other frameworks so we can ignore
434 if (status == connector->status)
437 if (status == connector_status_disconnected) {
438 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
442 edid = drm_get_edid(connector, vc4_hdmi->ddc);
446 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
449 vc4_hdmi_reset_link(connector, ctx);
452 if (ret != vc4_hdmi->status) {
453 extcon_set_state_sync(vc4_hdmi->edev, EXTCON_DISP_HDMI,
454 (status == connector_status_connected ?
456 vc4_hdmi->status = ret;
461 static int vc4_hdmi_connector_detect_ctx(struct drm_connector *connector,
462 struct drm_modeset_acquire_ctx *ctx,
465 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
466 enum drm_connector_status status = connector_status_disconnected;
469 * NOTE: This function should really take vc4_hdmi->mutex, but
470 * doing so results in reentrancy issues since
471 * vc4_hdmi_handle_hotplug() can call into other functions that
472 * would take the mutex while it's held here.
474 * Concurrency isn't an issue at the moment since we don't share
475 * any state with any of the other frameworks so we can ignore
479 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
481 if (force_hotplug & BIT(vc4_hdmi->encoder.type - VC4_ENCODER_TYPE_HDMI0))
482 status = connector_status_connected;
483 else if (vc4_hdmi->hpd_gpio) {
484 if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
485 status = connector_status_connected;
487 if (vc4_hdmi->variant->hp_detect &&
488 vc4_hdmi->variant->hp_detect(vc4_hdmi))
489 status = connector_status_connected;
492 vc4_hdmi_handle_hotplug(vc4_hdmi, ctx, status);
493 pm_runtime_put(&vc4_hdmi->pdev->dev);
498 static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
500 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
501 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
506 * NOTE: This function should really take vc4_hdmi->mutex, but
507 * doing so results in reentrancy issues since
508 * cec_s_phys_addr_from_edid might call .adap_enable, which
509 * leads to that funtion being called with our mutex held.
511 * Concurrency isn't an issue at the moment since we don't share
512 * any state with any of the other frameworks so we can ignore
516 edid = drm_get_edid(connector, vc4_hdmi->ddc);
517 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
521 drm_connector_update_edid_property(connector, edid);
522 ret = drm_add_edid_modes(connector, edid);
525 if (!vc4->hvs->vc5_hdmi_enable_hdmi_20) {
526 struct drm_device *drm = connector->dev;
527 const struct drm_display_mode *mode;
529 list_for_each_entry(mode, &connector->probed_modes, head) {
530 if (vc4_hdmi_mode_needs_scrambling(mode, 8, VC4_HDMI_OUTPUT_RGB)) {
531 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
532 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
540 static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
541 struct drm_atomic_state *state)
543 struct drm_connector_state *old_state =
544 drm_atomic_get_old_connector_state(state, connector);
545 struct vc4_hdmi_connector_state *old_vc4_state = conn_state_to_vc4_hdmi_conn_state(old_state);
546 struct drm_connector_state *new_state =
547 drm_atomic_get_new_connector_state(state, connector);
548 struct vc4_hdmi_connector_state *new_vc4_state = conn_state_to_vc4_hdmi_conn_state(new_state);
549 struct drm_crtc *crtc = new_state->crtc;
554 if (old_state->colorspace != new_state->colorspace ||
555 old_vc4_state->broadcast_rgb != new_vc4_state->broadcast_rgb ||
556 old_vc4_state->requested_output_format != new_vc4_state->requested_output_format ||
557 !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
558 struct drm_crtc_state *crtc_state;
560 crtc_state = drm_atomic_get_crtc_state(state, crtc);
561 if (IS_ERR(crtc_state))
562 return PTR_ERR(crtc_state);
564 crtc_state->mode_changed = true;
571 * vc4_hdmi_connector_atomic_get_property - hook for
572 * connector->atomic_get_property.
573 * @connector: Connector to get the property for.
574 * @state: Connector state to retrieve the property from.
575 * @property: Property to retrieve.
576 * @val: Return value for the property.
578 * Returns the atomic property value for a digital connector.
580 int vc4_hdmi_connector_get_property(struct drm_connector *connector,
581 const struct drm_connector_state *state,
582 struct drm_property *property,
585 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
586 const struct vc4_hdmi_connector_state *vc4_conn_state =
587 const_conn_state_to_vc4_hdmi_conn_state(state);
589 if (property == vc4_hdmi->broadcast_rgb_property) {
590 *val = vc4_conn_state->broadcast_rgb;
591 } else if (property == vc4_hdmi->output_format_property) {
592 *val = vc4_conn_state->requested_output_format;
594 DRM_DEBUG_ATOMIC("Unknown property [PROP:%d:%s]\n",
595 property->base.id, property->name);
603 * vc4_hdmi_connector_atomic_set_property - hook for
604 * connector->atomic_set_property.
605 * @connector: Connector to set the property for.
606 * @state: Connector state to set the property on.
607 * @property: Property to set.
608 * @val: New value for the property.
610 * Sets the atomic property value for a digital connector.
612 int vc4_hdmi_connector_set_property(struct drm_connector *connector,
613 struct drm_connector_state *state,
614 struct drm_property *property,
617 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
618 struct vc4_hdmi_connector_state *vc4_conn_state =
619 conn_state_to_vc4_hdmi_conn_state(state);
621 if (property == vc4_hdmi->broadcast_rgb_property) {
622 vc4_conn_state->broadcast_rgb = val;
624 } else if (property == vc4_hdmi->output_format_property) {
625 vc4_conn_state->requested_output_format = val;
629 DRM_DEBUG_ATOMIC("Unknown property [PROP:%d:%s]\n",
630 property->base.id, property->name);
634 static void vc4_hdmi_connector_reset(struct drm_connector *connector)
636 struct vc4_hdmi_connector_state *old_state =
637 conn_state_to_vc4_hdmi_conn_state(connector->state);
638 struct vc4_hdmi_connector_state *new_state =
639 kzalloc(sizeof(*new_state), GFP_KERNEL);
641 if (connector->state)
642 __drm_atomic_helper_connector_destroy_state(connector->state);
645 __drm_atomic_helper_connector_reset(connector, &new_state->base);
650 new_state->base.max_bpc = 8;
651 new_state->base.max_requested_bpc = 8;
652 new_state->output_format = VC4_HDMI_OUTPUT_RGB;
653 drm_atomic_helper_connector_tv_reset(connector);
656 static struct drm_connector_state *
657 vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
659 struct drm_connector_state *conn_state = connector->state;
660 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
661 struct vc4_hdmi_connector_state *new_state;
663 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
667 new_state->pixel_rate = vc4_state->pixel_rate;
668 new_state->output_bpc = vc4_state->output_bpc;
669 new_state->output_format = vc4_state->output_format;
670 new_state->requested_output_format = vc4_state->requested_output_format;
671 new_state->broadcast_rgb = vc4_state->broadcast_rgb;
672 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
674 return &new_state->base;
677 static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
678 .fill_modes = drm_helper_probe_single_connector_modes,
679 .reset = vc4_hdmi_connector_reset,
680 .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
681 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
682 .atomic_get_property = vc4_hdmi_connector_get_property,
683 .atomic_set_property = vc4_hdmi_connector_set_property,
686 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
687 .detect_ctx = vc4_hdmi_connector_detect_ctx,
688 .get_modes = vc4_hdmi_connector_get_modes,
689 .atomic_check = vc4_hdmi_connector_atomic_check,
692 static const struct drm_prop_enum_list broadcast_rgb_names[] = {
693 { VC4_BROADCAST_RGB_AUTO, "Automatic" },
694 { VC4_BROADCAST_RGB_FULL, "Full" },
695 { VC4_BROADCAST_RGB_LIMITED, "Limited 16:235" },
699 vc4_hdmi_attach_broadcast_rgb_property(struct drm_device *dev,
700 struct vc4_hdmi *vc4_hdmi)
702 struct drm_property *prop = vc4_hdmi->broadcast_rgb_property;
705 prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
708 ARRAY_SIZE(broadcast_rgb_names));
712 vc4_hdmi->broadcast_rgb_property = prop;
715 drm_object_attach_property(&vc4_hdmi->connector.base, prop, 0);
718 static const struct drm_prop_enum_list output_format_names[] = {
719 { VC4_HDMI_OUTPUT_AUTO, "Automatic" },
720 { VC4_HDMI_OUTPUT_RGB, "RGB" },
721 { VC4_HDMI_OUTPUT_YUV422, "YCbCr 4:2:2" },
722 { VC4_HDMI_OUTPUT_YUV444, "YCbCr 4:4:4" },
726 vc4_hdmi_attach_output_format_property(struct drm_device *dev,
727 struct vc4_hdmi *vc4_hdmi)
729 struct drm_property *prop = vc4_hdmi->output_format_property;
732 prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
735 ARRAY_SIZE(output_format_names));
739 vc4_hdmi->output_format_property = prop;
742 drm_object_attach_property(&vc4_hdmi->connector.base, prop, 0);
745 static int vc4_hdmi_connector_init(struct drm_device *dev,
746 struct vc4_hdmi *vc4_hdmi)
748 struct drm_connector *connector = &vc4_hdmi->connector;
749 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
752 ret = drmm_connector_init(dev, connector,
753 &vc4_hdmi_connector_funcs,
754 DRM_MODE_CONNECTOR_HDMIA,
759 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
762 * Some of the properties below require access to state, like bpc.
763 * Allocate some default initial connector state with our reset helper.
765 if (connector->funcs->reset)
766 connector->funcs->reset(connector);
768 /* Create and attach TV margin props to this connector. */
769 ret = drm_mode_create_tv_margin_properties(dev);
773 ret = drm_mode_create_hdmi_colorspace_property(connector);
777 drm_connector_attach_colorspace_property(connector);
778 drm_connector_attach_tv_margin_properties(connector);
779 drm_connector_attach_max_bpc_property(connector, 8, 12);
781 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
782 DRM_CONNECTOR_POLL_DISCONNECT);
784 connector->interlace_allowed = 1;
785 connector->doublescan_allowed = 0;
786 connector->stereo_allowed = 1;
788 if (vc4_hdmi->variant->supports_hdr)
789 drm_connector_attach_hdr_output_metadata_property(connector);
791 vc4_hdmi_attach_broadcast_rgb_property(dev, vc4_hdmi);
792 vc4_hdmi_attach_output_format_property(dev, vc4_hdmi);
794 drm_connector_attach_encoder(connector, encoder);
799 static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
800 enum hdmi_infoframe_type type,
803 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
804 struct drm_device *drm = vc4_hdmi->connector.dev;
805 u32 packet_id = type - 0x80;
810 if (!drm_dev_enter(drm, &idx))
813 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
814 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
815 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
816 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
819 ret = wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
820 BIT(packet_id)), 100);
827 static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
828 union hdmi_infoframe *frame)
830 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
831 struct drm_device *drm = vc4_hdmi->connector.dev;
832 u32 packet_id = frame->any.type - 0x80;
833 const struct vc4_hdmi_register *ram_packet_start =
834 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
835 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
836 u32 packet_reg_next = ram_packet_start->offset +
837 VC4_HDMI_PACKET_STRIDE * (packet_id + 1);
838 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
839 ram_packet_start->reg);
840 uint8_t buffer[VC4_HDMI_PACKET_STRIDE] = {};
846 if (!drm_dev_enter(drm, &idx))
849 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
850 VC4_HDMI_RAM_PACKET_ENABLE),
851 "Packet RAM has to be on to store the packet.");
853 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
857 ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
859 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
863 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
865 for (i = 0; i < len; i += 7) {
866 writel(buffer[i + 0] << 0 |
872 writel(buffer[i + 3] << 0 |
874 buffer[i + 5] << 16 |
881 * clear remainder of packet ram as it's included in the
882 * infoframe and triggers a checksum error on hdmi analyser
884 for (; packet_reg < packet_reg_next; packet_reg += 4)
885 writel(0, base + packet_reg);
887 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
888 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
890 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
892 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
893 BIT(packet_id)), 100);
895 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
901 static void vc4_hdmi_avi_infoframe_colorspace(struct hdmi_avi_infoframe *frame,
902 enum vc4_hdmi_output_format fmt)
905 case VC4_HDMI_OUTPUT_RGB:
906 frame->colorspace = HDMI_COLORSPACE_RGB;
909 case VC4_HDMI_OUTPUT_YUV420:
910 frame->colorspace = HDMI_COLORSPACE_YUV420;
913 case VC4_HDMI_OUTPUT_YUV422:
914 frame->colorspace = HDMI_COLORSPACE_YUV422;
917 case VC4_HDMI_OUTPUT_YUV444:
918 frame->colorspace = HDMI_COLORSPACE_YUV444;
926 static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
928 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
929 struct drm_connector *connector = &vc4_hdmi->connector;
930 struct drm_connector_state *cstate = connector->state;
931 struct vc4_hdmi_connector_state *vc4_state =
932 conn_state_to_vc4_hdmi_conn_state(cstate);
933 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
934 union hdmi_infoframe frame;
937 lockdep_assert_held(&vc4_hdmi->mutex);
939 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
942 DRM_ERROR("couldn't fill AVI infoframe\n");
946 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
948 vc4_hdmi_is_full_range(vc4_hdmi, mode) ?
949 HDMI_QUANTIZATION_RANGE_FULL :
950 HDMI_QUANTIZATION_RANGE_LIMITED);
951 drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
952 vc4_hdmi_avi_infoframe_colorspace(&frame.avi, vc4_state->output_format);
953 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
955 vc4_hdmi_write_infoframe(encoder, &frame);
958 static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
960 union hdmi_infoframe frame;
963 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
965 DRM_ERROR("couldn't fill SPD infoframe\n");
969 frame.spd.sdi = HDMI_SPD_SDI_PC;
971 vc4_hdmi_write_infoframe(encoder, &frame);
974 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
976 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
977 struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
978 union hdmi_infoframe frame;
980 memcpy(&frame.audio, audio, sizeof(*audio));
981 vc4_hdmi_write_infoframe(encoder, &frame);
984 static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
986 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
987 struct drm_connector *connector = &vc4_hdmi->connector;
988 struct drm_connector_state *conn_state = connector->state;
989 union hdmi_infoframe frame;
991 lockdep_assert_held(&vc4_hdmi->mutex);
993 if (!vc4_hdmi->variant->supports_hdr)
996 if (!conn_state->hdr_output_metadata)
999 if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
1002 vc4_hdmi_write_infoframe(encoder, &frame);
1005 static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
1007 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1009 lockdep_assert_held(&vc4_hdmi->mutex);
1011 vc4_hdmi_set_avi_infoframe(encoder);
1012 vc4_hdmi_set_spd_infoframe(encoder);
1014 * If audio was streaming, then we need to reenabled the audio
1015 * infoframe here during encoder_enable.
1017 if (vc4_hdmi->audio.streaming)
1018 vc4_hdmi_set_audio_infoframe(encoder);
1020 vc4_hdmi_set_hdr_infoframe(encoder);
1023 #define SCRAMBLING_POLLING_DELAY_MS 1000
1025 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
1027 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1028 struct drm_device *drm = vc4_hdmi->connector.dev;
1029 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1030 unsigned long flags;
1033 lockdep_assert_held(&vc4_hdmi->mutex);
1035 if (!vc4_hdmi_supports_scrambling(encoder))
1038 if (!vc4_hdmi_mode_needs_scrambling(mode,
1039 vc4_hdmi->output_bpc,
1040 vc4_hdmi->output_format))
1043 if (!drm_dev_enter(drm, &idx))
1046 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
1047 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
1049 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1050 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
1051 VC5_HDMI_SCRAMBLER_CTL_ENABLE);
1052 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1056 vc4_hdmi->scdc_enabled = true;
1058 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
1059 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
1062 static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
1064 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1065 struct drm_device *drm = vc4_hdmi->connector.dev;
1066 unsigned long flags;
1069 lockdep_assert_held(&vc4_hdmi->mutex);
1071 if (!vc4_hdmi->scdc_enabled)
1074 vc4_hdmi->scdc_enabled = false;
1076 if (delayed_work_pending(&vc4_hdmi->scrambling_work))
1077 cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
1079 if (!drm_dev_enter(drm, &idx))
1082 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1083 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
1084 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
1085 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1087 drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
1088 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
1093 static void vc4_hdmi_scrambling_wq(struct work_struct *work)
1095 struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
1099 if (drm_scdc_get_scrambling_status(vc4_hdmi->ddc))
1102 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
1103 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
1105 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
1106 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
1109 static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
1110 struct drm_atomic_state *state)
1112 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1113 struct drm_device *drm = vc4_hdmi->connector.dev;
1114 unsigned long flags;
1117 mutex_lock(&vc4_hdmi->mutex);
1119 vc4_hdmi->output_enabled = false;
1121 if (!drm_dev_enter(drm, &idx))
1124 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1126 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
1128 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
1130 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1134 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1135 HDMI_WRITE(HDMI_VID_CTL,
1136 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
1137 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1139 vc4_hdmi_disable_scrambling(encoder);
1144 mutex_unlock(&vc4_hdmi->mutex);
1147 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
1148 struct drm_atomic_state *state)
1150 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1151 struct drm_device *drm = vc4_hdmi->connector.dev;
1152 unsigned long flags;
1156 mutex_lock(&vc4_hdmi->mutex);
1158 if (!drm_dev_enter(drm, &idx))
1161 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1162 HDMI_WRITE(HDMI_VID_CTL,
1163 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
1164 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1166 if (vc4_hdmi->variant->phy_disable)
1167 vc4_hdmi->variant->phy_disable(vc4_hdmi);
1169 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
1170 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1172 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
1174 DRM_ERROR("Failed to release power domain: %d\n", ret);
1179 mutex_unlock(&vc4_hdmi->mutex);
1182 static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1183 struct drm_connector_state *state,
1184 const struct drm_display_mode *mode)
1186 struct drm_device *drm = vc4_hdmi->connector.dev;
1187 unsigned long flags;
1191 if (!drm_dev_enter(drm, &idx))
1194 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1196 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
1197 VC4_HD_CSC_CTL_ORDER);
1199 if (!vc4_hdmi_is_full_range(vc4_hdmi, mode)) {
1200 /* CEA VICs other than #1 requre limited range RGB
1201 * output unless overridden by an AVI infoframe.
1202 * Apply a colorspace conversion to squash 0-255 down
1203 * to 16-235. The matrix here is:
1210 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
1211 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
1212 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1213 VC4_HD_CSC_CTL_MODE);
1215 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
1216 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
1217 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
1218 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
1219 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
1220 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
1223 /* The RGB order applies even when CSC is disabled. */
1224 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1226 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1232 * If we need to output Full Range RGB, then use the unity matrix
1238 * CEA VICs other than #1 require limited range RGB output unless
1239 * overridden by an AVI infoframe. Apply a colorspace conversion to
1240 * squash 0-255 down to 16-235. The matrix here is:
1246 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
1248 static const u16 vc5_hdmi_csc_full_rgb_to_rgb[2][3][4] = {
1250 /* Full range - unity */
1251 { 0x2000, 0x0000, 0x0000, 0x0000 },
1252 { 0x0000, 0x2000, 0x0000, 0x0000 },
1253 { 0x0000, 0x0000, 0x2000, 0x0000 },
1256 { 0x1b80, 0x0000, 0x0000, 0x0400 },
1257 { 0x0000, 0x1b80, 0x0000, 0x0400 },
1258 { 0x0000, 0x0000, 0x1b80, 0x0400 },
1263 * Conversion between Full Range RGB and YUV using the BT.601 Colorspace
1266 * [ 0.299000 0.587000 0.114000 0.000000 ]
1267 * [ -0.168736 -0.331264 0.500000 128.000000 ]
1268 * [ 0.500000 -0.418688 -0.081312 128.000000 ]
1271 * [ 0.255785 0.502160 0.097523 16.000000 ]
1272 * [ -0.147644 -0.289856 0.437500 128.000000 ]
1273 * [ 0.437500 -0.366352 -0.071148 128.000000 ]
1275 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
1277 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt601[2][3][4] = {
1280 { 0x0991, 0x12c9, 0x03a6, 0x0000 },
1281 { 0xfa9b, 0xf567, 0x1000, 0x2000 },
1282 { 0x1000, 0xf29b, 0xfd67, 0x2000 },
1285 { 0x082f, 0x1012, 0x031f, 0x0400 },
1286 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
1287 { 0x0e00, 0xf448, 0xfdba, 0x2000 },
1292 * Conversion between Full Range RGB and YUV using the BT.709 Colorspace
1295 * [ 0.212600 0.715200 0.072200 0.000000 ]
1296 * [ -0.114572 -0.385428 0.500000 128.000000 ]
1297 * [ 0.500000 -0.454153 -0.045847 128.000000 ]
1300 * [ 0.181873 0.611831 0.061765 16.000000 ]
1301 * [ -0.100251 -0.337249 0.437500 128.000000 ]
1302 * [ 0.437500 -0.397384 -0.040116 128.000000 ]
1304 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
1306 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt709[2][3][4] = {
1309 { 0x06ce, 0x16e3, 0x024f, 0x0000 },
1310 { 0xfc56, 0xf3ac, 0x1000, 0x2000 },
1311 { 0x1000, 0xf179, 0xfe89, 0x2000 },
1314 { 0x05d2, 0x1394, 0x01fa, 0x0400 },
1315 { 0xfccc, 0xf536, 0x0e00, 0x2000 },
1316 { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
1321 * Conversion between Full Range RGB and YUV using the BT.2020 Colorspace
1324 * [ 0.262700 0.678000 0.059300 0.000000 ]
1325 * [ -0.139630 -0.360370 0.500000 128.000000 ]
1326 * [ 0.500000 -0.459786 -0.040214 128.000000 ]
1329 * [ 0.224732 0.580008 0.050729 16.000000 ]
1330 * [ -0.122176 -0.315324 0.437500 128.000000 ]
1331 * [ 0.437500 -0.402312 -0.035188 128.000000 ]
1333 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
1335 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt2020[2][3][4] = {
1338 { 0x0868, 0x15b2, 0x01e6, 0x0000 },
1339 { 0xfb89, 0xf479, 0x1000, 0x2000 },
1340 { 0x1000, 0xf14a, 0xfeb8, 0x2000 },
1343 { 0x0731, 0x128f, 0x01a0, 0x0400 },
1344 { 0xfc18, 0xf5ea, 0x0e00, 0x2000 },
1345 { 0x0e00, 0xf321, 0xfee1, 0x2000 },
1349 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
1350 const u16 coeffs[3][4])
1352 lockdep_assert_held(&vc4_hdmi->hw_lock);
1354 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
1355 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
1356 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
1357 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
1358 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
1359 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
1362 static void vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi *vc4_hdmi,
1363 const u16 coeffs[3][4])
1365 lockdep_assert_held(&vc4_hdmi->hw_lock);
1367 /* YUV444 needs the CSC matrices using the channels in a different order */
1368 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[1][1] << 16) | coeffs[1][0]);
1369 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[1][3] << 16) | coeffs[1][2]);
1370 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[2][1] << 16) | coeffs[2][0]);
1371 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[2][3] << 16) | coeffs[2][2]);
1372 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[0][1] << 16) | coeffs[0][0]);
1373 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[0][3] << 16) | coeffs[0][2]);
1376 static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1377 struct drm_connector_state *state,
1378 const struct drm_display_mode *mode)
1380 struct drm_device *drm = vc4_hdmi->connector.dev;
1381 struct vc4_hdmi_connector_state *vc4_state =
1382 conn_state_to_vc4_hdmi_conn_state(state);
1383 unsigned int lim_range = vc4_hdmi_is_full_range(vc4_hdmi, mode) ? 0 : 1;
1384 const u16 (*csc)[4];
1385 unsigned long flags;
1387 u32 if_xbar = 0x543210;
1388 u32 csc_chan_ctl = 0;
1389 u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1390 VC5_MT_CP_CSC_CTL_MODE);
1393 if (!drm_dev_enter(drm, &idx))
1396 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1398 switch (vc4_state->output_format) {
1399 case VC4_HDMI_OUTPUT_YUV444:
1400 case VC4_HDMI_OUTPUT_YUV422:
1401 switch (state->colorspace) {
1403 case DRM_MODE_COLORIMETRY_NO_DATA:
1404 case DRM_MODE_COLORIMETRY_BT709_YCC:
1405 case DRM_MODE_COLORIMETRY_XVYCC_709:
1406 case DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED:
1407 case DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT:
1408 csc = vc5_hdmi_csc_full_rgb_to_yuv_bt709[lim_range];
1410 case DRM_MODE_COLORIMETRY_SMPTE_170M_YCC:
1411 case DRM_MODE_COLORIMETRY_XVYCC_601:
1412 case DRM_MODE_COLORIMETRY_SYCC_601:
1413 case DRM_MODE_COLORIMETRY_OPYCC_601:
1414 case DRM_MODE_COLORIMETRY_BT601_YCC:
1415 csc = vc5_hdmi_csc_full_rgb_to_yuv_bt601[lim_range];
1417 case DRM_MODE_COLORIMETRY_BT2020_CYCC:
1418 case DRM_MODE_COLORIMETRY_BT2020_YCC:
1419 case DRM_MODE_COLORIMETRY_BT2020_RGB:
1420 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
1421 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
1422 csc = vc5_hdmi_csc_full_rgb_to_yuv_bt2020[lim_range];
1426 if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
1427 csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
1428 VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
1429 VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
1430 VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
1432 csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
1433 VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
1435 if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
1436 VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);
1438 vc5_hdmi_set_csc_coeffs(vc4_hdmi, csc);
1440 vc5_hdmi_set_csc_coeffs_swap(vc4_hdmi, csc);
1445 case VC4_HDMI_OUTPUT_RGB:
1448 vc5_hdmi_set_csc_coeffs(vc4_hdmi,
1449 vc5_hdmi_csc_full_rgb_to_rgb[lim_range]);
1456 HDMI_WRITE(HDMI_VEC_INTERFACE_CFG, if_cfg);
1457 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, if_xbar);
1458 HDMI_WRITE(HDMI_CSC_CHANNEL_CTL, csc_chan_ctl);
1459 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1461 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1466 static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1467 struct drm_connector_state *state,
1468 const struct drm_display_mode *mode)
1470 struct drm_device *drm = vc4_hdmi->connector.dev;
1471 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1472 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1473 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1474 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1475 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1476 VC4_HDMI_VERTA_VSP) |
1477 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1478 VC4_HDMI_VERTA_VFP) |
1479 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
1480 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1481 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1483 VC4_HDMI_VERTB_VBP));
1484 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1485 VC4_SET_FIELD(mode->crtc_vtotal -
1486 mode->crtc_vsync_end,
1487 VC4_HDMI_VERTB_VBP));
1488 unsigned long flags;
1492 if (!drm_dev_enter(drm, &idx))
1495 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1497 HDMI_WRITE(HDMI_HORZA,
1498 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
1499 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
1500 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1501 VC4_HDMI_HORZA_HAP));
1503 HDMI_WRITE(HDMI_HORZB,
1504 VC4_SET_FIELD((mode->htotal -
1505 mode->hsync_end) * pixel_rep,
1506 VC4_HDMI_HORZB_HBP) |
1507 VC4_SET_FIELD((mode->hsync_end -
1508 mode->hsync_start) * pixel_rep,
1509 VC4_HDMI_HORZB_HSP) |
1510 VC4_SET_FIELD((mode->hsync_start -
1511 mode->hdisplay) * pixel_rep,
1512 VC4_HDMI_HORZB_HFP));
1514 HDMI_WRITE(HDMI_VERTA0, verta);
1515 HDMI_WRITE(HDMI_VERTA1, verta);
1517 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1518 HDMI_WRITE(HDMI_VERTB1, vertb);
1520 reg = HDMI_READ(HDMI_MISC_CONTROL);
1521 reg &= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1522 reg |= VC4_SET_FIELD(pixel_rep - 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP);
1523 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1525 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1530 static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1531 struct drm_connector_state *state,
1532 const struct drm_display_mode *mode)
1534 struct drm_device *drm = vc4_hdmi->connector.dev;
1535 const struct vc4_hdmi_connector_state *vc4_state =
1536 conn_state_to_vc4_hdmi_conn_state(state);
1537 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1538 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1539 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1540 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1541 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1542 VC5_HDMI_VERTA_VSP) |
1543 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1544 VC5_HDMI_VERTA_VFP) |
1545 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
1546 u32 vertb = (VC4_SET_FIELD(mode->htotal >> (2 - pixel_rep),
1547 VC5_HDMI_VERTB_VSPO) |
1548 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1550 VC4_HDMI_VERTB_VBP));
1551 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
1552 VC4_SET_FIELD(mode->crtc_vtotal -
1553 mode->crtc_vsync_end,
1554 VC4_HDMI_VERTB_VBP));
1555 unsigned long flags;
1560 if (!drm_dev_enter(drm, &idx))
1563 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1565 HDMI_WRITE(HDMI_HORZA,
1566 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
1567 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
1568 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1569 VC5_HDMI_HORZA_HAP) |
1570 VC4_SET_FIELD((mode->hsync_start -
1571 mode->hdisplay) * pixel_rep,
1572 VC5_HDMI_HORZA_HFP));
1574 HDMI_WRITE(HDMI_HORZB,
1575 VC4_SET_FIELD((mode->htotal -
1576 mode->hsync_end) * pixel_rep,
1577 VC5_HDMI_HORZB_HBP) |
1578 VC4_SET_FIELD((mode->hsync_end -
1579 mode->hsync_start) * pixel_rep,
1580 VC5_HDMI_HORZB_HSP));
1582 HDMI_WRITE(HDMI_VERTA0, verta);
1583 HDMI_WRITE(HDMI_VERTA1, verta);
1585 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1586 HDMI_WRITE(HDMI_VERTB1, vertb);
1588 switch (vc4_state->output_bpc) {
1602 * YCC422 is always 36-bit and not considered deep colour so
1603 * doesn't signal in GCP
1605 if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
1609 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
1610 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
1611 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
1612 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
1613 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
1614 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
1616 reg = HDMI_READ(HDMI_GCP_WORD_1);
1617 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
1618 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
1619 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK;
1620 reg |= VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE;
1621 HDMI_WRITE(HDMI_GCP_WORD_1, reg);
1623 reg = HDMI_READ(HDMI_GCP_CONFIG);
1624 reg |= VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
1625 HDMI_WRITE(HDMI_GCP_CONFIG, reg);
1627 reg = HDMI_READ(HDMI_MISC_CONTROL);
1628 reg &= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1629 reg |= VC4_SET_FIELD(pixel_rep - 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP);
1630 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1632 HDMI_WRITE(HDMI_CLOCK_STOP, 0);
1634 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1639 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
1641 struct drm_device *drm = vc4_hdmi->connector.dev;
1642 unsigned long flags;
1647 if (!drm_dev_enter(drm, &idx))
1650 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1652 drift = HDMI_READ(HDMI_FIFO_CTL);
1653 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
1655 HDMI_WRITE(HDMI_FIFO_CTL,
1656 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1657 HDMI_WRITE(HDMI_FIFO_CTL,
1658 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1660 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1662 usleep_range(1000, 1100);
1664 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1666 HDMI_WRITE(HDMI_FIFO_CTL,
1667 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1668 HDMI_WRITE(HDMI_FIFO_CTL,
1669 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1671 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1673 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
1674 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
1675 WARN_ONCE(ret, "Timeout waiting for "
1676 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
1681 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
1682 struct drm_atomic_state *state)
1684 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1685 struct drm_device *drm = vc4_hdmi->connector.dev;
1686 struct drm_connector *connector = &vc4_hdmi->connector;
1687 struct drm_connector_state *conn_state =
1688 drm_atomic_get_new_connector_state(state, connector);
1689 struct vc4_hdmi_connector_state *vc4_conn_state =
1690 conn_state_to_vc4_hdmi_conn_state(conn_state);
1691 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1692 unsigned long pixel_rate = vc4_conn_state->pixel_rate;
1693 unsigned long bvb_rate, hsm_rate;
1694 unsigned long flags;
1698 mutex_lock(&vc4_hdmi->mutex);
1700 if (!drm_dev_enter(drm, &idx))
1704 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
1705 * be faster than pixel clock, infinitesimally faster, tested in
1706 * simulation. Otherwise, exact value is unimportant for HDMI
1707 * operation." This conflicts with bcm2835's vc4 documentation, which
1708 * states HSM's clock has to be at least 108% of the pixel clock.
1710 * Real life tests reveal that vc4's firmware statement holds up, and
1711 * users are able to use pixel clocks closer to HSM's, namely for
1712 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
1713 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
1716 * Additionally, the AXI clock needs to be at least 25% of
1717 * pixel clock, but HSM ends up being the limiting factor.
1719 hsm_rate = max_t(unsigned long, 120000000, (pixel_rate / 100) * 101);
1720 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
1722 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1726 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
1728 DRM_ERROR("Failed to retain power domain: %d\n", ret);
1732 ret = clk_set_rate(vc4_hdmi->pixel_clock, pixel_rate);
1734 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
1735 goto err_put_runtime_pm;
1738 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
1740 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
1741 goto err_put_runtime_pm;
1745 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
1747 if (pixel_rate > 297000000)
1748 bvb_rate = 300000000;
1749 else if (pixel_rate > 148500000)
1750 bvb_rate = 150000000;
1752 bvb_rate = 75000000;
1754 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
1756 DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
1757 goto err_disable_pixel_clock;
1760 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
1762 DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
1763 goto err_disable_pixel_clock;
1766 if (vc4_hdmi->variant->phy_init)
1767 vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
1769 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1771 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1772 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1773 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
1774 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
1776 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1778 if (vc4_hdmi->variant->set_timings)
1779 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
1783 mutex_unlock(&vc4_hdmi->mutex);
1787 err_disable_pixel_clock:
1788 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1790 pm_runtime_put(&vc4_hdmi->pdev->dev);
1794 mutex_unlock(&vc4_hdmi->mutex);
1798 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
1799 struct drm_atomic_state *state)
1801 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1802 struct drm_device *drm = vc4_hdmi->connector.dev;
1803 struct drm_connector *connector = &vc4_hdmi->connector;
1804 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1805 struct drm_connector_state *conn_state =
1806 drm_atomic_get_new_connector_state(state, connector);
1807 unsigned long flags;
1810 mutex_lock(&vc4_hdmi->mutex);
1812 if (!drm_dev_enter(drm, &idx))
1815 if (vc4_hdmi->variant->csc_setup)
1816 vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
1818 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1819 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
1820 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1824 mutex_unlock(&vc4_hdmi->mutex);
1827 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
1828 struct drm_atomic_state *state)
1830 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1831 struct drm_device *drm = vc4_hdmi->connector.dev;
1832 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1833 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
1834 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1835 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1836 unsigned long flags;
1840 mutex_lock(&vc4_hdmi->mutex);
1842 if (!drm_dev_enter(drm, &idx))
1845 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1847 HDMI_WRITE(HDMI_VID_CTL,
1848 VC4_HD_VID_CTL_ENABLE |
1849 VC4_HD_VID_CTL_CLRRGB |
1850 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
1851 VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
1852 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
1853 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
1855 HDMI_WRITE(HDMI_VID_CTL,
1856 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1858 if (display->is_hdmi) {
1859 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1860 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1861 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1863 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1865 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1866 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1867 WARN_ONCE(ret, "Timeout waiting for "
1868 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1870 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1871 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
1872 ~(VC4_HDMI_RAM_PACKET_ENABLE));
1873 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1874 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1875 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1877 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1879 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1880 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1881 WARN_ONCE(ret, "Timeout waiting for "
1882 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1885 if (display->is_hdmi) {
1886 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1888 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1889 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
1891 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1892 VC4_HDMI_RAM_PACKET_ENABLE);
1894 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1895 vc4_hdmi->output_enabled = true;
1897 vc4_hdmi_set_infoframes(encoder);
1900 vc4_hdmi_recenter_fifo(vc4_hdmi);
1901 vc4_hdmi_enable_scrambling(encoder);
1904 mutex_unlock(&vc4_hdmi->mutex);
1907 static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
1908 struct drm_crtc_state *crtc_state,
1909 struct drm_connector_state *conn_state)
1911 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1912 struct vc4_hdmi_connector_state *vc4_state =
1913 conn_state_to_vc4_hdmi_conn_state(conn_state);
1915 mutex_lock(&vc4_hdmi->mutex);
1916 vc4_hdmi->output_bpc = vc4_state->output_bpc;
1917 vc4_hdmi->output_format = vc4_state->output_format;
1918 vc4_hdmi->requested_output_format = vc4_state->requested_output_format;
1919 vc4_hdmi->broadcast_rgb = vc4_state->broadcast_rgb;
1920 memcpy(&vc4_hdmi->saved_adjusted_mode,
1921 &crtc_state->adjusted_mode,
1922 sizeof(vc4_hdmi->saved_adjusted_mode));
1923 mutex_unlock(&vc4_hdmi->mutex);
1927 vc4_hdmi_sink_supports_format_bpc(const struct vc4_hdmi *vc4_hdmi,
1928 const struct drm_display_info *info,
1929 const struct drm_display_mode *mode,
1930 unsigned int format, unsigned int bpc)
1932 struct drm_device *dev = vc4_hdmi->connector.dev;
1933 u8 vic = drm_match_cea_mode(mode);
1935 if (vic == 1 && bpc != 8) {
1936 drm_dbg(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
1940 if (!info->is_hdmi &&
1941 (format != VC4_HDMI_OUTPUT_RGB || bpc != 8)) {
1942 drm_dbg(dev, "DVI Monitors require an RGB output at 8 bpc\n");
1947 case VC4_HDMI_OUTPUT_RGB:
1948 drm_dbg(dev, "RGB Format, checking the constraints.\n");
1950 if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
1951 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
1955 if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
1956 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
1960 drm_dbg(dev, "RGB format supported in that configuration.\n");
1964 case VC4_HDMI_OUTPUT_YUV422:
1965 drm_dbg(dev, "YUV422 format, checking the constraints.\n");
1967 if (!(info->color_formats & DRM_COLOR_FORMAT_YCRCB422)) {
1968 drm_dbg(dev, "Sink doesn't support YUV422.\n");
1973 drm_dbg(dev, "YUV422 only supports 12 bpc.\n");
1977 drm_dbg(dev, "YUV422 format supported in that configuration.\n");
1981 case VC4_HDMI_OUTPUT_YUV444:
1982 drm_dbg(dev, "YUV444 format, checking the constraints.\n");
1984 if (!(info->color_formats & DRM_COLOR_FORMAT_YCRCB444)) {
1985 drm_dbg(dev, "Sink doesn't support YUV444.\n");
1989 if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
1990 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
1994 if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
1995 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
1999 drm_dbg(dev, "YUV444 format supported in that configuration.\n");
2007 static enum drm_mode_status
2008 vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi *vc4_hdmi,
2009 const struct drm_display_mode *mode,
2010 unsigned long long clock)
2012 const struct drm_connector *connector = &vc4_hdmi->connector;
2013 const struct drm_display_info *info = &connector->display_info;
2014 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
2016 if (clock > vc4_hdmi->variant->max_pixel_clock)
2017 return MODE_CLOCK_HIGH;
2019 if (!vc4->hvs->vc5_hdmi_enable_hdmi_20 && clock > HDMI_14_MAX_TMDS_CLK)
2020 return MODE_CLOCK_HIGH;
2022 /* 4096x2160@60 is not reliable without overclocking core */
2023 if (!vc4->hvs->vc5_hdmi_enable_4096by2160 &&
2024 mode->hdisplay > 3840 && mode->vdisplay >= 2160 &&
2025 drm_mode_vrefresh(mode) >= 50)
2026 return MODE_CLOCK_HIGH;
2028 if (info->max_tmds_clock && clock > (info->max_tmds_clock * 1000))
2029 return MODE_CLOCK_HIGH;
2034 static unsigned long long
2035 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
2037 enum vc4_hdmi_output_format fmt)
2039 unsigned long long clock = mode->clock * 1000;
2041 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
2044 if (fmt == VC4_HDMI_OUTPUT_YUV422)
2047 return clock * bpc / 8;
2051 vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi *vc4_hdmi,
2052 struct vc4_hdmi_connector_state *vc4_state,
2053 const struct drm_display_mode *mode,
2054 unsigned int bpc, unsigned int fmt)
2056 unsigned long long clock;
2058 clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
2059 if (vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode, clock) != MODE_OK)
2062 vc4_state->pixel_rate = clock;
2068 vc4_hdmi_encoder_compute_format(const struct vc4_hdmi *vc4_hdmi,
2069 struct vc4_hdmi_connector_state *vc4_state,
2070 const struct drm_display_mode *mode,
2073 struct drm_device *dev = vc4_hdmi->connector.dev;
2074 const struct drm_connector *connector = &vc4_hdmi->connector;
2075 const struct drm_display_info *info = &connector->display_info;
2076 unsigned int format;
2078 if (vc4_state->requested_output_format != VC4_HDMI_OUTPUT_AUTO) {
2079 drm_dbg(dev, "Trying with user requested output %u\n",
2080 vc4_state->requested_output_format);
2082 format = vc4_state->requested_output_format;
2083 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode,
2087 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
2090 vc4_state->output_format = format;
2098 drm_dbg(dev, "Trying with an RGB output\n");
2100 format = VC4_HDMI_OUTPUT_RGB;
2101 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
2104 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
2107 vc4_state->output_format = format;
2112 drm_dbg(dev, "Failed, Trying with an YUV422 output\n");
2114 format = VC4_HDMI_OUTPUT_YUV422;
2115 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
2118 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
2121 vc4_state->output_format = format;
2126 drm_dbg(dev, "Failed. No Format Supported for that bpc count.\n");
2132 vc4_hdmi_encoder_compute_config(const struct vc4_hdmi *vc4_hdmi,
2133 struct vc4_hdmi_connector_state *vc4_state,
2134 const struct drm_display_mode *mode)
2136 struct drm_device *dev = vc4_hdmi->connector.dev;
2137 struct drm_connector_state *conn_state = &vc4_state->base;
2138 unsigned int max_bpc = clamp_t(unsigned int, conn_state->max_requested_bpc, 8, 12);
2142 for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
2143 drm_dbg(dev, "Trying with a %d bpc output\n", bpc);
2145 ret = vc4_hdmi_encoder_compute_format(vc4_hdmi, vc4_state,
2150 vc4_state->output_bpc = bpc;
2153 "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
2154 mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
2155 vc4_state->output_bpc,
2156 vc4_hdmi_output_fmt_str(vc4_state->output_format),
2157 vc4_state->pixel_rate);
2165 #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
2166 #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
2168 static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
2169 struct drm_crtc_state *crtc_state,
2170 struct drm_connector_state *conn_state)
2172 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
2173 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
2174 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2175 struct drm_connector *connector = &vc4_hdmi->connector;
2176 struct drm_connector_state *old_conn_state = drm_atomic_get_old_connector_state(conn_state->state, connector);
2177 struct vc4_hdmi_connector_state *old_vc4_state = conn_state_to_vc4_hdmi_conn_state(old_conn_state);
2178 unsigned long long pixel_rate = mode->clock * 1000;
2179 unsigned long long tmds_rate;
2182 if (vc4_hdmi->variant->unsupported_odd_h_timings) {
2183 if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
2184 /* Only try to fixup DBLCLK modes to get 480i and 576i
2186 * A generic solution for all modes with odd horizontal
2187 * timing values seems impossible based on trying to
2188 * solve it for 1366x768 monitors.
2190 if ((mode->hsync_start - mode->hdisplay) & 1)
2191 mode->hsync_start--;
2192 if ((mode->hsync_end - mode->hsync_start) & 1)
2196 /* Now check whether we still have odd values remaining */
2197 if ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
2198 (mode->hsync_end % 2) || (mode->htotal % 2))
2203 * The 1440p@60 pixel rate is in the same range than the first
2204 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
2205 * bandwidth). Slightly lower the frequency to bring it out of
2208 tmds_rate = pixel_rate * 10;
2209 if (vc4_hdmi->disable_wifi_frequencies &&
2210 (tmds_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
2211 tmds_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
2212 mode->clock = 238560;
2213 pixel_rate = mode->clock * 1000;
2216 ret = vc4_hdmi_encoder_compute_config(vc4_hdmi, vc4_state, mode);
2220 /* vc4_hdmi_encoder_compute_config may have changed output_bpc and/or output_format */
2221 if (vc4_state->output_bpc != old_vc4_state->output_bpc ||
2222 vc4_state->output_format != old_vc4_state->output_format)
2223 crtc_state->mode_changed = true;
2228 static enum drm_mode_status
2229 vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
2230 const struct drm_display_mode *mode)
2232 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2234 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
2235 !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
2236 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
2237 (mode->hsync_end % 2) || (mode->htotal % 2)))
2238 return MODE_H_ILLEGAL;
2240 return vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode, mode->clock * 1000);
2243 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
2244 .atomic_check = vc4_hdmi_encoder_atomic_check,
2245 .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
2246 .mode_valid = vc4_hdmi_encoder_mode_valid,
2249 static int vc4_hdmi_late_register(struct drm_encoder *encoder)
2251 struct drm_device *drm = encoder->dev;
2252 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2253 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
2256 ret = vc4_debugfs_add_file(drm->primary, variant->debugfs_name,
2257 vc4_hdmi_debugfs_regs,
2265 static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
2266 .late_register = vc4_hdmi_late_register,
2269 static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
2272 u32 channel_map = 0;
2274 for (i = 0; i < 8; i++) {
2275 if (channel_mask & BIT(i))
2276 channel_map |= i << (3 * i);
2281 static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
2284 u32 channel_map = 0;
2286 for (i = 0; i < 8; i++) {
2287 if (channel_mask & BIT(i))
2288 channel_map |= i << (4 * i);
2293 static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi)
2295 struct drm_device *drm = vc4_hdmi->connector.dev;
2296 unsigned long flags;
2300 if (!drm_dev_enter(drm, &idx))
2303 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2304 hotplug = HDMI_READ(HDMI_HOTPLUG);
2305 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2309 return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED);
2312 /* HDMI audio codec callbacks */
2313 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
2314 unsigned int samplerate)
2316 struct drm_device *drm = vc4_hdmi->connector.dev;
2318 unsigned long flags;
2322 if (!drm_dev_enter(drm, &idx))
2325 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
2326 rational_best_approximation(hsm_clock, samplerate,
2327 VC4_HD_MAI_SMP_N_MASK >>
2328 VC4_HD_MAI_SMP_N_SHIFT,
2329 (VC4_HD_MAI_SMP_M_MASK >>
2330 VC4_HD_MAI_SMP_M_SHIFT) + 1,
2333 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2334 HDMI_WRITE(HDMI_MAI_SMP,
2335 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
2336 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
2337 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2342 static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
2344 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
2348 lockdep_assert_held(&vc4_hdmi->mutex);
2349 lockdep_assert_held(&vc4_hdmi->hw_lock);
2351 n = 128 * samplerate / 1000;
2352 tmp = (u64)(mode->clock * 1000) * n;
2353 do_div(tmp, 128 * samplerate);
2356 HDMI_WRITE(HDMI_CRP_CFG,
2357 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
2358 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
2361 * We could get slightly more accurate clocks in some cases by
2362 * providing a CTS_1 value. The two CTS values are alternated
2363 * between based on the period fields
2365 HDMI_WRITE(HDMI_CTS_0, cts);
2366 HDMI_WRITE(HDMI_CTS_1, cts);
2369 static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
2371 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
2373 return snd_soc_card_get_drvdata(card);
2376 static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
2378 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
2380 lockdep_assert_held(&vc4_hdmi->mutex);
2383 * If the encoder is currently in DVI mode, treat the codec DAI
2386 if (!display->is_hdmi)
2392 static int vc4_hdmi_audio_startup(struct device *dev, void *data)
2394 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2395 struct drm_device *drm = vc4_hdmi->connector.dev;
2396 unsigned long flags;
2400 mutex_lock(&vc4_hdmi->mutex);
2402 if (!drm_dev_enter(drm, &idx)) {
2407 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2412 vc4_hdmi->audio.streaming = true;
2414 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2415 HDMI_WRITE(HDMI_MAI_CTL,
2416 VC4_HD_MAI_CTL_RESET |
2417 VC4_HD_MAI_CTL_FLUSH |
2418 VC4_HD_MAI_CTL_DLATE |
2419 VC4_HD_MAI_CTL_ERRORE |
2420 VC4_HD_MAI_CTL_ERRORF);
2421 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2423 if (vc4_hdmi->variant->phy_rng_enable)
2424 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
2429 mutex_unlock(&vc4_hdmi->mutex);
2434 static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
2436 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2437 struct device *dev = &vc4_hdmi->pdev->dev;
2438 unsigned long flags;
2441 lockdep_assert_held(&vc4_hdmi->mutex);
2443 vc4_hdmi->audio.streaming = false;
2444 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
2446 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
2448 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2450 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
2451 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
2452 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
2454 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2457 static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
2459 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2460 struct drm_device *drm = vc4_hdmi->connector.dev;
2461 unsigned long flags;
2464 mutex_lock(&vc4_hdmi->mutex);
2466 if (!drm_dev_enter(drm, &idx))
2469 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2471 HDMI_WRITE(HDMI_MAI_CTL,
2472 VC4_HD_MAI_CTL_DLATE |
2473 VC4_HD_MAI_CTL_ERRORE |
2474 VC4_HD_MAI_CTL_ERRORF);
2476 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2478 if (vc4_hdmi->variant->phy_rng_disable)
2479 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
2481 vc4_hdmi->audio.streaming = false;
2482 vc4_hdmi_audio_reset(vc4_hdmi);
2487 mutex_unlock(&vc4_hdmi->mutex);
2490 static int sample_rate_to_mai_fmt(int samplerate)
2492 switch (samplerate) {
2494 return VC4_HDMI_MAI_SAMPLE_RATE_8000;
2496 return VC4_HDMI_MAI_SAMPLE_RATE_11025;
2498 return VC4_HDMI_MAI_SAMPLE_RATE_12000;
2500 return VC4_HDMI_MAI_SAMPLE_RATE_16000;
2502 return VC4_HDMI_MAI_SAMPLE_RATE_22050;
2504 return VC4_HDMI_MAI_SAMPLE_RATE_24000;
2506 return VC4_HDMI_MAI_SAMPLE_RATE_32000;
2508 return VC4_HDMI_MAI_SAMPLE_RATE_44100;
2510 return VC4_HDMI_MAI_SAMPLE_RATE_48000;
2512 return VC4_HDMI_MAI_SAMPLE_RATE_64000;
2514 return VC4_HDMI_MAI_SAMPLE_RATE_88200;
2516 return VC4_HDMI_MAI_SAMPLE_RATE_96000;
2518 return VC4_HDMI_MAI_SAMPLE_RATE_128000;
2520 return VC4_HDMI_MAI_SAMPLE_RATE_176400;
2522 return VC4_HDMI_MAI_SAMPLE_RATE_192000;
2524 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
2528 /* HDMI audio codec callbacks */
2529 static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
2530 struct hdmi_codec_daifmt *daifmt,
2531 struct hdmi_codec_params *params)
2533 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2534 struct drm_device *drm = vc4_hdmi->connector.dev;
2535 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2536 unsigned int sample_rate = params->sample_rate;
2537 unsigned int channels = params->channels;
2538 unsigned long flags;
2539 u32 audio_packet_config, channel_mask;
2541 u32 mai_audio_format;
2542 u32 mai_sample_rate;
2546 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
2547 sample_rate, params->sample_width, channels);
2549 mutex_lock(&vc4_hdmi->mutex);
2551 if (!drm_dev_enter(drm, &idx)) {
2556 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2561 vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
2563 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2564 HDMI_WRITE(HDMI_MAI_CTL,
2565 VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
2566 VC4_HD_MAI_CTL_WHOLSMP |
2567 VC4_HD_MAI_CTL_CHALIGN |
2568 VC4_HD_MAI_CTL_ENABLE);
2570 mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
2571 if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
2572 params->channels == 8)
2573 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
2575 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
2576 HDMI_WRITE(HDMI_MAI_FMT,
2577 VC4_SET_FIELD(mai_sample_rate,
2578 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
2579 VC4_SET_FIELD(mai_audio_format,
2580 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));
2582 /* The B frame identifier should match the value used by alsa-lib (8) */
2583 audio_packet_config =
2584 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
2585 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
2586 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
2588 channel_mask = GENMASK(channels - 1, 0);
2589 audio_packet_config |= VC4_SET_FIELD(channel_mask,
2590 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
2592 /* Set the MAI threshold */
2593 HDMI_WRITE(HDMI_MAI_THR,
2594 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICHIGH) |
2595 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICLOW) |
2596 VC4_SET_FIELD(0x06, VC4_HD_MAI_THR_DREQHIGH) |
2597 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_DREQLOW));
2599 HDMI_WRITE(HDMI_MAI_CONFIG,
2600 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
2601 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
2602 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
2604 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
2605 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
2606 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
2608 vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
2610 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2612 memcpy(&vc4_hdmi->audio.infoframe, ¶ms->cea, sizeof(params->cea));
2613 if (vc4_hdmi->output_enabled)
2614 vc4_hdmi_set_audio_infoframe(encoder);
2619 mutex_unlock(&vc4_hdmi->mutex);
2624 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
2625 .name = "vc4-hdmi-cpu-dai-component",
2628 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
2630 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
2632 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
2637 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
2638 .name = "vc4-hdmi-cpu-dai",
2639 .probe = vc4_hdmi_audio_cpu_dai_probe,
2641 .stream_name = "Playback",
2644 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
2645 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
2646 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
2647 SNDRV_PCM_RATE_192000,
2648 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
2652 static const struct snd_dmaengine_pcm_config pcm_conf = {
2653 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
2654 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
2657 static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
2658 uint8_t *buf, size_t len)
2660 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2661 struct drm_connector *connector = &vc4_hdmi->connector;
2663 mutex_lock(&vc4_hdmi->mutex);
2664 memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
2665 mutex_unlock(&vc4_hdmi->mutex);
2670 static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
2671 .get_eld = vc4_hdmi_audio_get_eld,
2672 .prepare = vc4_hdmi_audio_prepare,
2673 .audio_shutdown = vc4_hdmi_audio_shutdown,
2674 .audio_startup = vc4_hdmi_audio_startup,
2677 static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
2678 .ops = &vc4_hdmi_codec_ops,
2679 .max_i2s_channels = 8,
2683 static void vc4_hdmi_audio_codec_release(void *ptr)
2685 struct vc4_hdmi *vc4_hdmi = ptr;
2687 platform_device_unregister(vc4_hdmi->audio.codec_pdev);
2688 vc4_hdmi->audio.codec_pdev = NULL;
2691 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
2693 const struct vc4_hdmi_register *mai_data =
2694 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
2695 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
2696 struct snd_soc_card *card = &vc4_hdmi->audio.card;
2697 struct device *dev = &vc4_hdmi->pdev->dev;
2698 struct platform_device *codec_pdev;
2705 * ASoC makes it a bit hard to retrieve a pointer to the
2706 * vc4_hdmi structure. Registering the card will overwrite our
2707 * device drvdata with a pointer to the snd_soc_card structure,
2708 * which can then be used to retrieve whatever drvdata we want
2711 * However, that doesn't fly in the case where we wouldn't
2712 * register an ASoC card (because of an old DT that is missing
2713 * the dmas properties for example), then the card isn't
2714 * registered and the device drvdata wouldn't be set.
2716 * We can deal with both cases by making sure a snd_soc_card
2717 * pointer and a vc4_hdmi structure are pointing to the same
2718 * memory address, so we can treat them indistinctly without any
2721 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
2722 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
2724 if (!of_find_property(dev->of_node, "dmas", &len) ||
2727 "'dmas' DT property is missing or empty, no HDMI audio\n");
2731 if (mai_data->reg != VC4_HD) {
2732 WARN_ONCE(true, "MAI isn't in the HD block\n");
2737 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
2738 * the bus address specified in the DT, because the physical address
2739 * (the one returned by platform_get_resource()) is not appropriate
2740 * for DMA transfers.
2741 * This VC/MMU should probably be exposed to avoid this kind of hacks.
2743 index = of_property_match_string(dev->of_node, "reg-names", "hd");
2744 /* Before BCM2711, we don't have a named register range */
2748 addr = of_get_address(dev->of_node, index, NULL, NULL);
2750 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
2751 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2752 vc4_hdmi->audio.dma_data.maxburst = 2;
2755 * NOTE: Strictly speaking, we should probably use a DRM-managed
2756 * registration there to avoid removing all the audio components
2757 * by the time the driver doesn't have any user anymore.
2759 * However, the ASoC core uses a number of devm_kzalloc calls
2760 * when registering, even when using non-device-managed
2761 * functions (such as in snd_soc_register_component()).
2763 * If we call snd_soc_unregister_component() in a DRM-managed
2764 * action, the device-managed actions have already been executed
2765 * and thus we would access memory that has been freed.
2767 * Using device-managed hooks here probably leaves us open to a
2768 * bunch of issues if userspace still has a handle on the ALSA
2769 * device when the device is removed. However, this is mitigated
2770 * by the use of drm_dev_enter()/drm_dev_exit() in the audio
2771 * path to prevent the access to the device resources if it
2772 * isn't there anymore.
2774 * Then, the vc4_hdmi structure is DRM-managed and thus only
2775 * freed whenever the last user has closed the DRM device file.
2776 * It should thus outlive ALSA in most situations.
2778 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
2780 dev_err(dev, "Could not register PCM component: %d\n", ret);
2784 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
2785 &vc4_hdmi_audio_cpu_dai_drv, 1);
2787 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
2791 codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
2792 PLATFORM_DEVID_AUTO,
2793 &vc4_hdmi_codec_pdata,
2794 sizeof(vc4_hdmi_codec_pdata));
2795 if (IS_ERR(codec_pdev)) {
2796 dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
2797 return PTR_ERR(codec_pdev);
2799 vc4_hdmi->audio.codec_pdev = codec_pdev;
2801 ret = devm_add_action_or_reset(dev, vc4_hdmi_audio_codec_release, vc4_hdmi);
2805 dai_link->cpus = &vc4_hdmi->audio.cpu;
2806 dai_link->codecs = &vc4_hdmi->audio.codec;
2807 dai_link->platforms = &vc4_hdmi->audio.platform;
2809 dai_link->num_cpus = 1;
2810 dai_link->num_codecs = 1;
2811 dai_link->num_platforms = 1;
2813 dai_link->name = "MAI";
2814 dai_link->stream_name = "MAI PCM";
2815 dai_link->codecs->dai_name = "i2s-hifi";
2816 dai_link->cpus->dai_name = dev_name(dev);
2817 dai_link->codecs->name = dev_name(&codec_pdev->dev);
2818 dai_link->platforms->name = dev_name(dev);
2820 card->dai_link = dai_link;
2821 card->num_links = 1;
2822 card->name = vc4_hdmi->variant->card_name;
2823 card->driver_name = "vc4-hdmi";
2825 card->owner = THIS_MODULE;
2828 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
2829 * stores a pointer to the snd card object in dev->driver_data. This
2830 * means we cannot use it for something else. The hdmi back-pointer is
2831 * now stored in card->drvdata and should be retrieved with
2832 * snd_soc_card_get_drvdata() if needed.
2834 snd_soc_card_set_drvdata(card, vc4_hdmi);
2835 ret = devm_snd_soc_register_card(dev, card);
2837 dev_err_probe(dev, ret, "Could not register sound card\n");
2843 static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
2845 struct vc4_hdmi *vc4_hdmi = priv;
2846 struct drm_connector *connector = &vc4_hdmi->connector;
2847 struct drm_device *dev = connector->dev;
2849 if (dev && dev->registered)
2850 drm_connector_helper_hpd_irq_event(connector);
2855 static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
2857 struct drm_connector *connector = &vc4_hdmi->connector;
2858 struct platform_device *pdev = vc4_hdmi->pdev;
2861 if (vc4_hdmi->variant->external_irq_controller) {
2862 unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
2863 unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
2865 ret = devm_request_threaded_irq(&pdev->dev, hpd_con,
2867 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2868 "vc4 hdmi hpd connected", vc4_hdmi);
2872 ret = devm_request_threaded_irq(&pdev->dev, hpd_rm,
2874 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2875 "vc4 hdmi hpd disconnected", vc4_hdmi);
2879 connector->polled = DRM_CONNECTOR_POLL_HPD;
2885 #ifdef CONFIG_DRM_VC4_HDMI_CEC
2886 static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
2888 struct vc4_hdmi *vc4_hdmi = priv;
2890 if (vc4_hdmi->cec_rx_msg.len)
2891 cec_received_msg(vc4_hdmi->cec_adap,
2892 &vc4_hdmi->cec_rx_msg);
2897 static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
2899 struct vc4_hdmi *vc4_hdmi = priv;
2901 if (vc4_hdmi->cec_tx_ok) {
2902 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
2906 * This CEC implementation makes 1 retry, so if we
2907 * get a NACK, then that means it made 2 attempts.
2909 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
2915 static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
2917 struct vc4_hdmi *vc4_hdmi = priv;
2920 if (vc4_hdmi->cec_irq_was_rx)
2921 ret = vc4_cec_irq_handler_rx_thread(irq, priv);
2923 ret = vc4_cec_irq_handler_tx_thread(irq, priv);
2928 static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
2930 struct drm_device *dev = vc4_hdmi->connector.dev;
2931 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
2934 lockdep_assert_held(&vc4_hdmi->hw_lock);
2936 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
2937 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
2939 if (msg->len > 16) {
2940 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
2944 for (i = 0; i < msg->len; i += 4) {
2945 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
2947 msg->msg[i] = val & 0xff;
2948 msg->msg[i + 1] = (val >> 8) & 0xff;
2949 msg->msg[i + 2] = (val >> 16) & 0xff;
2950 msg->msg[i + 3] = (val >> 24) & 0xff;
2954 static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2959 * We don't need to protect the register access using
2960 * drm_dev_enter() there because the interrupt handler lifetime
2961 * is tied to the device itself, and not to the DRM device.
2963 * So when the device will be gone, one of the first thing we
2964 * will be doing will be to unregister the interrupt handler,
2965 * and then unregister the DRM device. drm_dev_enter() would
2966 * thus always succeed if we are here.
2969 lockdep_assert_held(&vc4_hdmi->hw_lock);
2971 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
2972 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
2973 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
2974 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
2976 return IRQ_WAKE_THREAD;
2979 static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
2981 struct vc4_hdmi *vc4_hdmi = priv;
2984 spin_lock(&vc4_hdmi->hw_lock);
2985 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
2986 spin_unlock(&vc4_hdmi->hw_lock);
2991 static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2995 lockdep_assert_held(&vc4_hdmi->hw_lock);
2998 * We don't need to protect the register access using
2999 * drm_dev_enter() there because the interrupt handler lifetime
3000 * is tied to the device itself, and not to the DRM device.
3002 * So when the device will be gone, one of the first thing we
3003 * will be doing will be to unregister the interrupt handler,
3004 * and then unregister the DRM device. drm_dev_enter() would
3005 * thus always succeed if we are here.
3008 vc4_hdmi->cec_rx_msg.len = 0;
3009 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
3010 vc4_cec_read_msg(vc4_hdmi, cntrl1);
3011 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
3012 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
3013 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
3015 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
3017 return IRQ_WAKE_THREAD;
3020 static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
3022 struct vc4_hdmi *vc4_hdmi = priv;
3025 spin_lock(&vc4_hdmi->hw_lock);
3026 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
3027 spin_unlock(&vc4_hdmi->hw_lock);
3032 static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
3034 struct vc4_hdmi *vc4_hdmi = priv;
3035 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
3040 * We don't need to protect the register access using
3041 * drm_dev_enter() there because the interrupt handler lifetime
3042 * is tied to the device itself, and not to the DRM device.
3044 * So when the device will be gone, one of the first thing we
3045 * will be doing will be to unregister the interrupt handler,
3046 * and then unregister the DRM device. drm_dev_enter() would
3047 * thus always succeed if we are here.
3050 if (!(stat & VC4_HDMI_CPU_CEC))
3053 spin_lock(&vc4_hdmi->hw_lock);
3054 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
3055 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
3056 if (vc4_hdmi->cec_irq_was_rx)
3057 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
3059 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
3061 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
3062 spin_unlock(&vc4_hdmi->hw_lock);
3067 static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
3069 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3070 struct drm_device *drm = vc4_hdmi->connector.dev;
3071 /* clock period in microseconds */
3072 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
3073 unsigned long flags;
3078 if (!drm_dev_enter(drm, &idx))
3080 * We can't return an error code, because the CEC
3081 * framework will emit WARN_ON messages at unbind
3086 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
3092 mutex_lock(&vc4_hdmi->mutex);
3094 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3096 val = HDMI_READ(HDMI_CEC_CNTRL_5);
3097 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
3098 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
3099 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
3100 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
3101 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
3103 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
3104 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
3105 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
3106 HDMI_WRITE(HDMI_CEC_CNTRL_2,
3107 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
3108 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
3109 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
3110 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
3111 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
3112 HDMI_WRITE(HDMI_CEC_CNTRL_3,
3113 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
3114 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
3115 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
3116 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
3117 HDMI_WRITE(HDMI_CEC_CNTRL_4,
3118 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
3119 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
3120 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
3121 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
3123 if (!vc4_hdmi->variant->external_irq_controller)
3124 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
3126 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3128 mutex_unlock(&vc4_hdmi->mutex);
3134 static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
3136 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3137 struct drm_device *drm = vc4_hdmi->connector.dev;
3138 unsigned long flags;
3141 if (!drm_dev_enter(drm, &idx))
3143 * We can't return an error code, because the CEC
3144 * framework will emit WARN_ON messages at unbind
3149 mutex_lock(&vc4_hdmi->mutex);
3151 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3153 if (!vc4_hdmi->variant->external_irq_controller)
3154 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
3156 HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
3157 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
3159 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3161 mutex_unlock(&vc4_hdmi->mutex);
3163 pm_runtime_put(&vc4_hdmi->pdev->dev);
3170 static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
3173 return vc4_hdmi_cec_enable(adap);
3175 return vc4_hdmi_cec_disable(adap);
3178 static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
3180 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3181 struct drm_device *drm = vc4_hdmi->connector.dev;
3182 unsigned long flags;
3185 if (!drm_dev_enter(drm, &idx))
3187 * We can't return an error code, because the CEC
3188 * framework will emit WARN_ON messages at unbind
3193 mutex_lock(&vc4_hdmi->mutex);
3194 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3195 HDMI_WRITE(HDMI_CEC_CNTRL_1,
3196 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
3197 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
3198 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3199 mutex_unlock(&vc4_hdmi->mutex);
3206 static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
3207 u32 signal_free_time, struct cec_msg *msg)
3209 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3210 struct drm_device *dev = vc4_hdmi->connector.dev;
3211 unsigned long flags;
3216 if (!drm_dev_enter(dev, &idx))
3219 if (msg->len > 16) {
3220 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
3225 mutex_lock(&vc4_hdmi->mutex);
3227 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3229 for (i = 0; i < msg->len; i += 4)
3230 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
3232 (msg->msg[i + 1] << 8) |
3233 (msg->msg[i + 2] << 16) |
3234 (msg->msg[i + 3] << 24));
3236 val = HDMI_READ(HDMI_CEC_CNTRL_1);
3237 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
3238 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
3239 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
3240 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
3241 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
3243 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
3245 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3246 mutex_unlock(&vc4_hdmi->mutex);
3252 static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
3253 .adap_enable = vc4_hdmi_cec_adap_enable,
3254 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
3255 .adap_transmit = vc4_hdmi_cec_adap_transmit,
3258 static void vc4_hdmi_cec_release(void *ptr)
3260 struct vc4_hdmi *vc4_hdmi = ptr;
3262 cec_unregister_adapter(vc4_hdmi->cec_adap);
3263 vc4_hdmi->cec_adap = NULL;
3266 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
3268 struct cec_connector_info conn_info;
3269 struct platform_device *pdev = vc4_hdmi->pdev;
3270 struct device *dev = &pdev->dev;
3271 unsigned long flags;
3274 if (!of_find_property(dev->of_node, "interrupts", NULL)) {
3275 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
3279 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
3282 CEC_CAP_CONNECTOR_INFO, 1);
3283 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
3287 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
3288 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
3290 if (vc4_hdmi->variant->external_irq_controller) {
3291 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-rx"),
3292 vc4_cec_irq_handler_rx_bare,
3293 vc4_cec_irq_handler_rx_thread, 0,
3294 "vc4 hdmi cec rx", vc4_hdmi);
3296 goto err_delete_cec_adap;
3298 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-tx"),
3299 vc4_cec_irq_handler_tx_bare,
3300 vc4_cec_irq_handler_tx_thread, 0,
3301 "vc4 hdmi cec tx", vc4_hdmi);
3303 goto err_delete_cec_adap;
3305 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3306 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
3307 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3309 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
3310 vc4_cec_irq_handler,
3311 vc4_cec_irq_handler_thread, 0,
3312 "vc4 hdmi cec", vc4_hdmi);
3314 goto err_delete_cec_adap;
3317 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
3319 goto err_delete_cec_adap;
3322 * NOTE: Strictly speaking, we should probably use a DRM-managed
3323 * registration there to avoid removing the CEC adapter by the
3324 * time the DRM driver doesn't have any user anymore.
3326 * However, the CEC framework already cleans up the CEC adapter
3327 * only when the last user has closed its file descriptor, so we
3328 * don't need to handle it in DRM.
3330 * By the time the device-managed hook is executed, we will give
3331 * up our reference to the CEC adapter and therefore don't
3332 * really care when it's actually freed.
3334 * There's still a problematic sequence: if we unregister our
3335 * CEC adapter, but the userspace keeps a handle on the CEC
3336 * adapter but not the DRM device for some reason. In such a
3337 * case, our vc4_hdmi structure will be freed, but the
3338 * cec_adapter structure will have a dangling pointer to what
3339 * used to be our HDMI controller. If we get a CEC call at that
3340 * moment, we could end up with a use-after-free. Fortunately,
3341 * the CEC framework already handles this too, by calling
3342 * cec_is_registered() in cec_ioctl() and cec_poll().
3344 ret = devm_add_action_or_reset(dev, vc4_hdmi_cec_release, vc4_hdmi);
3350 err_delete_cec_adap:
3351 cec_delete_adapter(vc4_hdmi->cec_adap);
3356 static int vc4_hdmi_cec_resume(struct vc4_hdmi *vc4_hdmi)
3358 unsigned long flags;
3361 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3362 value = HDMI_READ(HDMI_CEC_CNTRL_1);
3363 /* Set the logical address to Unregistered */
3364 value |= VC4_HDMI_CEC_ADDR_MASK;
3365 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
3366 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3368 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
3370 if (!vc4_hdmi->variant->external_irq_controller) {
3371 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3372 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
3373 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3379 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
3384 static int vc4_hdmi_cec_resume(struct vc4_hdmi *vc4_hdmi)
3390 static void vc4_hdmi_free_regset(struct drm_device *drm, void *ptr)
3392 struct debugfs_reg32 *regs = ptr;
3397 static int vc4_hdmi_build_regset(struct drm_device *drm,
3398 struct vc4_hdmi *vc4_hdmi,
3399 struct debugfs_regset32 *regset,
3400 enum vc4_hdmi_regs reg)
3402 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
3403 struct debugfs_reg32 *regs, *new_regs;
3404 unsigned int count = 0;
3408 regs = kcalloc(variant->num_registers, sizeof(*regs),
3413 for (i = 0; i < variant->num_registers; i++) {
3414 const struct vc4_hdmi_register *field = &variant->registers[i];
3416 if (field->reg != reg)
3419 regs[count].name = field->name;
3420 regs[count].offset = field->offset;
3424 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
3428 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
3429 regset->regs = new_regs;
3430 regset->nregs = count;
3432 ret = drmm_add_action_or_reset(drm, vc4_hdmi_free_regset, new_regs);
3439 static int vc4_hdmi_init_resources(struct drm_device *drm,
3440 struct vc4_hdmi *vc4_hdmi)
3442 struct platform_device *pdev = vc4_hdmi->pdev;
3443 struct device *dev = &pdev->dev;
3446 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
3447 if (IS_ERR(vc4_hdmi->hdmicore_regs))
3448 return PTR_ERR(vc4_hdmi->hdmicore_regs);
3450 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
3451 if (IS_ERR(vc4_hdmi->hd_regs))
3452 return PTR_ERR(vc4_hdmi->hd_regs);
3454 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3458 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3462 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
3463 if (IS_ERR(vc4_hdmi->pixel_clock)) {
3464 ret = PTR_ERR(vc4_hdmi->pixel_clock);
3465 if (ret != -EPROBE_DEFER)
3466 DRM_ERROR("Failed to get pixel clock\n");
3470 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3471 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3472 DRM_ERROR("Failed to get HDMI state machine clock\n");
3473 return PTR_ERR(vc4_hdmi->hsm_clock);
3475 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
3476 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
3481 static int vc5_hdmi_init_resources(struct drm_device *drm,
3482 struct vc4_hdmi *vc4_hdmi)
3484 struct platform_device *pdev = vc4_hdmi->pdev;
3485 struct device *dev = &pdev->dev;
3486 struct resource *res;
3489 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
3493 vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
3494 resource_size(res));
3495 if (!vc4_hdmi->hdmicore_regs)
3498 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
3502 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
3503 if (!vc4_hdmi->hd_regs)
3506 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
3510 vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
3511 if (!vc4_hdmi->cec_regs)
3514 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
3518 vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
3519 if (!vc4_hdmi->csc_regs)
3522 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
3526 vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
3527 if (!vc4_hdmi->dvp_regs)
3530 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
3534 vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
3535 if (!vc4_hdmi->phy_regs)
3538 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
3542 vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
3543 if (!vc4_hdmi->ram_regs)
3546 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
3550 vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
3551 if (!vc4_hdmi->rm_regs)
3554 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3555 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3556 DRM_ERROR("Failed to get HDMI state machine clock\n");
3557 return PTR_ERR(vc4_hdmi->hsm_clock);
3560 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
3561 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
3562 DRM_ERROR("Failed to get pixel bvb clock\n");
3563 return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
3566 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
3567 if (IS_ERR(vc4_hdmi->audio_clock)) {
3568 DRM_ERROR("Failed to get audio clock\n");
3569 return PTR_ERR(vc4_hdmi->audio_clock);
3572 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
3573 if (IS_ERR(vc4_hdmi->cec_clock)) {
3574 DRM_ERROR("Failed to get CEC clock\n");
3575 return PTR_ERR(vc4_hdmi->cec_clock);
3578 vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
3579 if (IS_ERR(vc4_hdmi->reset)) {
3580 DRM_ERROR("Failed to get HDMI reset line\n");
3581 return PTR_ERR(vc4_hdmi->reset);
3584 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3588 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3592 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->cec_regset, VC5_CEC);
3596 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->csc_regset, VC5_CSC);
3600 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->dvp_regset, VC5_DVP);
3604 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->phy_regset, VC5_PHY);
3608 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->ram_regset, VC5_RAM);
3612 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->rm_regset, VC5_RM);
3619 static int __maybe_unused vc4_hdmi_runtime_suspend(struct device *dev)
3621 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3623 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3628 static int vc4_hdmi_runtime_resume(struct device *dev)
3630 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3633 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
3637 if (vc4_hdmi->variant->reset)
3638 vc4_hdmi->variant->reset(vc4_hdmi);
3640 ret = vc4_hdmi_cec_resume(vc4_hdmi);
3642 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3649 static void vc4_hdmi_put_ddc_device(void *ptr)
3651 struct vc4_hdmi *vc4_hdmi = ptr;
3653 put_device(&vc4_hdmi->ddc->dev);
3656 #ifdef CONFIG_EXTCON
3657 static const unsigned int vc4_hdmi_extcon_cable[] = {
3663 static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
3665 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
3666 struct platform_device *pdev = to_platform_device(dev);
3667 struct drm_device *drm = dev_get_drvdata(master);
3668 struct vc4_hdmi *vc4_hdmi;
3669 struct drm_encoder *encoder;
3670 struct device_node *ddc_node;
3673 vc4_hdmi = drmm_kzalloc(drm, sizeof(*vc4_hdmi), GFP_KERNEL);
3677 ret = drmm_mutex_init(drm, &vc4_hdmi->mutex);
3681 spin_lock_init(&vc4_hdmi->hw_lock);
3682 INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
3684 dev_set_drvdata(dev, vc4_hdmi);
3685 encoder = &vc4_hdmi->encoder.base;
3686 vc4_hdmi->encoder.type = variant->encoder_type;
3687 vc4_hdmi->encoder.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
3688 vc4_hdmi->encoder.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
3689 vc4_hdmi->encoder.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
3690 vc4_hdmi->encoder.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
3691 vc4_hdmi->encoder.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
3692 vc4_hdmi->pdev = pdev;
3693 vc4_hdmi->variant = variant;
3696 * Since we don't know the state of the controller and its
3697 * display (if any), let's assume it's always enabled.
3698 * vc4_hdmi_disable_scrambling() will thus run at boot, make
3699 * sure it's disabled, and avoid any inconsistency.
3701 if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK)
3702 vc4_hdmi->scdc_enabled = true;
3704 ret = variant->init_resources(drm, vc4_hdmi);
3708 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
3710 DRM_ERROR("Failed to find ddc node in device tree\n");
3714 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
3715 of_node_put(ddc_node);
3716 if (!vc4_hdmi->ddc) {
3717 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
3718 return -EPROBE_DEFER;
3721 ret = devm_add_action_or_reset(dev, vc4_hdmi_put_ddc_device, vc4_hdmi);
3725 #ifdef CONFIG_EXTCON
3726 vc4_hdmi->status = connector_status_disconnected;
3728 /* Initialize extcon device */
3729 vc4_hdmi->edev = devm_extcon_dev_allocate(dev, vc4_hdmi_extcon_cable);
3730 if (IS_ERR(vc4_hdmi->edev)) {
3731 dev_err(dev, "failed to allocate memory for extcon\n");
3732 return PTR_ERR(vc4_hdmi->edev);
3735 ret = devm_extcon_dev_register(dev, vc4_hdmi->edev);
3737 dev_err(dev, "failed to register extcon device\n");
3742 /* Only use the GPIO HPD pin if present in the DT, otherwise
3743 * we'll use the HDMI core's register.
3745 vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
3746 if (IS_ERR(vc4_hdmi->hpd_gpio)) {
3747 return PTR_ERR(vc4_hdmi->hpd_gpio);
3750 vc4_hdmi->disable_wifi_frequencies =
3751 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
3754 * If we boot without any cable connected to the HDMI connector,
3755 * the firmware will skip the HSM initialization and leave it
3756 * with a rate of 0, resulting in a bus lockup when we're
3757 * accessing the registers even if it's enabled.
3759 * Let's put a sensible default at runtime_resume so that we
3760 * don't end up in this situation.
3762 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, HSM_MIN_CLOCK_FREQ);
3766 ret = devm_pm_runtime_enable(dev);
3770 ret = pm_runtime_resume_and_get(dev);
3774 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
3775 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
3776 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
3777 clk_prepare_enable(vc4_hdmi->pixel_clock);
3778 clk_prepare_enable(vc4_hdmi->hsm_clock);
3779 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
3782 ret = drmm_encoder_init(drm, encoder,
3783 &vc4_hdmi_encoder_funcs,
3784 DRM_MODE_ENCODER_TMDS,
3787 goto err_put_runtime_pm;
3789 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
3791 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
3793 goto err_put_runtime_pm;
3795 ret = vc4_hdmi_hotplug_init(vc4_hdmi);
3797 goto err_put_runtime_pm;
3799 ret = vc4_hdmi_cec_init(vc4_hdmi);
3801 goto err_put_runtime_pm;
3803 ret = vc4_hdmi_audio_init(vc4_hdmi);
3805 goto err_put_runtime_pm;
3807 pm_runtime_put_sync(dev);
3812 pm_runtime_put_sync(dev);
3817 static const struct component_ops vc4_hdmi_ops = {
3818 .bind = vc4_hdmi_bind,
3821 static int vc4_hdmi_dev_probe(struct platform_device *pdev)
3823 return component_add(&pdev->dev, &vc4_hdmi_ops);
3826 static int vc4_hdmi_dev_remove(struct platform_device *pdev)
3828 component_del(&pdev->dev, &vc4_hdmi_ops);
3832 static const struct vc4_hdmi_variant bcm2835_variant = {
3833 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3834 .debugfs_name = "hdmi_regs",
3835 .card_name = "vc4-hdmi",
3836 .max_pixel_clock = 162000000,
3837 .registers = vc4_hdmi_fields,
3838 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
3840 .init_resources = vc4_hdmi_init_resources,
3841 .csc_setup = vc4_hdmi_csc_setup,
3842 .reset = vc4_hdmi_reset,
3843 .set_timings = vc4_hdmi_set_timings,
3844 .phy_init = vc4_hdmi_phy_init,
3845 .phy_disable = vc4_hdmi_phy_disable,
3846 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
3847 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
3848 .channel_map = vc4_hdmi_channel_map,
3849 .supports_hdr = false,
3852 static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
3853 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3854 .debugfs_name = "hdmi0_regs",
3855 .card_name = "vc4-hdmi-0",
3856 .max_pixel_clock = 600000000,
3857 .registers = vc5_hdmi_hdmi0_fields,
3858 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
3859 .phy_lane_mapping = {
3865 .unsupported_odd_h_timings = true,
3866 .external_irq_controller = true,
3868 .init_resources = vc5_hdmi_init_resources,
3869 .csc_setup = vc5_hdmi_csc_setup,
3870 .reset = vc5_hdmi_reset,
3871 .set_timings = vc5_hdmi_set_timings,
3872 .phy_init = vc5_hdmi_phy_init,
3873 .phy_disable = vc5_hdmi_phy_disable,
3874 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3875 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3876 .channel_map = vc5_hdmi_channel_map,
3877 .supports_hdr = true,
3878 .hp_detect = vc5_hdmi_hp_detect,
3881 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
3882 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
3883 .debugfs_name = "hdmi1_regs",
3884 .card_name = "vc4-hdmi-1",
3885 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
3886 .registers = vc5_hdmi_hdmi1_fields,
3887 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
3888 .phy_lane_mapping = {
3894 .unsupported_odd_h_timings = true,
3895 .external_irq_controller = true,
3897 .init_resources = vc5_hdmi_init_resources,
3898 .csc_setup = vc5_hdmi_csc_setup,
3899 .reset = vc5_hdmi_reset,
3900 .set_timings = vc5_hdmi_set_timings,
3901 .phy_init = vc5_hdmi_phy_init,
3902 .phy_disable = vc5_hdmi_phy_disable,
3903 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3904 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3905 .channel_map = vc5_hdmi_channel_map,
3906 .supports_hdr = true,
3907 .hp_detect = vc5_hdmi_hp_detect,
3910 static const struct of_device_id vc4_hdmi_dt_match[] = {
3911 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
3912 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
3913 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
3917 static const struct dev_pm_ops vc4_hdmi_pm_ops = {
3918 SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
3919 vc4_hdmi_runtime_resume,
3923 struct platform_driver vc4_hdmi_driver = {
3924 .probe = vc4_hdmi_dev_probe,
3925 .remove = vc4_hdmi_dev_remove,
3928 .of_match_table = vc4_hdmi_dt_match,
3929 .pm = &vc4_hdmi_pm_ops,