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/i2c.h>
44 #include <linux/module.h>
45 #include <linux/moduleparam.h>
46 #include <linux/of_address.h>
47 #include <linux/of_gpio.h>
48 #include <linux/of_platform.h>
49 #include <linux/pm_runtime.h>
50 #include <linux/rational.h>
51 #include <linux/reset.h>
52 #include <sound/dmaengine_pcm.h>
53 #include <sound/hdmi-codec.h>
54 #include <sound/pcm_drm_eld.h>
55 #include <sound/pcm_params.h>
56 #include <sound/soc.h>
57 #include "media/cec.h"
60 #include "vc4_hdmi_regs.h"
64 * "Broadcast RGB" property.
65 * Allows overriding of HDMI full or limited range RGB
67 #define VC4_BROADCAST_RGB_AUTO 0
68 #define VC4_BROADCAST_RGB_FULL 1
69 #define VC4_BROADCAST_RGB_LIMITED 2
71 #define VC5_HDMI_HORZA_HFP_SHIFT 16
72 #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
73 #define VC5_HDMI_HORZA_VPOS BIT(15)
74 #define VC5_HDMI_HORZA_HPOS BIT(14)
75 #define VC5_HDMI_HORZA_HAP_SHIFT 0
76 #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
78 #define VC5_HDMI_HORZB_HBP_SHIFT 16
79 #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
80 #define VC5_HDMI_HORZB_HSP_SHIFT 0
81 #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
83 #define VC5_HDMI_VERTA_VSP_SHIFT 24
84 #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
85 #define VC5_HDMI_VERTA_VFP_SHIFT 16
86 #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
87 #define VC5_HDMI_VERTA_VAL_SHIFT 0
88 #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
90 #define VC5_HDMI_VERTB_VSPO_SHIFT 16
91 #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
93 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
94 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
95 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
96 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
98 #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
100 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
101 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
103 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
104 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
106 #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
108 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
109 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
111 # define VC4_HD_M_SW_RST BIT(2)
112 # define VC4_HD_M_ENABLE BIT(0)
114 #define HSM_MIN_CLOCK_FREQ 120000000
115 #define CEC_CLOCK_FREQ 40000
117 #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
119 /* bit field to force hotplug detection. bit0 = HDMI0 */
120 static int force_hotplug = 0;
121 module_param(force_hotplug, int, 0644);
123 static const char * const output_format_str[] = {
124 [VC4_HDMI_OUTPUT_RGB] = "RGB",
125 [VC4_HDMI_OUTPUT_YUV420] = "YUV 4:2:0",
126 [VC4_HDMI_OUTPUT_YUV422] = "YUV 4:2:2",
127 [VC4_HDMI_OUTPUT_YUV444] = "YUV 4:4:4",
130 static const char *vc4_hdmi_output_fmt_str(enum vc4_hdmi_output_format fmt)
132 if (fmt >= ARRAY_SIZE(output_format_str))
135 return output_format_str[fmt];
138 static unsigned long long
139 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
140 unsigned int bpc, enum vc4_hdmi_output_format fmt);
142 static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
144 enum vc4_hdmi_output_format fmt)
146 unsigned long long clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
148 return clock > HDMI_14_MAX_TMDS_CLK;
151 static bool vc4_hdmi_is_full_range(struct vc4_hdmi *vc4_hdmi,
152 const struct drm_display_mode *mode)
154 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
156 if (vc4_hdmi->broadcast_rgb == VC4_BROADCAST_RGB_LIMITED)
158 else if (vc4_hdmi->broadcast_rgb == VC4_BROADCAST_RGB_FULL)
160 return !vc4_encoder->hdmi_monitor ||
161 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
164 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
166 struct drm_info_node *node = (struct drm_info_node *)m->private;
167 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
168 struct drm_device *drm = vc4_hdmi->connector.dev;
169 struct drm_printer p = drm_seq_file_printer(m);
172 if (!drm_dev_enter(drm, &idx))
175 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
176 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
177 drm_print_regset32(&p, &vc4_hdmi->cec_regset);
178 drm_print_regset32(&p, &vc4_hdmi->csc_regset);
179 drm_print_regset32(&p, &vc4_hdmi->dvp_regset);
180 drm_print_regset32(&p, &vc4_hdmi->phy_regset);
181 drm_print_regset32(&p, &vc4_hdmi->ram_regset);
182 drm_print_regset32(&p, &vc4_hdmi->rm_regset);
189 static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
191 struct drm_device *drm = vc4_hdmi->connector.dev;
196 * We can be called by our bind callback, when the
197 * connector->dev pointer might not be initialised yet.
199 if (drm && !drm_dev_enter(drm, &idx))
202 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
204 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
206 HDMI_WRITE(HDMI_M_CTL, 0);
208 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
210 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
211 VC4_HDMI_SW_RESET_HDMI |
212 VC4_HDMI_SW_RESET_FORMAT_DETECT);
214 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
216 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
222 static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
224 struct drm_device *drm = vc4_hdmi->connector.dev;
229 * We can be called by our bind callback, when the
230 * connector->dev pointer might not be initialised yet.
232 if (drm && !drm_dev_enter(drm, &idx))
235 reset_control_reset(vc4_hdmi->reset);
237 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
239 HDMI_WRITE(HDMI_DVP_CTL, 0);
241 HDMI_WRITE(HDMI_CLOCK_STOP,
242 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
244 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
250 #ifdef CONFIG_DRM_VC4_HDMI_CEC
251 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
253 struct drm_device *drm = vc4_hdmi->connector.dev;
254 unsigned long cec_rate;
261 * This function is called by our runtime_resume implementation
262 * and thus at bind time, when we haven't registered our
263 * connector yet and thus don't have a pointer to the DRM
266 if (drm && !drm_dev_enter(drm, &idx))
269 cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
271 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
273 value = HDMI_READ(HDMI_CEC_CNTRL_1);
274 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
277 * Set the clock divider: the hsm_clock rate and this divider
278 * setting will give a 40 kHz CEC clock.
280 clk_cnt = cec_rate / CEC_CLOCK_FREQ;
281 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
282 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
284 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
290 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
293 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder);
295 static enum drm_connector_status
296 vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
298 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
299 bool connected = false;
301 mutex_lock(&vc4_hdmi->mutex);
303 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
305 if (force_hotplug & BIT(vc4_hdmi->encoder.base.type - VC4_ENCODER_TYPE_HDMI0))
307 else if (vc4_hdmi->hpd_gpio) {
308 if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
311 if (vc4_hdmi->variant->hp_detect &&
312 vc4_hdmi->variant->hp_detect(vc4_hdmi))
317 if (connector->status != connector_status_connected) {
318 struct edid *edid = drm_get_edid(connector, vc4_hdmi->ddc);
321 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
322 vc4_hdmi->encoder.hdmi_monitor = drm_detect_hdmi_monitor(edid);
325 vc4_hdmi->encoder.hdmi_monitor = false;
329 vc4_hdmi_enable_scrambling(&vc4_hdmi->encoder.base.base);
330 pm_runtime_put(&vc4_hdmi->pdev->dev);
331 mutex_unlock(&vc4_hdmi->mutex);
332 return connector_status_connected;
335 vc4_hdmi->encoder.hdmi_monitor = false;
337 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
338 pm_runtime_put(&vc4_hdmi->pdev->dev);
339 mutex_unlock(&vc4_hdmi->mutex);
340 return connector_status_disconnected;
343 static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
345 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
346 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
347 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
351 mutex_lock(&vc4_hdmi->mutex);
353 edid = drm_get_edid(connector, vc4_hdmi->ddc);
354 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
360 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
362 drm_connector_update_edid_property(connector, edid);
363 ret = drm_add_edid_modes(connector, edid);
366 if (!vc4->hvs->vc5_hdmi_enable_scrambling) {
367 struct drm_device *drm = connector->dev;
368 struct drm_display_mode *mode;
370 list_for_each_entry(mode, &connector->probed_modes, head) {
371 if (vc4_hdmi_mode_needs_scrambling(mode, 8, VC4_HDMI_OUTPUT_RGB)) {
372 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
373 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
379 mutex_unlock(&vc4_hdmi->mutex);
384 static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
385 struct drm_atomic_state *state)
387 struct drm_connector_state *old_state =
388 drm_atomic_get_old_connector_state(state, connector);
389 struct vc4_hdmi_connector_state *old_vc4_state = conn_state_to_vc4_hdmi_conn_state(old_state);
390 struct drm_connector_state *new_state =
391 drm_atomic_get_new_connector_state(state, connector);
392 struct vc4_hdmi_connector_state *new_vc4_state = conn_state_to_vc4_hdmi_conn_state(new_state);
393 struct drm_crtc *crtc = new_state->crtc;
398 if (old_state->colorspace != new_state->colorspace ||
399 old_vc4_state->broadcast_rgb != new_vc4_state->broadcast_rgb ||
400 !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
401 struct drm_crtc_state *crtc_state;
403 crtc_state = drm_atomic_get_crtc_state(state, crtc);
404 if (IS_ERR(crtc_state))
405 return PTR_ERR(crtc_state);
407 crtc_state->mode_changed = true;
414 * vc4_hdmi_connector_atomic_get_property - hook for
415 * connector->atomic_get_property.
416 * @connector: Connector to get the property for.
417 * @state: Connector state to retrieve the property from.
418 * @property: Property to retrieve.
419 * @val: Return value for the property.
421 * Returns the atomic property value for a digital connector.
423 int vc4_hdmi_connector_get_property(struct drm_connector *connector,
424 const struct drm_connector_state *state,
425 struct drm_property *property,
428 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
429 const struct vc4_hdmi_connector_state *vc4_conn_state =
430 const_conn_state_to_vc4_hdmi_conn_state(state);
432 if (property == vc4_hdmi->broadcast_rgb_property) {
433 *val = vc4_conn_state->broadcast_rgb;
435 DRM_DEBUG_ATOMIC("Unknown property [PROP:%d:%s]\n",
436 property->base.id, property->name);
444 * vc4_hdmi_connector_atomic_set_property - hook for
445 * connector->atomic_set_property.
446 * @connector: Connector to set the property for.
447 * @state: Connector state to set the property on.
448 * @property: Property to set.
449 * @val: New value for the property.
451 * Sets the atomic property value for a digital connector.
453 int vc4_hdmi_connector_set_property(struct drm_connector *connector,
454 struct drm_connector_state *state,
455 struct drm_property *property,
458 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
459 struct vc4_hdmi_connector_state *vc4_conn_state =
460 conn_state_to_vc4_hdmi_conn_state(state);
462 if (property == vc4_hdmi->broadcast_rgb_property) {
463 vc4_conn_state->broadcast_rgb = val;
467 DRM_DEBUG_ATOMIC("Unknown property [PROP:%d:%s]\n",
468 property->base.id, property->name);
472 static void vc4_hdmi_connector_reset(struct drm_connector *connector)
474 struct vc4_hdmi_connector_state *old_state =
475 conn_state_to_vc4_hdmi_conn_state(connector->state);
476 struct vc4_hdmi_connector_state *new_state =
477 kzalloc(sizeof(*new_state), GFP_KERNEL);
479 if (connector->state)
480 __drm_atomic_helper_connector_destroy_state(connector->state);
483 __drm_atomic_helper_connector_reset(connector, &new_state->base);
488 new_state->base.max_bpc = 8;
489 new_state->base.max_requested_bpc = 8;
490 new_state->output_format = VC4_HDMI_OUTPUT_RGB;
491 drm_atomic_helper_connector_tv_reset(connector);
494 static struct drm_connector_state *
495 vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
497 struct drm_connector_state *conn_state = connector->state;
498 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
499 struct vc4_hdmi_connector_state *new_state;
501 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
505 new_state->pixel_rate = vc4_state->pixel_rate;
506 new_state->output_bpc = vc4_state->output_bpc;
507 new_state->output_format = vc4_state->output_format;
508 new_state->broadcast_rgb = vc4_state->broadcast_rgb;
509 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
511 return &new_state->base;
514 static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
515 .detect = vc4_hdmi_connector_detect,
516 .fill_modes = drm_helper_probe_single_connector_modes,
517 .reset = vc4_hdmi_connector_reset,
518 .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
519 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
520 .atomic_get_property = vc4_hdmi_connector_get_property,
521 .atomic_set_property = vc4_hdmi_connector_set_property,
524 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
525 .get_modes = vc4_hdmi_connector_get_modes,
526 .atomic_check = vc4_hdmi_connector_atomic_check,
529 static const struct drm_prop_enum_list broadcast_rgb_names[] = {
530 { VC4_BROADCAST_RGB_AUTO, "Automatic" },
531 { VC4_BROADCAST_RGB_FULL, "Full" },
532 { VC4_BROADCAST_RGB_LIMITED, "Limited 16:235" },
536 vc4_hdmi_attach_broadcast_rgb_property(struct drm_device *dev,
537 struct vc4_hdmi *vc4_hdmi)
539 struct drm_property *prop = vc4_hdmi->broadcast_rgb_property;
542 prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
545 ARRAY_SIZE(broadcast_rgb_names));
549 vc4_hdmi->broadcast_rgb_property = prop;
552 drm_object_attach_property(&vc4_hdmi->connector.base, prop, 0);
555 static int vc4_hdmi_connector_init(struct drm_device *dev,
556 struct vc4_hdmi *vc4_hdmi)
558 struct drm_connector *connector = &vc4_hdmi->connector;
559 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
562 ret = drmm_connector_init(dev, connector,
563 &vc4_hdmi_connector_funcs,
564 DRM_MODE_CONNECTOR_HDMIA,
569 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
572 * Some of the properties below require access to state, like bpc.
573 * Allocate some default initial connector state with our reset helper.
575 if (connector->funcs->reset)
576 connector->funcs->reset(connector);
578 /* Create and attach TV margin props to this connector. */
579 ret = drm_mode_create_tv_margin_properties(dev);
583 ret = drm_mode_create_hdmi_colorspace_property(connector);
587 drm_connector_attach_colorspace_property(connector);
588 drm_connector_attach_tv_margin_properties(connector);
589 drm_connector_attach_max_bpc_property(connector, 8, 12);
591 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
592 DRM_CONNECTOR_POLL_DISCONNECT);
594 connector->interlace_allowed = 1;
595 connector->doublescan_allowed = 0;
596 connector->stereo_allowed = 1;
598 if (vc4_hdmi->variant->supports_hdr)
599 drm_connector_attach_hdr_output_metadata_property(connector);
601 vc4_hdmi_attach_broadcast_rgb_property(dev, vc4_hdmi);
603 drm_connector_attach_encoder(connector, encoder);
608 static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
609 enum hdmi_infoframe_type type,
612 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
613 struct drm_device *drm = vc4_hdmi->connector.dev;
614 u32 packet_id = type - 0x80;
619 if (!drm_dev_enter(drm, &idx))
622 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
623 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
624 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
625 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
628 ret = wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
629 BIT(packet_id)), 100);
636 static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
637 union hdmi_infoframe *frame)
639 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
640 struct drm_device *drm = vc4_hdmi->connector.dev;
641 u32 packet_id = frame->any.type - 0x80;
642 const struct vc4_hdmi_register *ram_packet_start =
643 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
644 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
645 u32 packet_reg_next = ram_packet_start->offset +
646 VC4_HDMI_PACKET_STRIDE * (packet_id + 1);
647 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
648 ram_packet_start->reg);
649 uint8_t buffer[VC4_HDMI_PACKET_STRIDE] = {};
655 if (!drm_dev_enter(drm, &idx))
658 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
659 VC4_HDMI_RAM_PACKET_ENABLE),
660 "Packet RAM has to be on to store the packet.");
662 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
666 ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
668 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
672 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
674 for (i = 0; i < len; i += 7) {
675 writel(buffer[i + 0] << 0 |
681 writel(buffer[i + 3] << 0 |
683 buffer[i + 5] << 16 |
690 * clear remainder of packet ram as it's included in the
691 * infoframe and triggers a checksum error on hdmi analyser
693 for (; packet_reg < packet_reg_next; packet_reg += 4)
694 writel(0, base + packet_reg);
696 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
697 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
699 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
701 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
702 BIT(packet_id)), 100);
704 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
710 static void vc4_hdmi_avi_infoframe_colorspace(struct hdmi_avi_infoframe *frame,
711 enum vc4_hdmi_output_format fmt)
714 case VC4_HDMI_OUTPUT_RGB:
715 frame->colorspace = HDMI_COLORSPACE_RGB;
718 case VC4_HDMI_OUTPUT_YUV420:
719 frame->colorspace = HDMI_COLORSPACE_YUV420;
722 case VC4_HDMI_OUTPUT_YUV422:
723 frame->colorspace = HDMI_COLORSPACE_YUV422;
726 case VC4_HDMI_OUTPUT_YUV444:
727 frame->colorspace = HDMI_COLORSPACE_YUV444;
735 static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
737 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
738 struct drm_connector *connector = &vc4_hdmi->connector;
739 struct drm_connector_state *cstate = connector->state;
740 struct vc4_hdmi_connector_state *vc4_state =
741 conn_state_to_vc4_hdmi_conn_state(cstate);
742 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
743 union hdmi_infoframe frame;
746 lockdep_assert_held(&vc4_hdmi->mutex);
748 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
751 DRM_ERROR("couldn't fill AVI infoframe\n");
755 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
757 vc4_hdmi_is_full_range(vc4_hdmi, mode) ?
758 HDMI_QUANTIZATION_RANGE_FULL :
759 HDMI_QUANTIZATION_RANGE_LIMITED);
760 drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
761 vc4_hdmi_avi_infoframe_colorspace(&frame.avi, vc4_state->output_format);
762 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
764 vc4_hdmi_write_infoframe(encoder, &frame);
767 static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
769 union hdmi_infoframe frame;
772 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
774 DRM_ERROR("couldn't fill SPD infoframe\n");
778 frame.spd.sdi = HDMI_SPD_SDI_PC;
780 vc4_hdmi_write_infoframe(encoder, &frame);
783 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
785 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
786 struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
787 union hdmi_infoframe frame;
789 memcpy(&frame.audio, audio, sizeof(*audio));
790 vc4_hdmi_write_infoframe(encoder, &frame);
793 static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
795 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
796 struct drm_connector *connector = &vc4_hdmi->connector;
797 struct drm_connector_state *conn_state = connector->state;
798 union hdmi_infoframe frame;
800 lockdep_assert_held(&vc4_hdmi->mutex);
802 if (!vc4_hdmi->variant->supports_hdr)
805 if (!conn_state->hdr_output_metadata)
808 if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
811 vc4_hdmi_write_infoframe(encoder, &frame);
814 static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
816 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
818 lockdep_assert_held(&vc4_hdmi->mutex);
820 vc4_hdmi_set_avi_infoframe(encoder);
821 vc4_hdmi_set_spd_infoframe(encoder);
823 * If audio was streaming, then we need to reenabled the audio
824 * infoframe here during encoder_enable.
826 if (vc4_hdmi->audio.streaming)
827 vc4_hdmi_set_audio_infoframe(encoder);
829 vc4_hdmi_set_hdr_infoframe(encoder);
832 static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder,
833 struct drm_display_mode *mode)
835 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
836 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
837 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
839 lockdep_assert_held(&vc4_hdmi->mutex);
841 if (!vc4_encoder->hdmi_monitor)
844 if (!display->hdmi.scdc.supported ||
845 !display->hdmi.scdc.scrambling.supported)
851 #define SCRAMBLING_POLLING_DELAY_MS 1000
853 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
855 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
856 struct drm_device *drm = vc4_hdmi->connector.dev;
857 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
861 lockdep_assert_held(&vc4_hdmi->mutex);
863 if (!vc4_hdmi_supports_scrambling(encoder, mode))
866 if (!vc4_hdmi_mode_needs_scrambling(mode,
867 vc4_hdmi->output_bpc,
868 vc4_hdmi->output_format))
871 if (!drm_dev_enter(drm, &idx))
874 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
875 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
877 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
878 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
879 VC5_HDMI_SCRAMBLER_CTL_ENABLE);
880 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
884 vc4_hdmi->scdc_enabled = true;
886 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
887 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
890 static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
892 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
893 struct drm_device *drm = vc4_hdmi->connector.dev;
897 lockdep_assert_held(&vc4_hdmi->mutex);
899 if (!vc4_hdmi->scdc_enabled)
902 vc4_hdmi->scdc_enabled = false;
904 if (delayed_work_pending(&vc4_hdmi->scrambling_work))
905 cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
907 if (!drm_dev_enter(drm, &idx))
910 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
911 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
912 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
913 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
915 drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
916 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
921 static void vc4_hdmi_scrambling_wq(struct work_struct *work)
923 struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
927 if (drm_scdc_get_scrambling_status(vc4_hdmi->ddc))
930 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
931 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
933 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
934 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
937 static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
938 struct drm_atomic_state *state)
940 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
941 struct drm_device *drm = vc4_hdmi->connector.dev;
945 mutex_lock(&vc4_hdmi->mutex);
947 vc4_hdmi->output_enabled = false;
949 if (!drm_dev_enter(drm, &idx))
952 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
954 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
956 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
958 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
962 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
963 HDMI_WRITE(HDMI_VID_CTL,
964 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
965 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
967 vc4_hdmi_disable_scrambling(encoder);
972 mutex_unlock(&vc4_hdmi->mutex);
975 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
976 struct drm_atomic_state *state)
978 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
979 struct drm_device *drm = vc4_hdmi->connector.dev;
984 mutex_lock(&vc4_hdmi->mutex);
986 if (!drm_dev_enter(drm, &idx))
989 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
990 HDMI_WRITE(HDMI_VID_CTL,
991 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
992 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
994 if (vc4_hdmi->variant->phy_disable)
995 vc4_hdmi->variant->phy_disable(vc4_hdmi);
997 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
998 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1000 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
1002 DRM_ERROR("Failed to release power domain: %d\n", ret);
1007 mutex_unlock(&vc4_hdmi->mutex);
1010 static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1011 struct drm_connector_state *state,
1012 const struct drm_display_mode *mode)
1014 struct drm_device *drm = vc4_hdmi->connector.dev;
1015 unsigned long flags;
1019 if (!drm_dev_enter(drm, &idx))
1022 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1024 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
1025 VC4_HD_CSC_CTL_ORDER);
1027 if (!vc4_hdmi_is_full_range(vc4_hdmi, mode)) {
1028 /* CEA VICs other than #1 requre limited range RGB
1029 * output unless overridden by an AVI infoframe.
1030 * Apply a colorspace conversion to squash 0-255 down
1031 * to 16-235. The matrix here is:
1038 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
1039 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
1040 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1041 VC4_HD_CSC_CTL_MODE);
1043 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
1044 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
1045 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
1046 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
1047 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
1048 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
1051 /* The RGB order applies even when CSC is disabled. */
1052 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1054 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1060 * If we need to output Full Range RGB, then use the unity matrix
1066 * CEA VICs other than #1 require limited range RGB output unless
1067 * overridden by an AVI infoframe. Apply a colorspace conversion to
1068 * squash 0-255 down to 16-235. The matrix here is:
1074 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
1076 static const u16 vc5_hdmi_csc_full_rgb_to_rgb[2][3][4] = {
1078 /* Full range - unity */
1079 { 0x2000, 0x0000, 0x0000, 0x0000 },
1080 { 0x0000, 0x2000, 0x0000, 0x0000 },
1081 { 0x0000, 0x0000, 0x2000, 0x0000 },
1084 { 0x1b80, 0x0000, 0x0000, 0x0400 },
1085 { 0x0000, 0x1b80, 0x0000, 0x0400 },
1086 { 0x0000, 0x0000, 0x1b80, 0x0400 },
1091 * Conversion between Full Range RGB and YUV using the BT.601 Colorspace
1094 * [ 0.299000 0.587000 0.114000 0.000000 ]
1095 * [ -0.168736 -0.331264 0.500000 128.000000 ]
1096 * [ 0.500000 -0.418688 -0.081312 128.000000 ]
1099 * [ 0.255785 0.502160 0.097523 16.000000 ]
1100 * [ -0.147644 -0.289856 0.437500 128.000000 ]
1101 * [ 0.437500 -0.366352 -0.071148 128.000000 ]
1103 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
1105 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt601[2][3][4] = {
1108 { 0x0991, 0x12c9, 0x03a6, 0x0000 },
1109 { 0xfa9b, 0xf567, 0x1000, 0x2000 },
1110 { 0x1000, 0xf29b, 0xfd67, 0x2000 },
1113 { 0x082f, 0x1012, 0x031f, 0x0400 },
1114 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
1115 { 0x0e00, 0xf448, 0xfdba, 0x2000 },
1120 * Conversion between Full Range RGB and YUV using the BT.709 Colorspace
1123 * [ 0.212600 0.715200 0.072200 0.000000 ]
1124 * [ -0.114572 -0.385428 0.500000 128.000000 ]
1125 * [ 0.500000 -0.454153 -0.045847 128.000000 ]
1128 * [ 0.181873 0.611831 0.061765 16.000000 ]
1129 * [ -0.100251 -0.337249 0.437500 128.000000 ]
1130 * [ 0.437500 -0.397384 -0.040116 128.000000 ]
1132 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
1134 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt709[2][3][4] = {
1137 { 0x06ce, 0x16e3, 0x024f, 0x0000 },
1138 { 0xfc56, 0xf3ac, 0x1000, 0x2000 },
1139 { 0x1000, 0xf179, 0xfe89, 0x2000 },
1142 { 0x05d2, 0x1394, 0x01fa, 0x0400 },
1143 { 0xfccc, 0xf536, 0x0e00, 0x2000 },
1144 { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
1149 * Conversion between Full Range RGB and YUV using the BT.2020 Colorspace
1152 * [ 0.262700 0.678000 0.059300 0.000000 ]
1153 * [ -0.139630 -0.360370 0.500000 128.000000 ]
1154 * [ 0.500000 -0.459786 -0.040214 128.000000 ]
1157 * [ 0.224732 0.580008 0.050729 16.000000 ]
1158 * [ -0.122176 -0.315324 0.437500 128.000000 ]
1159 * [ 0.437500 -0.402312 -0.035188 128.000000 ]
1161 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
1163 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt2020[2][3][4] = {
1166 { 0x0868, 0x15b2, 0x01e6, 0x0000 },
1167 { 0xfb89, 0xf479, 0x1000, 0x2000 },
1168 { 0x1000, 0xf14a, 0xfeb8, 0x2000 },
1171 { 0x0731, 0x128f, 0x01a0, 0x0400 },
1172 { 0xfc18, 0xf5ea, 0x0e00, 0x2000 },
1173 { 0x0e00, 0xf321, 0xfee1, 0x2000 },
1177 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
1178 const u16 coeffs[3][4])
1180 lockdep_assert_held(&vc4_hdmi->hw_lock);
1182 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
1183 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
1184 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
1185 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
1186 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
1187 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
1190 static void vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi *vc4_hdmi,
1191 const u16 coeffs[3][4])
1193 lockdep_assert_held(&vc4_hdmi->hw_lock);
1195 /* YUV444 needs the CSC matrices using the channels in a different order */
1196 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[2][1] << 16) | coeffs[2][0]);
1197 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[2][3] << 16) | coeffs[2][2]);
1198 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[0][1] << 16) | coeffs[0][0]);
1199 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[0][3] << 16) | coeffs[0][2]);
1200 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[1][1] << 16) | coeffs[1][0]);
1201 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[1][3] << 16) | coeffs[1][2]);
1204 static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1205 struct drm_connector_state *state,
1206 const struct drm_display_mode *mode)
1208 struct drm_device *drm = vc4_hdmi->connector.dev;
1209 struct vc4_hdmi_connector_state *vc4_state =
1210 conn_state_to_vc4_hdmi_conn_state(state);
1211 unsigned int lim_range = vc4_hdmi_is_full_range(vc4_hdmi, mode) ? 0 : 1;
1212 const u16 (*csc)[4];
1213 unsigned long flags;
1215 u32 if_xbar = 0x543210;
1216 u32 csc_chan_ctl = 0;
1217 u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1218 VC5_MT_CP_CSC_CTL_MODE);
1221 if (!drm_dev_enter(drm, &idx))
1224 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1226 switch (vc4_state->output_format) {
1227 case VC4_HDMI_OUTPUT_YUV444:
1228 case VC4_HDMI_OUTPUT_YUV422:
1229 switch (state->colorspace) {
1231 case DRM_MODE_COLORIMETRY_NO_DATA:
1232 case DRM_MODE_COLORIMETRY_BT709_YCC:
1233 case DRM_MODE_COLORIMETRY_XVYCC_709:
1234 case DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED:
1235 case DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT:
1236 csc = vc5_hdmi_csc_full_rgb_to_yuv_bt709[lim_range];
1238 case DRM_MODE_COLORIMETRY_SMPTE_170M_YCC:
1239 case DRM_MODE_COLORIMETRY_XVYCC_601:
1240 case DRM_MODE_COLORIMETRY_SYCC_601:
1241 case DRM_MODE_COLORIMETRY_OPYCC_601:
1242 case DRM_MODE_COLORIMETRY_BT601_YCC:
1243 csc = vc5_hdmi_csc_full_rgb_to_yuv_bt601[lim_range];
1245 case DRM_MODE_COLORIMETRY_BT2020_CYCC:
1246 case DRM_MODE_COLORIMETRY_BT2020_YCC:
1247 case DRM_MODE_COLORIMETRY_BT2020_RGB:
1248 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
1249 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
1250 csc = vc5_hdmi_csc_full_rgb_to_yuv_bt2020[lim_range];
1254 if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
1255 csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
1256 VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
1257 VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
1258 VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
1260 csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
1261 VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
1263 if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
1264 VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);
1266 vc5_hdmi_set_csc_coeffs(vc4_hdmi, csc);
1268 vc5_hdmi_set_csc_coeffs_swap(vc4_hdmi, csc);
1273 case VC4_HDMI_OUTPUT_RGB:
1276 vc5_hdmi_set_csc_coeffs(vc4_hdmi,
1277 vc5_hdmi_csc_full_rgb_to_rgb[lim_range]);
1284 HDMI_WRITE(HDMI_VEC_INTERFACE_CFG, if_cfg);
1285 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, if_xbar);
1286 HDMI_WRITE(HDMI_CSC_CHANNEL_CTL, csc_chan_ctl);
1287 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1289 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1294 static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1295 struct drm_connector_state *state,
1296 struct drm_display_mode *mode)
1298 struct drm_device *drm = vc4_hdmi->connector.dev;
1299 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1300 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1301 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1302 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1303 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1304 VC4_HDMI_VERTA_VSP) |
1305 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1306 VC4_HDMI_VERTA_VFP) |
1307 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
1308 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1309 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1311 VC4_HDMI_VERTB_VBP));
1312 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1313 VC4_SET_FIELD(mode->crtc_vtotal -
1314 mode->crtc_vsync_end,
1315 VC4_HDMI_VERTB_VBP));
1316 unsigned long flags;
1320 if (!drm_dev_enter(drm, &idx))
1323 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1325 HDMI_WRITE(HDMI_HORZA,
1326 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
1327 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
1328 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1329 VC4_HDMI_HORZA_HAP));
1331 HDMI_WRITE(HDMI_HORZB,
1332 VC4_SET_FIELD((mode->htotal -
1333 mode->hsync_end) * pixel_rep,
1334 VC4_HDMI_HORZB_HBP) |
1335 VC4_SET_FIELD((mode->hsync_end -
1336 mode->hsync_start) * pixel_rep,
1337 VC4_HDMI_HORZB_HSP) |
1338 VC4_SET_FIELD((mode->hsync_start -
1339 mode->hdisplay) * pixel_rep,
1340 VC4_HDMI_HORZB_HFP));
1342 HDMI_WRITE(HDMI_VERTA0, verta);
1343 HDMI_WRITE(HDMI_VERTA1, verta);
1345 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1346 HDMI_WRITE(HDMI_VERTB1, vertb);
1348 reg = HDMI_READ(HDMI_MISC_CONTROL);
1349 reg &= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1350 reg |= VC4_SET_FIELD(pixel_rep - 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP);
1351 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1353 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1358 static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1359 struct drm_connector_state *state,
1360 struct drm_display_mode *mode)
1362 struct drm_device *drm = vc4_hdmi->connector.dev;
1363 const struct vc4_hdmi_connector_state *vc4_state =
1364 conn_state_to_vc4_hdmi_conn_state(state);
1365 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1366 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1367 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1368 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1369 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1370 VC5_HDMI_VERTA_VSP) |
1371 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1372 VC5_HDMI_VERTA_VFP) |
1373 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
1374 u32 vertb = (VC4_SET_FIELD(mode->htotal >> (2 - pixel_rep),
1375 VC5_HDMI_VERTB_VSPO) |
1376 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1378 VC4_HDMI_VERTB_VBP));
1379 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
1380 VC4_SET_FIELD(mode->crtc_vtotal -
1381 mode->crtc_vsync_end,
1382 VC4_HDMI_VERTB_VBP));
1383 unsigned long flags;
1389 if (!drm_dev_enter(drm, &idx))
1392 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1394 HDMI_WRITE(HDMI_HORZA,
1395 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
1396 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
1397 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1398 VC5_HDMI_HORZA_HAP) |
1399 VC4_SET_FIELD((mode->hsync_start -
1400 mode->hdisplay) * pixel_rep,
1401 VC5_HDMI_HORZA_HFP));
1403 HDMI_WRITE(HDMI_HORZB,
1404 VC4_SET_FIELD((mode->htotal -
1405 mode->hsync_end) * pixel_rep,
1406 VC5_HDMI_HORZB_HBP) |
1407 VC4_SET_FIELD((mode->hsync_end -
1408 mode->hsync_start) * pixel_rep,
1409 VC5_HDMI_HORZB_HSP));
1411 HDMI_WRITE(HDMI_VERTA0, verta);
1412 HDMI_WRITE(HDMI_VERTA1, verta);
1414 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1415 HDMI_WRITE(HDMI_VERTB1, vertb);
1417 switch (vc4_state->output_bpc) {
1434 * YCC422 is always 36-bit and not considered deep colour so
1435 * doesn't signal in GCP
1437 if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
1442 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
1443 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
1444 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
1445 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
1446 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
1447 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
1449 reg = HDMI_READ(HDMI_GCP_WORD_1);
1450 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
1451 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
1452 HDMI_WRITE(HDMI_GCP_WORD_1, reg);
1454 reg = HDMI_READ(HDMI_GCP_CONFIG);
1455 reg &= ~VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
1456 reg |= gcp_en ? VC5_HDMI_GCP_CONFIG_GCP_ENABLE : 0;
1457 HDMI_WRITE(HDMI_GCP_CONFIG, reg);
1459 reg = HDMI_READ(HDMI_MISC_CONTROL);
1460 reg &= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1461 reg |= VC4_SET_FIELD(pixel_rep - 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP);
1462 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1464 HDMI_WRITE(HDMI_CLOCK_STOP, 0);
1466 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1471 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
1473 struct drm_device *drm = vc4_hdmi->connector.dev;
1474 unsigned long flags;
1479 if (!drm_dev_enter(drm, &idx))
1482 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1484 drift = HDMI_READ(HDMI_FIFO_CTL);
1485 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
1487 HDMI_WRITE(HDMI_FIFO_CTL,
1488 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1489 HDMI_WRITE(HDMI_FIFO_CTL,
1490 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1492 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1494 usleep_range(1000, 1100);
1496 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1498 HDMI_WRITE(HDMI_FIFO_CTL,
1499 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1500 HDMI_WRITE(HDMI_FIFO_CTL,
1501 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1503 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1505 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
1506 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
1507 WARN_ONCE(ret, "Timeout waiting for "
1508 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
1513 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
1514 struct drm_atomic_state *state)
1516 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1517 struct drm_device *drm = vc4_hdmi->connector.dev;
1518 struct drm_connector *connector = &vc4_hdmi->connector;
1519 struct drm_connector_state *conn_state =
1520 drm_atomic_get_new_connector_state(state, connector);
1521 struct vc4_hdmi_connector_state *vc4_conn_state =
1522 conn_state_to_vc4_hdmi_conn_state(conn_state);
1523 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1524 unsigned long pixel_rate = vc4_conn_state->pixel_rate;
1525 unsigned long bvb_rate, hsm_rate;
1526 unsigned long flags;
1530 mutex_lock(&vc4_hdmi->mutex);
1532 if (!drm_dev_enter(drm, &idx))
1536 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
1537 * be faster than pixel clock, infinitesimally faster, tested in
1538 * simulation. Otherwise, exact value is unimportant for HDMI
1539 * operation." This conflicts with bcm2835's vc4 documentation, which
1540 * states HSM's clock has to be at least 108% of the pixel clock.
1542 * Real life tests reveal that vc4's firmware statement holds up, and
1543 * users are able to use pixel clocks closer to HSM's, namely for
1544 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
1545 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
1548 * Additionally, the AXI clock needs to be at least 25% of
1549 * pixel clock, but HSM ends up being the limiting factor.
1551 hsm_rate = max_t(unsigned long, 120000000, (pixel_rate / 100) * 101);
1552 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
1554 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1558 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
1560 DRM_ERROR("Failed to retain power domain: %d\n", ret);
1564 ret = clk_set_rate(vc4_hdmi->pixel_clock, pixel_rate);
1566 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
1567 goto err_put_runtime_pm;
1570 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
1572 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
1573 goto err_put_runtime_pm;
1577 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
1579 if (pixel_rate > 297000000)
1580 bvb_rate = 300000000;
1581 else if (pixel_rate > 148500000)
1582 bvb_rate = 150000000;
1584 bvb_rate = 75000000;
1586 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
1588 DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
1589 goto err_disable_pixel_clock;
1592 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
1594 DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
1595 goto err_disable_pixel_clock;
1598 if (vc4_hdmi->variant->phy_init)
1599 vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
1601 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1603 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1604 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1605 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
1606 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
1608 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1610 if (vc4_hdmi->variant->set_timings)
1611 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
1615 mutex_unlock(&vc4_hdmi->mutex);
1619 err_disable_pixel_clock:
1620 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1622 pm_runtime_put(&vc4_hdmi->pdev->dev);
1626 mutex_unlock(&vc4_hdmi->mutex);
1630 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
1631 struct drm_atomic_state *state)
1633 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1634 struct drm_device *drm = vc4_hdmi->connector.dev;
1635 struct drm_connector *connector = &vc4_hdmi->connector;
1636 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1637 struct drm_connector_state *conn_state =
1638 drm_atomic_get_new_connector_state(state, connector);
1639 unsigned long flags;
1642 mutex_lock(&vc4_hdmi->mutex);
1644 if (!drm_dev_enter(drm, &idx))
1647 if (vc4_hdmi->variant->csc_setup)
1648 vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
1650 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1651 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
1652 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1656 mutex_unlock(&vc4_hdmi->mutex);
1659 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
1660 struct drm_atomic_state *state)
1662 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1663 struct drm_device *drm = vc4_hdmi->connector.dev;
1664 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1665 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
1666 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1667 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1668 unsigned long flags;
1672 mutex_lock(&vc4_hdmi->mutex);
1674 if (!drm_dev_enter(drm, &idx))
1677 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1679 HDMI_WRITE(HDMI_VID_CTL,
1680 VC4_HD_VID_CTL_ENABLE |
1681 VC4_HD_VID_CTL_CLRRGB |
1682 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
1683 VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
1684 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
1685 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
1687 HDMI_WRITE(HDMI_VID_CTL,
1688 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1690 if (vc4_encoder->hdmi_monitor) {
1691 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1692 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1693 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1695 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1697 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1698 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1699 WARN_ONCE(ret, "Timeout waiting for "
1700 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1702 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1703 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
1704 ~(VC4_HDMI_RAM_PACKET_ENABLE));
1705 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1706 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1707 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1709 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1711 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1712 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1713 WARN_ONCE(ret, "Timeout waiting for "
1714 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1717 if (vc4_encoder->hdmi_monitor) {
1718 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1720 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1721 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
1723 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1724 VC4_HDMI_RAM_PACKET_ENABLE);
1726 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1727 vc4_hdmi->output_enabled = true;
1729 vc4_hdmi_set_infoframes(encoder);
1732 vc4_hdmi_recenter_fifo(vc4_hdmi);
1733 vc4_hdmi_enable_scrambling(encoder);
1736 mutex_unlock(&vc4_hdmi->mutex);
1739 static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
1740 struct drm_crtc_state *crtc_state,
1741 struct drm_connector_state *conn_state)
1743 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1744 struct vc4_hdmi_connector_state *vc4_state =
1745 conn_state_to_vc4_hdmi_conn_state(conn_state);
1747 mutex_lock(&vc4_hdmi->mutex);
1748 vc4_hdmi->output_bpc = vc4_state->output_bpc;
1749 vc4_hdmi->output_format = vc4_state->output_format;
1750 vc4_hdmi->broadcast_rgb = vc4_state->broadcast_rgb;
1751 memcpy(&vc4_hdmi->saved_adjusted_mode,
1752 &crtc_state->adjusted_mode,
1753 sizeof(vc4_hdmi->saved_adjusted_mode));
1754 mutex_unlock(&vc4_hdmi->mutex);
1758 vc4_hdmi_sink_supports_format_bpc(const struct vc4_hdmi *vc4_hdmi,
1759 const struct drm_display_info *info,
1760 const struct drm_display_mode *mode,
1761 unsigned int format, unsigned int bpc)
1763 struct drm_device *dev = vc4_hdmi->connector.dev;
1764 u8 vic = drm_match_cea_mode(mode);
1766 if (vic == 1 && bpc != 8) {
1767 drm_dbg(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
1771 if (!info->is_hdmi &&
1772 (format != VC4_HDMI_OUTPUT_RGB || bpc != 8)) {
1773 drm_dbg(dev, "DVI Monitors require an RGB output at 8 bpc\n");
1778 case VC4_HDMI_OUTPUT_RGB:
1779 drm_dbg(dev, "RGB Format, checking the constraints.\n");
1781 if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
1782 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
1786 if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
1787 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
1791 drm_dbg(dev, "RGB format supported in that configuration.\n");
1795 case VC4_HDMI_OUTPUT_YUV422:
1796 drm_dbg(dev, "YUV422 format, checking the constraints.\n");
1798 if (!(info->color_formats & DRM_COLOR_FORMAT_YCRCB422)) {
1799 drm_dbg(dev, "Sink doesn't support YUV422.\n");
1804 drm_dbg(dev, "YUV422 only supports 12 bpc.\n");
1808 drm_dbg(dev, "YUV422 format supported in that configuration.\n");
1812 case VC4_HDMI_OUTPUT_YUV444:
1813 drm_dbg(dev, "YUV444 format, checking the constraints.\n");
1815 if (!(info->color_formats & DRM_COLOR_FORMAT_YCRCB444)) {
1816 drm_dbg(dev, "Sink doesn't support YUV444.\n");
1820 if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
1821 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
1825 if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
1826 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
1830 drm_dbg(dev, "YUV444 format supported in that configuration.\n");
1838 static enum drm_mode_status
1839 vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi *vc4_hdmi,
1840 unsigned long long clock)
1842 const struct drm_connector *connector = &vc4_hdmi->connector;
1843 const struct drm_display_info *info = &connector->display_info;
1844 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
1846 if (clock > vc4_hdmi->variant->max_pixel_clock)
1847 return MODE_CLOCK_HIGH;
1849 if (!vc4->hvs->vc5_hdmi_enable_scrambling && clock > HDMI_14_MAX_TMDS_CLK)
1850 return MODE_CLOCK_HIGH;
1852 if (info->max_tmds_clock && clock > (info->max_tmds_clock * 1000))
1853 return MODE_CLOCK_HIGH;
1858 static unsigned long long
1859 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
1861 enum vc4_hdmi_output_format fmt)
1863 unsigned long long clock = mode->clock * 1000;
1865 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1868 if (fmt == VC4_HDMI_OUTPUT_YUV422)
1871 return clock * bpc / 8;
1875 vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi *vc4_hdmi,
1876 struct vc4_hdmi_connector_state *vc4_state,
1877 const struct drm_display_mode *mode,
1878 unsigned int bpc, unsigned int fmt)
1880 unsigned long long clock;
1882 clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
1883 if (vc4_hdmi_encoder_clock_valid(vc4_hdmi, clock) != MODE_OK)
1886 vc4_state->pixel_rate = clock;
1892 vc4_hdmi_encoder_compute_format(const struct vc4_hdmi *vc4_hdmi,
1893 struct vc4_hdmi_connector_state *vc4_state,
1894 const struct drm_display_mode *mode,
1897 struct drm_device *dev = vc4_hdmi->connector.dev;
1898 const struct drm_connector *connector = &vc4_hdmi->connector;
1899 const struct drm_display_info *info = &connector->display_info;
1900 unsigned int format;
1902 drm_dbg(dev, "Trying with an RGB output\n");
1904 format = VC4_HDMI_OUTPUT_RGB;
1905 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
1908 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
1911 vc4_state->output_format = format;
1916 drm_dbg(dev, "Failed, Trying with an YUV422 output\n");
1918 format = VC4_HDMI_OUTPUT_YUV422;
1919 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
1922 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
1925 vc4_state->output_format = format;
1930 drm_dbg(dev, "Failed. No Format Supported for that bpc count.\n");
1936 vc4_hdmi_encoder_compute_config(const struct vc4_hdmi *vc4_hdmi,
1937 struct vc4_hdmi_connector_state *vc4_state,
1938 const struct drm_display_mode *mode)
1940 struct drm_device *dev = vc4_hdmi->connector.dev;
1941 struct drm_connector_state *conn_state = &vc4_state->base;
1942 unsigned int max_bpc = clamp_t(unsigned int, conn_state->max_bpc, 8, 12);
1946 for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
1947 drm_dbg(dev, "Trying with a %d bpc output\n", bpc);
1949 ret = vc4_hdmi_encoder_compute_format(vc4_hdmi, vc4_state,
1954 vc4_state->output_bpc = bpc;
1957 "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
1958 mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
1959 vc4_state->output_bpc,
1960 vc4_hdmi_output_fmt_str(vc4_state->output_format),
1961 vc4_state->pixel_rate);
1969 #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
1970 #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
1972 static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
1973 struct drm_crtc_state *crtc_state,
1974 struct drm_connector_state *conn_state)
1976 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
1977 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
1978 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1979 struct drm_connector *connector = &vc4_hdmi->connector;
1980 struct drm_connector_state *old_conn_state = drm_atomic_get_old_connector_state(conn_state->state, connector);
1981 struct vc4_hdmi_connector_state *old_vc4_state = conn_state_to_vc4_hdmi_conn_state(old_conn_state);
1982 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
1983 unsigned long long pixel_rate = mode->clock * 1000;
1984 unsigned long long tmds_rate;
1987 if (vc4_hdmi->variant->unsupported_odd_h_timings) {
1988 if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
1989 /* Only try to fixup DBLCLK modes to get 480i and 576i
1991 * A generic solution for all modes with odd horizontal
1992 * timing values seems impossible based on trying to
1993 * solve it for 1366x768 monitors.
1995 if ((mode->hsync_start - mode->hdisplay) & 1)
1996 mode->hsync_start--;
1997 if ((mode->hsync_end - mode->hsync_start) & 1)
2001 /* Now check whether we still have odd values remaining */
2002 if ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
2003 (mode->hsync_end % 2) || (mode->htotal % 2))
2007 /* 4096x2160@60 is not reliable without overclocking core */
2008 if (mode->hdisplay > 3840 && mode->vdisplay >= 2160 &&
2009 drm_mode_vrefresh(mode) >= 50 &&
2010 !vc4->hvs->vc5_hdmi_enable_4096by2160)
2014 * The 1440p@60 pixel rate is in the same range than the first
2015 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
2016 * bandwidth). Slightly lower the frequency to bring it out of
2019 tmds_rate = pixel_rate * 10;
2020 if (vc4_hdmi->disable_wifi_frequencies &&
2021 (tmds_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
2022 tmds_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
2023 mode->clock = 238560;
2024 pixel_rate = mode->clock * 1000;
2027 ret = vc4_hdmi_encoder_compute_config(vc4_hdmi, vc4_state, mode);
2031 /* vc4_hdmi_encoder_compute_config may have changed output_bpc and/or output_format */
2032 if (vc4_state->output_bpc != old_vc4_state->output_bpc ||
2033 vc4_state->output_format != old_vc4_state->output_format)
2034 crtc_state->mode_changed = true;
2039 static enum drm_mode_status
2040 vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
2041 const struct drm_display_mode *mode)
2043 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2044 const struct drm_connector *connector = &vc4_hdmi->connector;
2045 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
2047 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
2048 !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
2049 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
2050 (mode->hsync_end % 2) || (mode->htotal % 2)))
2051 return MODE_H_ILLEGAL;
2053 if (mode->hdisplay > 3840 && mode->vdisplay >= 2160 &&
2054 drm_mode_vrefresh(mode) >= 50 &&
2055 !vc4->hvs->vc5_hdmi_enable_4096by2160)
2056 return MODE_CLOCK_HIGH;
2058 return vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode->clock * 1000);
2061 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
2062 .atomic_check = vc4_hdmi_encoder_atomic_check,
2063 .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
2064 .mode_valid = vc4_hdmi_encoder_mode_valid,
2067 static int vc4_hdmi_late_register(struct drm_encoder *encoder)
2069 struct drm_device *drm = encoder->dev;
2070 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2071 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
2074 ret = vc4_debugfs_add_file(drm->primary, variant->debugfs_name,
2075 vc4_hdmi_debugfs_regs,
2083 static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
2084 .late_register = vc4_hdmi_late_register,
2087 static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
2090 u32 channel_map = 0;
2092 for (i = 0; i < 8; i++) {
2093 if (channel_mask & BIT(i))
2094 channel_map |= i << (3 * i);
2099 static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
2102 u32 channel_map = 0;
2104 for (i = 0; i < 8; i++) {
2105 if (channel_mask & BIT(i))
2106 channel_map |= i << (4 * i);
2111 static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi)
2113 struct drm_device *drm = vc4_hdmi->connector.dev;
2114 unsigned long flags;
2118 if (!drm_dev_enter(drm, &idx))
2121 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2122 hotplug = HDMI_READ(HDMI_HOTPLUG);
2123 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2127 return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED);
2130 /* HDMI audio codec callbacks */
2131 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
2132 unsigned int samplerate)
2134 struct drm_device *drm = vc4_hdmi->connector.dev;
2136 unsigned long flags;
2140 if (!drm_dev_enter(drm, &idx))
2143 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
2144 rational_best_approximation(hsm_clock, samplerate,
2145 VC4_HD_MAI_SMP_N_MASK >>
2146 VC4_HD_MAI_SMP_N_SHIFT,
2147 (VC4_HD_MAI_SMP_M_MASK >>
2148 VC4_HD_MAI_SMP_M_SHIFT) + 1,
2151 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2152 HDMI_WRITE(HDMI_MAI_SMP,
2153 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
2154 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
2155 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2160 static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
2162 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
2166 lockdep_assert_held(&vc4_hdmi->mutex);
2167 lockdep_assert_held(&vc4_hdmi->hw_lock);
2169 n = 128 * samplerate / 1000;
2170 tmp = (u64)(mode->clock * 1000) * n;
2171 do_div(tmp, 128 * samplerate);
2174 HDMI_WRITE(HDMI_CRP_CFG,
2175 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
2176 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
2179 * We could get slightly more accurate clocks in some cases by
2180 * providing a CTS_1 value. The two CTS values are alternated
2181 * between based on the period fields
2183 HDMI_WRITE(HDMI_CTS_0, cts);
2184 HDMI_WRITE(HDMI_CTS_1, cts);
2187 static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
2189 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
2191 return snd_soc_card_get_drvdata(card);
2194 static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
2196 lockdep_assert_held(&vc4_hdmi->mutex);
2199 * If the encoder is currently in DVI mode, treat the codec DAI
2202 if (!vc4_hdmi->encoder.hdmi_monitor)
2208 static int vc4_hdmi_audio_startup(struct device *dev, void *data)
2210 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2211 struct drm_device *drm = vc4_hdmi->connector.dev;
2212 unsigned long flags;
2216 mutex_lock(&vc4_hdmi->mutex);
2218 if (!drm_dev_enter(drm, &idx)) {
2223 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2228 vc4_hdmi->audio.streaming = true;
2230 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2231 HDMI_WRITE(HDMI_MAI_CTL,
2232 VC4_HD_MAI_CTL_RESET |
2233 VC4_HD_MAI_CTL_FLUSH |
2234 VC4_HD_MAI_CTL_DLATE |
2235 VC4_HD_MAI_CTL_ERRORE |
2236 VC4_HD_MAI_CTL_ERRORF);
2237 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2239 if (vc4_hdmi->variant->phy_rng_enable)
2240 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
2245 mutex_unlock(&vc4_hdmi->mutex);
2250 static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
2252 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
2253 struct device *dev = &vc4_hdmi->pdev->dev;
2254 unsigned long flags;
2257 lockdep_assert_held(&vc4_hdmi->mutex);
2259 vc4_hdmi->audio.streaming = false;
2260 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
2262 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
2264 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2266 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
2267 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
2268 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
2270 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2273 static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
2275 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2276 struct drm_device *drm = vc4_hdmi->connector.dev;
2277 unsigned long flags;
2280 mutex_lock(&vc4_hdmi->mutex);
2282 if (!drm_dev_enter(drm, &idx))
2285 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2287 HDMI_WRITE(HDMI_MAI_CTL,
2288 VC4_HD_MAI_CTL_DLATE |
2289 VC4_HD_MAI_CTL_ERRORE |
2290 VC4_HD_MAI_CTL_ERRORF);
2292 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2294 if (vc4_hdmi->variant->phy_rng_disable)
2295 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
2297 vc4_hdmi->audio.streaming = false;
2298 vc4_hdmi_audio_reset(vc4_hdmi);
2303 mutex_unlock(&vc4_hdmi->mutex);
2306 static int sample_rate_to_mai_fmt(int samplerate)
2308 switch (samplerate) {
2310 return VC4_HDMI_MAI_SAMPLE_RATE_8000;
2312 return VC4_HDMI_MAI_SAMPLE_RATE_11025;
2314 return VC4_HDMI_MAI_SAMPLE_RATE_12000;
2316 return VC4_HDMI_MAI_SAMPLE_RATE_16000;
2318 return VC4_HDMI_MAI_SAMPLE_RATE_22050;
2320 return VC4_HDMI_MAI_SAMPLE_RATE_24000;
2322 return VC4_HDMI_MAI_SAMPLE_RATE_32000;
2324 return VC4_HDMI_MAI_SAMPLE_RATE_44100;
2326 return VC4_HDMI_MAI_SAMPLE_RATE_48000;
2328 return VC4_HDMI_MAI_SAMPLE_RATE_64000;
2330 return VC4_HDMI_MAI_SAMPLE_RATE_88200;
2332 return VC4_HDMI_MAI_SAMPLE_RATE_96000;
2334 return VC4_HDMI_MAI_SAMPLE_RATE_128000;
2336 return VC4_HDMI_MAI_SAMPLE_RATE_176400;
2338 return VC4_HDMI_MAI_SAMPLE_RATE_192000;
2340 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
2344 /* HDMI audio codec callbacks */
2345 static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
2346 struct hdmi_codec_daifmt *daifmt,
2347 struct hdmi_codec_params *params)
2349 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2350 struct drm_device *drm = vc4_hdmi->connector.dev;
2351 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
2352 unsigned int sample_rate = params->sample_rate;
2353 unsigned int channels = params->channels;
2354 unsigned long flags;
2355 u32 audio_packet_config, channel_mask;
2357 u32 mai_audio_format;
2358 u32 mai_sample_rate;
2362 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
2363 sample_rate, params->sample_width, channels);
2365 mutex_lock(&vc4_hdmi->mutex);
2367 if (!drm_dev_enter(drm, &idx)) {
2372 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2377 vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
2379 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2380 HDMI_WRITE(HDMI_MAI_CTL,
2381 VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
2382 VC4_HD_MAI_CTL_WHOLSMP |
2383 VC4_HD_MAI_CTL_CHALIGN |
2384 VC4_HD_MAI_CTL_ENABLE);
2386 mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
2387 if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
2388 params->channels == 8)
2389 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
2391 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
2392 HDMI_WRITE(HDMI_MAI_FMT,
2393 VC4_SET_FIELD(mai_sample_rate,
2394 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
2395 VC4_SET_FIELD(mai_audio_format,
2396 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));
2398 /* The B frame identifier should match the value used by alsa-lib (8) */
2399 audio_packet_config =
2400 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
2401 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
2402 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
2404 channel_mask = GENMASK(channels - 1, 0);
2405 audio_packet_config |= VC4_SET_FIELD(channel_mask,
2406 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
2408 /* Set the MAI threshold */
2409 HDMI_WRITE(HDMI_MAI_THR,
2410 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICHIGH) |
2411 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICLOW) |
2412 VC4_SET_FIELD(0x06, VC4_HD_MAI_THR_DREQHIGH) |
2413 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_DREQLOW));
2415 HDMI_WRITE(HDMI_MAI_CONFIG,
2416 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
2417 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
2418 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
2420 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
2421 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
2422 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
2424 vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
2426 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2428 memcpy(&vc4_hdmi->audio.infoframe, ¶ms->cea, sizeof(params->cea));
2429 if (vc4_hdmi->output_enabled)
2430 vc4_hdmi_set_audio_infoframe(encoder);
2435 mutex_unlock(&vc4_hdmi->mutex);
2440 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
2441 .name = "vc4-hdmi-cpu-dai-component",
2444 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
2446 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
2448 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
2453 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
2454 .name = "vc4-hdmi-cpu-dai",
2455 .probe = vc4_hdmi_audio_cpu_dai_probe,
2457 .stream_name = "Playback",
2460 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
2461 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
2462 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
2463 SNDRV_PCM_RATE_192000,
2464 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
2468 static const struct snd_dmaengine_pcm_config pcm_conf = {
2469 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
2470 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
2473 static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
2474 uint8_t *buf, size_t len)
2476 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2477 struct drm_connector *connector = &vc4_hdmi->connector;
2479 mutex_lock(&vc4_hdmi->mutex);
2480 memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
2481 mutex_unlock(&vc4_hdmi->mutex);
2486 static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
2487 .get_eld = vc4_hdmi_audio_get_eld,
2488 .prepare = vc4_hdmi_audio_prepare,
2489 .audio_shutdown = vc4_hdmi_audio_shutdown,
2490 .audio_startup = vc4_hdmi_audio_startup,
2493 static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
2494 .ops = &vc4_hdmi_codec_ops,
2495 .max_i2s_channels = 8,
2499 static void vc4_hdmi_audio_codec_release(void *ptr)
2501 struct vc4_hdmi *vc4_hdmi = ptr;
2503 platform_device_unregister(vc4_hdmi->audio.codec_pdev);
2504 vc4_hdmi->audio.codec_pdev = NULL;
2507 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
2509 const struct vc4_hdmi_register *mai_data =
2510 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
2511 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
2512 struct snd_soc_card *card = &vc4_hdmi->audio.card;
2513 struct device *dev = &vc4_hdmi->pdev->dev;
2514 struct platform_device *codec_pdev;
2521 * ASoC makes it a bit hard to retrieve a pointer to the
2522 * vc4_hdmi structure. Registering the card will overwrite our
2523 * device drvdata with a pointer to the snd_soc_card structure,
2524 * which can then be used to retrieve whatever drvdata we want
2527 * However, that doesn't fly in the case where we wouldn't
2528 * register an ASoC card (because of an old DT that is missing
2529 * the dmas properties for example), then the card isn't
2530 * registered and the device drvdata wouldn't be set.
2532 * We can deal with both cases by making sure a snd_soc_card
2533 * pointer and a vc4_hdmi structure are pointing to the same
2534 * memory address, so we can treat them indistinctly without any
2537 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
2538 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
2540 if (!of_find_property(dev->of_node, "dmas", &len) ||
2543 "'dmas' DT property is missing or empty, no HDMI audio\n");
2547 if (mai_data->reg != VC4_HD) {
2548 WARN_ONCE(true, "MAI isn't in the HD block\n");
2553 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
2554 * the bus address specified in the DT, because the physical address
2555 * (the one returned by platform_get_resource()) is not appropriate
2556 * for DMA transfers.
2557 * This VC/MMU should probably be exposed to avoid this kind of hacks.
2559 index = of_property_match_string(dev->of_node, "reg-names", "hd");
2560 /* Before BCM2711, we don't have a named register range */
2564 addr = of_get_address(dev->of_node, index, NULL, NULL);
2566 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
2567 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2568 vc4_hdmi->audio.dma_data.maxburst = 2;
2571 * NOTE: Strictly speaking, we should probably use a DRM-managed
2572 * registration there to avoid removing all the audio components
2573 * by the time the driver doesn't have any user anymore.
2575 * However, the ASoC core uses a number of devm_kzalloc calls
2576 * when registering, even when using non-device-managed
2577 * functions (such as in snd_soc_register_component()).
2579 * If we call snd_soc_unregister_component() in a DRM-managed
2580 * action, the device-managed actions have already been executed
2581 * and thus we would access memory that has been freed.
2583 * Using device-managed hooks here probably leaves us open to a
2584 * bunch of issues if userspace still has a handle on the ALSA
2585 * device when the device is removed. However, this is mitigated
2586 * by the use of drm_dev_enter()/drm_dev_exit() in the audio
2587 * path to prevent the access to the device resources if it
2588 * isn't there anymore.
2590 * Then, the vc4_hdmi structure is DRM-managed and thus only
2591 * freed whenever the last user has closed the DRM device file.
2592 * It should thus outlive ALSA in most situations.
2594 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
2596 dev_err(dev, "Could not register PCM component: %d\n", ret);
2600 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
2601 &vc4_hdmi_audio_cpu_dai_drv, 1);
2603 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
2607 codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
2608 PLATFORM_DEVID_AUTO,
2609 &vc4_hdmi_codec_pdata,
2610 sizeof(vc4_hdmi_codec_pdata));
2611 if (IS_ERR(codec_pdev)) {
2612 dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
2613 return PTR_ERR(codec_pdev);
2615 vc4_hdmi->audio.codec_pdev = codec_pdev;
2617 ret = devm_add_action_or_reset(dev, vc4_hdmi_audio_codec_release, vc4_hdmi);
2621 dai_link->cpus = &vc4_hdmi->audio.cpu;
2622 dai_link->codecs = &vc4_hdmi->audio.codec;
2623 dai_link->platforms = &vc4_hdmi->audio.platform;
2625 dai_link->num_cpus = 1;
2626 dai_link->num_codecs = 1;
2627 dai_link->num_platforms = 1;
2629 dai_link->name = "MAI";
2630 dai_link->stream_name = "MAI PCM";
2631 dai_link->codecs->dai_name = "i2s-hifi";
2632 dai_link->cpus->dai_name = dev_name(dev);
2633 dai_link->codecs->name = dev_name(&codec_pdev->dev);
2634 dai_link->platforms->name = dev_name(dev);
2636 card->dai_link = dai_link;
2637 card->num_links = 1;
2638 card->name = vc4_hdmi->variant->card_name;
2639 card->driver_name = "vc4-hdmi";
2641 card->owner = THIS_MODULE;
2644 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
2645 * stores a pointer to the snd card object in dev->driver_data. This
2646 * means we cannot use it for something else. The hdmi back-pointer is
2647 * now stored in card->drvdata and should be retrieved with
2648 * snd_soc_card_get_drvdata() if needed.
2650 snd_soc_card_set_drvdata(card, vc4_hdmi);
2651 ret = devm_snd_soc_register_card(dev, card);
2653 dev_err_probe(dev, ret, "Could not register sound card\n");
2659 static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
2661 struct vc4_hdmi *vc4_hdmi = priv;
2662 struct drm_connector *connector = &vc4_hdmi->connector;
2663 struct drm_device *dev = connector->dev;
2665 if (dev && dev->registered)
2666 drm_connector_helper_hpd_irq_event(connector);
2671 static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
2673 struct drm_connector *connector = &vc4_hdmi->connector;
2674 struct platform_device *pdev = vc4_hdmi->pdev;
2677 if (vc4_hdmi->variant->external_irq_controller) {
2678 unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
2679 unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
2681 ret = devm_request_threaded_irq(&pdev->dev, hpd_con,
2683 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2684 "vc4 hdmi hpd connected", vc4_hdmi);
2688 ret = devm_request_threaded_irq(&pdev->dev, hpd_rm,
2690 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2691 "vc4 hdmi hpd disconnected", vc4_hdmi);
2695 connector->polled = DRM_CONNECTOR_POLL_HPD;
2701 #ifdef CONFIG_DRM_VC4_HDMI_CEC
2702 static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
2704 struct vc4_hdmi *vc4_hdmi = priv;
2706 if (vc4_hdmi->cec_rx_msg.len)
2707 cec_received_msg(vc4_hdmi->cec_adap,
2708 &vc4_hdmi->cec_rx_msg);
2713 static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
2715 struct vc4_hdmi *vc4_hdmi = priv;
2717 if (vc4_hdmi->cec_tx_ok) {
2718 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
2722 * This CEC implementation makes 1 retry, so if we
2723 * get a NACK, then that means it made 2 attempts.
2725 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
2731 static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
2733 struct vc4_hdmi *vc4_hdmi = priv;
2736 if (vc4_hdmi->cec_irq_was_rx)
2737 ret = vc4_cec_irq_handler_rx_thread(irq, priv);
2739 ret = vc4_cec_irq_handler_tx_thread(irq, priv);
2744 static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
2746 struct drm_device *dev = vc4_hdmi->connector.dev;
2747 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
2750 lockdep_assert_held(&vc4_hdmi->hw_lock);
2752 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
2753 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
2755 if (msg->len > 16) {
2756 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
2760 for (i = 0; i < msg->len; i += 4) {
2761 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
2763 msg->msg[i] = val & 0xff;
2764 msg->msg[i + 1] = (val >> 8) & 0xff;
2765 msg->msg[i + 2] = (val >> 16) & 0xff;
2766 msg->msg[i + 3] = (val >> 24) & 0xff;
2770 static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2775 * We don't need to protect the register access using
2776 * drm_dev_enter() there because the interrupt handler lifetime
2777 * is tied to the device itself, and not to the DRM device.
2779 * So when the device will be gone, one of the first thing we
2780 * will be doing will be to unregister the interrupt handler,
2781 * and then unregister the DRM device. drm_dev_enter() would
2782 * thus always succeed if we are here.
2785 lockdep_assert_held(&vc4_hdmi->hw_lock);
2787 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
2788 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
2789 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
2790 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
2792 return IRQ_WAKE_THREAD;
2795 static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
2797 struct vc4_hdmi *vc4_hdmi = priv;
2800 spin_lock(&vc4_hdmi->hw_lock);
2801 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
2802 spin_unlock(&vc4_hdmi->hw_lock);
2807 static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2811 lockdep_assert_held(&vc4_hdmi->hw_lock);
2814 * We don't need to protect the register access using
2815 * drm_dev_enter() there because the interrupt handler lifetime
2816 * is tied to the device itself, and not to the DRM device.
2818 * So when the device will be gone, one of the first thing we
2819 * will be doing will be to unregister the interrupt handler,
2820 * and then unregister the DRM device. drm_dev_enter() would
2821 * thus always succeed if we are here.
2824 vc4_hdmi->cec_rx_msg.len = 0;
2825 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
2826 vc4_cec_read_msg(vc4_hdmi, cntrl1);
2827 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
2828 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
2829 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
2831 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
2833 return IRQ_WAKE_THREAD;
2836 static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
2838 struct vc4_hdmi *vc4_hdmi = priv;
2841 spin_lock(&vc4_hdmi->hw_lock);
2842 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
2843 spin_unlock(&vc4_hdmi->hw_lock);
2848 static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
2850 struct vc4_hdmi *vc4_hdmi = priv;
2851 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
2856 * We don't need to protect the register access using
2857 * drm_dev_enter() there because the interrupt handler lifetime
2858 * is tied to the device itself, and not to the DRM device.
2860 * So when the device will be gone, one of the first thing we
2861 * will be doing will be to unregister the interrupt handler,
2862 * and then unregister the DRM device. drm_dev_enter() would
2863 * thus always succeed if we are here.
2866 if (!(stat & VC4_HDMI_CPU_CEC))
2869 spin_lock(&vc4_hdmi->hw_lock);
2870 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
2871 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
2872 if (vc4_hdmi->cec_irq_was_rx)
2873 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
2875 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
2877 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
2878 spin_unlock(&vc4_hdmi->hw_lock);
2883 static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
2885 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2886 struct drm_device *drm = vc4_hdmi->connector.dev;
2887 /* clock period in microseconds */
2888 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
2889 unsigned long flags;
2895 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2896 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2897 * .detect or .get_modes might call .adap_enable, which leads to this
2898 * function being called with that mutex held.
2900 * Concurrency is not an issue for the moment since we don't share any
2901 * state with KMS, so we can ignore the lock for now, but we need to
2902 * keep it in mind if we were to change that assumption.
2905 if (!drm_dev_enter(drm, &idx))
2907 * We can't return an error code, because the CEC
2908 * framework will emit WARN_ON messages at unbind
2913 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
2919 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2921 val = HDMI_READ(HDMI_CEC_CNTRL_5);
2922 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
2923 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
2924 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
2925 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
2926 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
2928 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
2929 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
2930 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
2931 HDMI_WRITE(HDMI_CEC_CNTRL_2,
2932 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
2933 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
2934 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
2935 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
2936 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
2937 HDMI_WRITE(HDMI_CEC_CNTRL_3,
2938 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
2939 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
2940 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
2941 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
2942 HDMI_WRITE(HDMI_CEC_CNTRL_4,
2943 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
2944 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
2945 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
2946 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
2948 if (!vc4_hdmi->variant->external_irq_controller)
2949 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
2951 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2958 static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
2960 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2961 struct drm_device *drm = vc4_hdmi->connector.dev;
2962 unsigned long flags;
2965 if (!drm_dev_enter(drm, &idx))
2967 * We can't return an error code, because the CEC
2968 * framework will emit WARN_ON messages at unbind
2974 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2975 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2976 * .detect or .get_modes might call .adap_enable, which leads to this
2977 * function being called with that mutex held.
2979 * Concurrency is not an issue for the moment since we don't share any
2980 * state with KMS, so we can ignore the lock for now, but we need to
2981 * keep it in mind if we were to change that assumption.
2984 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2986 if (!vc4_hdmi->variant->external_irq_controller)
2987 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
2989 HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
2990 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
2992 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2994 pm_runtime_put(&vc4_hdmi->pdev->dev);
3001 static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
3004 return vc4_hdmi_cec_enable(adap);
3006 return vc4_hdmi_cec_disable(adap);
3009 static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
3011 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3012 struct drm_device *drm = vc4_hdmi->connector.dev;
3013 unsigned long flags;
3017 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
3018 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
3019 * .detect or .get_modes might call .adap_enable, which leads to this
3020 * function being called with that mutex held.
3022 * Concurrency is not an issue for the moment since we don't share any
3023 * state with KMS, so we can ignore the lock for now, but we need to
3024 * keep it in mind if we were to change that assumption.
3027 if (!drm_dev_enter(drm, &idx))
3029 * We can't return an error code, because the CEC
3030 * framework will emit WARN_ON messages at unbind
3035 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3036 HDMI_WRITE(HDMI_CEC_CNTRL_1,
3037 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
3038 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
3039 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3046 static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
3047 u32 signal_free_time, struct cec_msg *msg)
3049 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3050 struct drm_device *dev = vc4_hdmi->connector.dev;
3051 unsigned long flags;
3057 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
3058 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
3059 * .detect or .get_modes might call .adap_enable, which leads to this
3060 * function being called with that mutex held.
3062 * Concurrency is not an issue for the moment since we don't share any
3063 * state with KMS, so we can ignore the lock for now, but we need to
3064 * keep it in mind if we were to change that assumption.
3067 if (!drm_dev_enter(dev, &idx))
3070 if (msg->len > 16) {
3071 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
3076 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3078 for (i = 0; i < msg->len; i += 4)
3079 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
3081 (msg->msg[i + 1] << 8) |
3082 (msg->msg[i + 2] << 16) |
3083 (msg->msg[i + 3] << 24));
3085 val = HDMI_READ(HDMI_CEC_CNTRL_1);
3086 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
3087 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
3088 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
3089 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
3090 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
3092 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
3094 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3101 static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
3102 .adap_enable = vc4_hdmi_cec_adap_enable,
3103 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
3104 .adap_transmit = vc4_hdmi_cec_adap_transmit,
3107 static void vc4_hdmi_cec_release(void *ptr)
3109 struct vc4_hdmi *vc4_hdmi = ptr;
3111 cec_unregister_adapter(vc4_hdmi->cec_adap);
3112 vc4_hdmi->cec_adap = NULL;
3115 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
3117 struct cec_connector_info conn_info;
3118 struct platform_device *pdev = vc4_hdmi->pdev;
3119 struct device *dev = &pdev->dev;
3120 unsigned long flags;
3123 if (!of_find_property(dev->of_node, "interrupts", NULL)) {
3124 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
3128 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
3131 CEC_CAP_CONNECTOR_INFO, 1);
3132 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
3136 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
3137 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
3139 if (vc4_hdmi->variant->external_irq_controller) {
3140 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-rx"),
3141 vc4_cec_irq_handler_rx_bare,
3142 vc4_cec_irq_handler_rx_thread, 0,
3143 "vc4 hdmi cec rx", vc4_hdmi);
3145 goto err_delete_cec_adap;
3147 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-tx"),
3148 vc4_cec_irq_handler_tx_bare,
3149 vc4_cec_irq_handler_tx_thread, 0,
3150 "vc4 hdmi cec tx", vc4_hdmi);
3152 goto err_delete_cec_adap;
3154 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3155 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
3156 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3158 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
3159 vc4_cec_irq_handler,
3160 vc4_cec_irq_handler_thread, 0,
3161 "vc4 hdmi cec", vc4_hdmi);
3163 goto err_delete_cec_adap;
3166 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
3168 goto err_delete_cec_adap;
3171 * NOTE: Strictly speaking, we should probably use a DRM-managed
3172 * registration there to avoid removing the CEC adapter by the
3173 * time the DRM driver doesn't have any user anymore.
3175 * However, the CEC framework already cleans up the CEC adapter
3176 * only when the last user has closed its file descriptor, so we
3177 * don't need to handle it in DRM.
3179 * By the time the device-managed hook is executed, we will give
3180 * up our reference to the CEC adapter and therefore don't
3181 * really care when it's actually freed.
3183 * There's still a problematic sequence: if we unregister our
3184 * CEC adapter, but the userspace keeps a handle on the CEC
3185 * adapter but not the DRM device for some reason. In such a
3186 * case, our vc4_hdmi structure will be freed, but the
3187 * cec_adapter structure will have a dangling pointer to what
3188 * used to be our HDMI controller. If we get a CEC call at that
3189 * moment, we could end up with a use-after-free. Fortunately,
3190 * the CEC framework already handles this too, by calling
3191 * cec_is_registered() in cec_ioctl() and cec_poll().
3193 ret = devm_add_action_or_reset(dev, vc4_hdmi_cec_release, vc4_hdmi);
3199 err_delete_cec_adap:
3200 cec_delete_adapter(vc4_hdmi->cec_adap);
3205 static int vc4_hdmi_cec_resume(struct vc4_hdmi *vc4_hdmi)
3207 unsigned long flags;
3210 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3211 value = HDMI_READ(HDMI_CEC_CNTRL_1);
3212 /* Set the logical address to Unregistered */
3213 value |= VC4_HDMI_CEC_ADDR_MASK;
3214 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
3215 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3217 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
3219 if (!vc4_hdmi->variant->external_irq_controller) {
3220 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3221 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
3222 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3228 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
3233 static int vc4_hdmi_cec_resume(struct vc4_hdmi *vc4_hdmi)
3239 static void vc4_hdmi_free_regset(struct drm_device *drm, void *ptr)
3241 struct debugfs_reg32 *regs = ptr;
3246 static int vc4_hdmi_build_regset(struct drm_device *drm,
3247 struct vc4_hdmi *vc4_hdmi,
3248 struct debugfs_regset32 *regset,
3249 enum vc4_hdmi_regs reg)
3251 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
3252 struct debugfs_reg32 *regs, *new_regs;
3253 unsigned int count = 0;
3257 regs = kcalloc(variant->num_registers, sizeof(*regs),
3262 for (i = 0; i < variant->num_registers; i++) {
3263 const struct vc4_hdmi_register *field = &variant->registers[i];
3265 if (field->reg != reg)
3268 regs[count].name = field->name;
3269 regs[count].offset = field->offset;
3273 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
3277 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
3278 regset->regs = new_regs;
3279 regset->nregs = count;
3281 ret = drmm_add_action_or_reset(drm, vc4_hdmi_free_regset, new_regs);
3288 static int vc4_hdmi_init_resources(struct drm_device *drm,
3289 struct vc4_hdmi *vc4_hdmi)
3291 struct platform_device *pdev = vc4_hdmi->pdev;
3292 struct device *dev = &pdev->dev;
3295 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
3296 if (IS_ERR(vc4_hdmi->hdmicore_regs))
3297 return PTR_ERR(vc4_hdmi->hdmicore_regs);
3299 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
3300 if (IS_ERR(vc4_hdmi->hd_regs))
3301 return PTR_ERR(vc4_hdmi->hd_regs);
3303 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3307 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3311 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
3312 if (IS_ERR(vc4_hdmi->pixel_clock)) {
3313 ret = PTR_ERR(vc4_hdmi->pixel_clock);
3314 if (ret != -EPROBE_DEFER)
3315 DRM_ERROR("Failed to get pixel clock\n");
3319 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3320 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3321 DRM_ERROR("Failed to get HDMI state machine clock\n");
3322 return PTR_ERR(vc4_hdmi->hsm_clock);
3324 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
3325 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
3330 static int vc5_hdmi_init_resources(struct drm_device *drm,
3331 struct vc4_hdmi *vc4_hdmi)
3333 struct platform_device *pdev = vc4_hdmi->pdev;
3334 struct device *dev = &pdev->dev;
3335 struct resource *res;
3338 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
3342 vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
3343 resource_size(res));
3344 if (!vc4_hdmi->hdmicore_regs)
3347 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
3351 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
3352 if (!vc4_hdmi->hd_regs)
3355 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
3359 vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
3360 if (!vc4_hdmi->cec_regs)
3363 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
3367 vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
3368 if (!vc4_hdmi->csc_regs)
3371 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
3375 vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
3376 if (!vc4_hdmi->dvp_regs)
3379 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
3383 vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
3384 if (!vc4_hdmi->phy_regs)
3387 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
3391 vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
3392 if (!vc4_hdmi->ram_regs)
3395 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
3399 vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
3400 if (!vc4_hdmi->rm_regs)
3403 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3404 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3405 DRM_ERROR("Failed to get HDMI state machine clock\n");
3406 return PTR_ERR(vc4_hdmi->hsm_clock);
3409 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
3410 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
3411 DRM_ERROR("Failed to get pixel bvb clock\n");
3412 return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
3415 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
3416 if (IS_ERR(vc4_hdmi->audio_clock)) {
3417 DRM_ERROR("Failed to get audio clock\n");
3418 return PTR_ERR(vc4_hdmi->audio_clock);
3421 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
3422 if (IS_ERR(vc4_hdmi->cec_clock)) {
3423 DRM_ERROR("Failed to get CEC clock\n");
3424 return PTR_ERR(vc4_hdmi->cec_clock);
3427 vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
3428 if (IS_ERR(vc4_hdmi->reset)) {
3429 DRM_ERROR("Failed to get HDMI reset line\n");
3430 return PTR_ERR(vc4_hdmi->reset);
3433 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3437 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3441 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->cec_regset, VC5_CEC);
3445 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->csc_regset, VC5_CSC);
3449 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->dvp_regset, VC5_DVP);
3453 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->phy_regset, VC5_PHY);
3457 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->ram_regset, VC5_RAM);
3461 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->rm_regset, VC5_RM);
3468 static int __maybe_unused vc4_hdmi_runtime_suspend(struct device *dev)
3470 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3472 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3477 static int vc4_hdmi_runtime_resume(struct device *dev)
3479 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3482 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
3486 if (vc4_hdmi->variant->reset)
3487 vc4_hdmi->variant->reset(vc4_hdmi);
3489 ret = vc4_hdmi_cec_resume(vc4_hdmi);
3491 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3498 static void vc4_hdmi_put_ddc_device(void *ptr)
3500 struct vc4_hdmi *vc4_hdmi = ptr;
3502 put_device(&vc4_hdmi->ddc->dev);
3505 static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
3507 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
3508 struct platform_device *pdev = to_platform_device(dev);
3509 struct drm_device *drm = dev_get_drvdata(master);
3510 struct vc4_hdmi *vc4_hdmi;
3511 struct drm_encoder *encoder;
3512 struct device_node *ddc_node;
3515 vc4_hdmi = drmm_kzalloc(drm, sizeof(*vc4_hdmi), GFP_KERNEL);
3519 ret = drmm_mutex_init(drm, &vc4_hdmi->mutex);
3523 spin_lock_init(&vc4_hdmi->hw_lock);
3524 INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
3526 dev_set_drvdata(dev, vc4_hdmi);
3527 encoder = &vc4_hdmi->encoder.base.base;
3528 vc4_hdmi->encoder.base.type = variant->encoder_type;
3529 vc4_hdmi->encoder.base.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
3530 vc4_hdmi->encoder.base.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
3531 vc4_hdmi->encoder.base.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
3532 vc4_hdmi->encoder.base.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
3533 vc4_hdmi->encoder.base.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
3534 vc4_hdmi->pdev = pdev;
3535 vc4_hdmi->variant = variant;
3538 * Since we don't know the state of the controller and its
3539 * display (if any), let's assume it's always enabled.
3540 * vc4_hdmi_disable_scrambling() will thus run at boot, make
3541 * sure it's disabled, and avoid any inconsistency.
3543 if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK)
3544 vc4_hdmi->scdc_enabled = true;
3546 ret = variant->init_resources(drm, vc4_hdmi);
3550 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
3552 DRM_ERROR("Failed to find ddc node in device tree\n");
3556 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
3557 of_node_put(ddc_node);
3558 if (!vc4_hdmi->ddc) {
3559 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
3560 return -EPROBE_DEFER;
3563 ret = devm_add_action_or_reset(dev, vc4_hdmi_put_ddc_device, vc4_hdmi);
3567 /* Only use the GPIO HPD pin if present in the DT, otherwise
3568 * we'll use the HDMI core's register.
3570 vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
3571 if (IS_ERR(vc4_hdmi->hpd_gpio)) {
3572 return PTR_ERR(vc4_hdmi->hpd_gpio);
3575 vc4_hdmi->disable_wifi_frequencies =
3576 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
3579 * If we boot without any cable connected to the HDMI connector,
3580 * the firmware will skip the HSM initialization and leave it
3581 * with a rate of 0, resulting in a bus lockup when we're
3582 * accessing the registers even if it's enabled.
3584 * Let's put a sensible default at runtime_resume so that we
3585 * don't end up in this situation.
3587 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, HSM_MIN_CLOCK_FREQ);
3591 ret = devm_pm_runtime_enable(dev);
3595 ret = pm_runtime_resume_and_get(dev);
3599 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
3600 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
3601 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
3602 clk_prepare_enable(vc4_hdmi->pixel_clock);
3603 clk_prepare_enable(vc4_hdmi->hsm_clock);
3604 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
3607 ret = drmm_encoder_init(drm, encoder,
3608 &vc4_hdmi_encoder_funcs,
3609 DRM_MODE_ENCODER_TMDS,
3612 goto err_put_runtime_pm;
3614 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
3616 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
3618 goto err_put_runtime_pm;
3620 ret = vc4_hdmi_hotplug_init(vc4_hdmi);
3622 goto err_put_runtime_pm;
3624 ret = vc4_hdmi_cec_init(vc4_hdmi);
3626 goto err_put_runtime_pm;
3628 ret = vc4_hdmi_audio_init(vc4_hdmi);
3630 goto err_put_runtime_pm;
3632 pm_runtime_put_sync(dev);
3637 pm_runtime_put_sync(dev);
3642 static const struct component_ops vc4_hdmi_ops = {
3643 .bind = vc4_hdmi_bind,
3646 static int vc4_hdmi_dev_probe(struct platform_device *pdev)
3648 return component_add(&pdev->dev, &vc4_hdmi_ops);
3651 static int vc4_hdmi_dev_remove(struct platform_device *pdev)
3653 component_del(&pdev->dev, &vc4_hdmi_ops);
3657 static const struct vc4_hdmi_variant bcm2835_variant = {
3658 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3659 .debugfs_name = "hdmi_regs",
3660 .card_name = "vc4-hdmi",
3661 .max_pixel_clock = 162000000,
3662 .registers = vc4_hdmi_fields,
3663 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
3665 .init_resources = vc4_hdmi_init_resources,
3666 .csc_setup = vc4_hdmi_csc_setup,
3667 .reset = vc4_hdmi_reset,
3668 .set_timings = vc4_hdmi_set_timings,
3669 .phy_init = vc4_hdmi_phy_init,
3670 .phy_disable = vc4_hdmi_phy_disable,
3671 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
3672 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
3673 .channel_map = vc4_hdmi_channel_map,
3674 .supports_hdr = false,
3677 static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
3678 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3679 .debugfs_name = "hdmi0_regs",
3680 .card_name = "vc4-hdmi-0",
3681 .max_pixel_clock = 600000000,
3682 .registers = vc5_hdmi_hdmi0_fields,
3683 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
3684 .phy_lane_mapping = {
3690 .unsupported_odd_h_timings = true,
3691 .external_irq_controller = true,
3693 .init_resources = vc5_hdmi_init_resources,
3694 .csc_setup = vc5_hdmi_csc_setup,
3695 .reset = vc5_hdmi_reset,
3696 .set_timings = vc5_hdmi_set_timings,
3697 .phy_init = vc5_hdmi_phy_init,
3698 .phy_disable = vc5_hdmi_phy_disable,
3699 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3700 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3701 .channel_map = vc5_hdmi_channel_map,
3702 .supports_hdr = true,
3703 .hp_detect = vc5_hdmi_hp_detect,
3706 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
3707 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
3708 .debugfs_name = "hdmi1_regs",
3709 .card_name = "vc4-hdmi-1",
3710 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
3711 .registers = vc5_hdmi_hdmi1_fields,
3712 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
3713 .phy_lane_mapping = {
3719 .unsupported_odd_h_timings = true,
3720 .external_irq_controller = true,
3722 .init_resources = vc5_hdmi_init_resources,
3723 .csc_setup = vc5_hdmi_csc_setup,
3724 .reset = vc5_hdmi_reset,
3725 .set_timings = vc5_hdmi_set_timings,
3726 .phy_init = vc5_hdmi_phy_init,
3727 .phy_disable = vc5_hdmi_phy_disable,
3728 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3729 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3730 .channel_map = vc5_hdmi_channel_map,
3731 .supports_hdr = true,
3732 .hp_detect = vc5_hdmi_hp_detect,
3735 static const struct of_device_id vc4_hdmi_dt_match[] = {
3736 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
3737 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
3738 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
3742 static const struct dev_pm_ops vc4_hdmi_pm_ops = {
3743 SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
3744 vc4_hdmi_runtime_resume,
3748 struct platform_driver vc4_hdmi_driver = {
3749 .probe = vc4_hdmi_dev_probe,
3750 .remove = vc4_hdmi_dev_remove,
3753 .of_match_table = vc4_hdmi_dt_match,
3754 .pm = &vc4_hdmi_pm_ops,